Select、pipe 和 waitpid - 如何等待特定的 child?
Select,pipie and waitpd - how to wait for specyfic child?
他是交易员:
我有 n fork
,在 fork 中我有 exec
,一切都与 pipe
连接。
我的问题:
如果一些 child 做 exit()
我想 close
他的 pipe
能够阅读。 - 这该怎么做? Waitpid 最有可能...
现在我等待所有 child 这样的:
for(i = 0; i< val; i++)
{
wait(&status);
close(fd[i][1]);
}
val - child 的数量。
当你 fork 时,parent 接收到 child 的 pid。
您需要将这些 pid 保存在某种数据结构中(也许是散列 table 或链表)。您还应该保持与该 pid 关联的 pipe-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()
returns 时,您将获得退出的 child 的 pid(以及可选的退出状态)。使用它来查找哪个管道与该进程一起使用,以便您关闭正确的管道。
他是交易员:
我有 n fork
,在 fork 中我有 exec
,一切都与 pipe
连接。
我的问题:
如果一些 child 做 exit()
我想 close
他的 pipe
能够阅读。 - 这该怎么做? Waitpid 最有可能...
现在我等待所有 child 这样的:
for(i = 0; i< val; i++)
{
wait(&status);
close(fd[i][1]);
}
val - child 的数量。
当你 fork 时,parent 接收到 child 的 pid。 您需要将这些 pid 保存在某种数据结构中(也许是散列 table 或链表)。您还应该保持与该 pid 关联的 pipe-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()
returns 时,您将获得退出的 child 的 pid(以及可选的退出状态)。使用它来查找哪个管道与该进程一起使用,以便您关闭正确的管道。