所以我試圖在fork()之後的子進程上運行一個系統命令(或者exec,或者其他),然後向它推入一些輸入,然後得到它的輸出。它在fork()後面看起來像這樣,pc和cp是父子和父子管道。現在pipe,fork和non-blocking IPC
case 0:
/* Child. */
close(STDOUT_FILENO); /* Close current stdout. */
dup2(cp[1], STDOUT_FILENO);
close(STDIN_FILENO);
dup2(pc[0], STDIN_FILENO);
close(pc[1]);
close(cp[0]);
execlp("cat", "cat", NULL);
exit(1);
default:
/* Parent. */
/* Close what we don't need. */
printf("Input to child:\n");
string theinput("Hey there baby");
write(pc[1], theinput.c_str(), theinput.size());
close(pc[1]);
cout << "The input : " << theinput << endl;
printf("\nOutput from child:\n");
close(cp[1]);
while(read(cp[0], &ch, 1) == 1)
{
write(1, &ch, 1);
outcount++;
}
exit(0);
,似乎很好地工作(如果你想要的代碼:http://pastebin.com/Fh7GrxYm),但是當我在#posix在IRC聊天,他們會瘋了,怎麼這會潛在地阻止了,怎麼了「取決於內核如何感覺」。
有同樣的事情的MSDN博客文章:http://blogs.msdn.com/b/oldnewthing/archive/2011/07/07/10183884.aspx
一個如何防止堵塞,如果有的話,等?