【问题标题】:Select,pipie and waitpd - how to wait for specyfic child? [closed]选择、管道和waitpid - 如何等待特定的孩子? [关闭]
【发布时间】:2016-01-06 14:25:54
【问题描述】:

他是交易: 我有 n fork,在 fork 我有 exec,一切都与 pipe 连接。
我的问题:
如果某个孩子做exit() 我想close 他的pipe 能够阅读。 - 这该怎么做? Waitpid 最有可能...

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

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

val - 孩子的数量。

【问题讨论】:

  • 不太清楚你在问什么。如果您想在子(ren)退出时关闭通过管道建立的某些连接,那么 1)忽略 SIGPIPE(这样您的父/服务器不会退出 - SIGPIPE 的默认操作是退出) 2)查找 EPIPE 错误号。 3) 关闭返回EPIPE 的文件描述符。除了收获目的,您不需要为此目的使用 wait()。

标签: c linux select pipe waitpid


【解决方案1】:

当你 fork 时,父进程会收到子进程的 pid。 您需要将这些 pid 保存在某种数据结构中(可能是哈希表或链表)。您还应该保持与该 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() 返回时,您将获得退出的孩子的 pid(以及可选的退出状态)。使用它来查找与该进程一起使用的管道,以便关闭正确的管道。

【讨论】:

    猜你喜欢
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    相关资源
    最近更新 更多