Mac 操作系统阻止读取 (POSIX)

发布于 2024-12-11 22:37:41 字数 777 浏览 5 评论 0原文

我尝试使用经典方案 fork()/pipe() 组织处理的父子之间的阻塞传输, 但我不明白为什么只有子进程中的第一个 read() 被阻塞,但所有后续读取都没有阻塞,而且它们在读取后返回的结果不为零!

例如:

父级,首先将文件名写入子级,然后等待答案:

for (NSString* file in filenames) { 

        fprintf(pict_log, "send to conversion file %s\n", filename);
        write(g_pfds[1], filename, 512);
        memset(filename, ' ', 512);
        read(g_pfds[0], filename, 512);
        fprintf(pict_log, "completed for file: %s\n", filename);
}

子级,相同,反之亦然。

while(!g_break_child) 
{
            memset(filename, ' ', 512);
            int read_bytes = read(g_pfds[0], filename, 512);
            // some processing...
            write(g_pfds[1], filename, 512);
        }

每次迭代后我都应该被阻止在 child 的 read() 上,但为什么这没有发生?

I try to organize blocking transfer between parent-child processed using classic scheme fork()/pipe(),
but I coudn't understand why only first read() in child is blocking, but all subsequent reads are not, and besides that they returns not zero result after read!

for example:

parent, first write filename to child, than wait for answer:

for (NSString* file in filenames) { 

        fprintf(pict_log, "send to conversion file %s\n", filename);
        write(g_pfds[1], filename, 512);
        memset(filename, ' ', 512);
        read(g_pfds[0], filename, 512);
        fprintf(pict_log, "completed for file: %s\n", filename);
}

child, the same but vice versa.

while(!g_break_child) 
{
            memset(filename, ' ', 512);
            int read_bytes = read(g_pfds[0], filename, 512);
            // some processing...
            write(g_pfds[1], filename, 512);
        }

I should be blocked on child's read() after each iteration, but why this doesn't happened?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

只是偏爱你 2024-12-18 22:37:41

现在我可以回答自己了,问题是:
在我的例子中,我需要双向传输,但是当我们通过 pipeline() 函数打开几个描述符时,我们因此创建单向通道,如果双向传输,我们需要调用 pipeline() 两次来创建两个单向管道!

Now I could answer to myself, problem is:
in my case I need bidirectional transfer, but when we open couple of descriptors through pipe() func, we thus create unidirectional channel, in case bi-directional transfer we need call pipe() twice to create two unidirectional pipes!

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