简体   繁体   English

打开命名管道的问题

[英]Problem with opening a named pipe

I've run into a problem while trying to send data through pipes, to be more exact: i do not get non-null file descriptors for pipe. 更确切地说,在尝试通过管道发送数据时遇到了一个问题:我没有得到管道的非空文件描述符。 Here is the code for creation of the pipe: 这是创建管道的代码:

//PIPE is defined as a "/tmp/my.fifo"
umask(0);
...
mknod(PIPE,S_IFIFO,0);
...
p=fopen(PIPE,"w");
if (p)
{
    //fprintf(p,"some message");
    fclose(p);
}
else
    printf("Could not open the pipe\n");

Here is the code for reading from the pipe: 这是从管道读取的代码:

cos_pipe = fopen(PIPE,"r");
if (cos_pipe)
{
    fgets(buffer,80,cos_pipe);
    ...
    fclose(cos_pipe);
}
else
{
    printf("Couldn't open the pipe\n");
    usleep(300000);
}

Code is compiled into two different bineries that i launch separately. 代码被编译成两个分别启动的不同二进制文件。 All the output i get is "Couldn't open the pipe". 我得到的所有输出是“无法打开管道”。

On somewhat related note: should the program that created pipe delete it later? 关于一些相关的注意事项:创建管道的程序是否应在以后将其删除?

The mode argument requires permissions too. mode参数也需要权限。 Use S_IFIFO|S_IRUSR|S_IWUSR . 使用S_IFIFO|S_IRUSR|S_IWUSR

Consider using the mkfifo function instead: 考虑改用mkfifo函数:

mkfifo(PIPE,S_IRUSR|S_IWUSR)

You should remove the pipe when you are done using it. 使用完毕后,应删除管道。 Also, what happens if more than one instance of your program is running at once - you're using a fixed name for the pipe. 同样,如果一次运行一个以上的程序实例,会发生什么情况-您对管道使用了固定的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM