我编写这个程序是为了测试Ubuntu中的FIFO。主程序创建一个子进程来写入某些内容,然后父进程读取并打印它。
/*
communication with named pipe(or FIFO)
@author myqiqiang
@email myqiqiang@gmail.com
*/
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<errno.h>
#include<fcntl.h>
#include<string.h>
#define FIFO_SERVER "/home/myqiqiang/fifoserver" //fifo directioy
#define BUFFERSIZE 80
void main()
{
pid_t pc;
int flag,fd;
char data[BUFFERSIZE+1];
char* test="a test string";
if(mkfifo(FIFO_SERVER,O_CREAT|O_EXCL)<0) //create fifo
{
printf("create named pipe failed\n");
exit(1);
}
printf("create named pipe sucessfully\n");
pc=fork(); //create process
if(pc==0)
{
memset(data,0,strlen(test));
fd=open(FIFO_SERVER,O_WRONLY,0); //open the fifo
if(fd==-1) //if open failed
{
printf("write:cann't open the named pipe\n");
unlink(FIFO_SERVER);
exit(1);
}
flag=write(fd,test,13); //write data
if(flag==-1) //write failed
{
printf("write data error\n");
unlink(FIFO_SERVER);
exit(1);
}
printf("write data successfully\n");
close(fd); //clsoe fifo
unlink(FIFO_SERVER); //delete fifo
}
else
if(pc>0)
{
memset(data,0,strlen(test));
fd=open(FIFO_SERVER,O_RDONLY,0);
if(fd==-1)
{
printf("read:cann't open the named pipe\n");
unlink(FIFO_SERVER);
exit(1);
}
flag=read(fd,data,13);
if(flag==-1)
{
printf("read data error\n");
unlink(FIFO_SERVER);
exit(1);
}
printf("the data is%s\n",data);
close(fd);
unlink(FIFO_SERVER);
}
else
{
printf("create process error!\n");
unlink(FIFO_SERVER);
exit(1);
}
}然而,它显示了这一点,每次我执行,我相信先进先出有本板条箱。
myqiqiang@ubuntu:~/code/ch03/experiment$ ./3
create named pipe sucessfully
read:cann't open the named pipe
write:cann't open the named pipe发布于 2014-04-16 05:43:35
mkfifo()的第二个参数应该是chmod-type模式(例如0777),而不是O_标志的组合。
您的进程正在创建一个没有足够权限的管道。
发布于 2014-04-16 05:47:46
您需要在S_IWUSR、S_IRUSR、S_IRGRP、S_IROTH模式(参考http://pubs.opengroup.org/onlinepubs/009604599/functions/mkfifo.html )中使用mkfifo。
if(mkfifo(FIFO_SERVER, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH)<0)发布于 2017-11-27 23:19:15
当您执行mkfifo并以普通用户(不是root用户)的身份运行时,您的权限是:
P-wx
因此,您需要读取权限,最简单的方法是将其添加到mkfifo标志:
if(mkfifo(FIFO_SERVER,O_CREAT|O_EXCL|S_IRWXU)<0)
它将创建您可以从以下位置读取的文件:
prwx
https://stackoverflow.com/questions/23100788
复制相似问题