打开命名管道时出现问题
我在尝试通过管道发送数据时遇到了问题,更准确地说:我没有获得管道的非空文件描述符。 这是创建管道的代码:
//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");
这是从管道读取的代码:
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);
}
代码被编译成两个不同的二进制文件,我分别启动。我得到的所有输出都是“无法打开管道”。
在某种程度上相关的注释:创建管道的程序应该稍后删除它吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
mode
参数也需要权限。使用S_IFIFO|S_IRUSR|S_IWUSR
。考虑改用
mkfifo
函数:使用完管道后,应将其移除。另外,如果您的程序的多个实例同时运行,会发生什么情况 - 您为管道使用了固定名称。
The
mode
argument requires permissions too. UseS_IFIFO|S_IRUSR|S_IWUSR
.Consider using the
mkfifo
function instead: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.