can not open FIFO

与世无争的帅哥 提交于 2021-01-29 14:48:08

问题


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.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);
        }
}

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

回答1:


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.




回答2:


you need to use mkfifo with S_IWUSR, S_IRUSR, S_IRGRP ,S_IROTH modes refer http://pubs.opengroup.org/onlinepubs/009604599/functions/mkfifo.html

if(mkfifo(FIFO_SERVER, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH)<0)



回答3:


When you do mkfifo and run as normal user (not-root) your permissions are:

p-wx------ 1 root root 0 Nov 27 15:17 fifoserver

So you need to reading permissions, easiest way is to add it to mkfifo FLAGS:

if(mkfifo(FIFO_SERVER,O_CREAT|O_EXCL|S_IRWXU)<0)

It will create file you can read from:

prwx------ 1 root root 0 Nov 27 15:18 fifoserver




回答4:


The fifo is created but not getting open later so check the permission. you can specify permossion while creating a fifo.



来源:https://stackoverflow.com/questions/23100788/can-not-open-fifo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!