他是交易: 我有 n
fork
,在 fork 中我有 exec
,一切都与 pipe
连接。exit()
我想close
他的pipe
能够阅读。 - 这个怎么做?等待 很可能...
现在我等待所有这样的孩子:
for(i = 0; i< val; i++)
{
wait(&status);
close(fd[i][1]);
}
val - 孩子的数量。
当你分叉时,父进程会收到子进程的 pid。 您需要将这些 pid 保存在某种数据结构中(可能是哈希表或链表)。您还应该保留与该 pid 关联的管道 fd。所以也许是这样的数据结构:
typedef struct pidsnpipes pidsnpipes;
struct pidsnpipes {
pidsnpipes * next; /* for linked list */
pid_t childpid;
int pipefd; /* parents end of this pipe */
int status; /* if you want to remember the child's exit status */
};
pidsnpipes * childprocs = NULL;
当
wait()
返回时,您将获得退出的子进程的 pid(以及可选的退出状态)。使用它来查找该进程所属的管道,以便您关闭正确的管道。