选择、管道和waitpid - 如何等待特定的子进程? [已关闭]

问题描述 投票:0回答:1

他是交易: 我有 n

fork
,在 fork 中我有
exec
,一切都与
pipe
连接。
我的问题:
如果某个孩子
exit()
我想
close
他的
pipe
能够阅读。 - 这个怎么做?等待 很可能...

现在我等待所有这样的孩子:

for(i = 0; i< val; i++)
        {
                wait(&status);
                close(fd[i][1]);
        }

val - 孩子的数量。

c linux pipe waitpid
1个回答
1
投票

当你分叉时,父进程会收到子进程的 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(以及可选的退出状态)。使用它来查找该进程所属的管道,以便您关闭正确的管道。

© www.soinside.com 2019 - 2024. All rights reserved.