打开fifo的权限没有设置,修改后的代码:
#include <fcntl.h>#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#define FIFO_SERVER "/tmp/fifoserver"
#define BUFFERSIZE 80
int main()
{
int fd
if((mkfifo(FIFO_SERVER,O_CREAT|O_EXCL|660)<0)&&(errno!=EEXIST))
{
printf("cannot create fifoserver!\n")
exit(-1)
}
printf("fifoserver create suceessfully!\n")
int pid
pid=fork()
if(pid==0)
{
char *bewrite="Hello,world!"
fd=open(FIFO_SERVER,O_WRONLY,0)
if(fd<0)
{
printf("child: cannot open fifoserver!\n")
exit(-1)
}
int wtnum
wtnum=write(fd,bewrite,strlen(bewrite))
if(wtnum<0)
{
printf("fifoserver write error!\n")
exit(-1)
}
printf("fifoserver write successfully!\n")
close(fd)
}
else if(pid>0)
{
char buf[BUFFERSIZE+1]
fd=open(FIFO_SERVER,O_RDONLY,0)
if(fd<0)
{
printf("parent: cannot open fifoserver!\n")
exit(-1)
}
printf("before read()(),buf has no data.\n")
int rdnum
rdnum=read(fd,buf,BUFFERSIZE)
if(rdnum<0)
{
printf("fifoserver read error!\n")
exit(-1)
}
buf[rdnum]=0
printf("after read(),buf has data:\n%s\n",buf)
close(fd)
}
else if(pid<0)
{
printf("fork new process error!\n")
exit(-1)
}
return 0
}
创建 fifo 时,写 0777 (8进制的 777)
你用了 nonblock 方式去打开fifo的写端,此时fifo的读端没有人open,所以一定会返回失败。必须先有人以读的方式打开fifo后,才能以nonblock方式打开写。或者去掉 O_NONBLOCK 参数。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)