打开命名管道时出现问题

发布于 2024-11-02 02:46:29 字数 598 浏览 3 评论 0原文

我在尝试通过管道发送数据时遇到了问题,更准确地说:我没有获得管道的非空文件描述符。 这是创建管道的代码:

//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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤凫 2024-11-09 02:46:29

mode 参数也需要权限。使用S_IFIFO|S_IRUSR|S_IWUSR

考虑改用 mkfifo 函数:

mkfifo(PIPE,S_IRUSR|S_IWUSR)

使用完管道后,应将其移除。另外,如果您的程序的多个实例同时运行,会发生什么情况 - 您为管道使用了固定名称。

The mode argument requires permissions too. Use S_IFIFO|S_IRUSR|S_IWUSR.

Consider using the mkfifo function instead:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文