Mac 操作系统阻止读取 (POSIX)
我尝试使用经典方案 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在我可以回答自己了,问题是:
在我的例子中,我需要双向传输,但是当我们通过 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!