无法打开FIFO

编程入门 行业动态 更新时间:2024-10-28 06:27:17
本文介绍了无法打开FIFO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我编写此程序来测试Ubuntu中的FIFO.主程序创建一个子进程来写入内容,然后父进程读取并打印它

I write this program to test the FIFO in Ubuntu。The main program create a child process to write something ,and then the parent read and print it

/* communication with named pipe(or FIFO) @author myqiqiang @email myqiqiang@gmail */ #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); } }

但是,每次执行时都会显示此信息,我确定fifo已创建.

however,it shows this every time i execute,i am sure that the fifo has benn crated .

myqiqiang@ubuntu:~/code/ch03/experiment$ ./3 create named pipe sucessfully read:cann't open the named pipe write:cann't open the named pipe

推荐答案

应该是chmod类型的模式(例如0777),而不是O_标志的组合.

The second argument to mkfifo() should be a chmod-type mode (e.g. 0777), not a combination of O_ flags.

您的过程正在创建一个没有足够权限的管道.

Your process is creating a pipe for which it doesn't have sufficient permissions.

更多推荐

无法打开FIFO

本文发布于:2023-11-30 05:09:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648832.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法打开   FIFO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!