Tag: 进程间

在C中使用管道用于父子IPC会使程序阻塞

我正在编写一个服务器,当fork接受套接字连接时,fork()关闭子进程。 当孩子与客户沟通时,它必须将一些信息发送回父母。 我正在使用管道来实现这一目标。 问题是当我尝试做父子IPC时,父母在读取孩子的输入时会阻塞。 这意味着,即使孩子们同时运行,他们也只能一次一个地处理,因为他们都在等待父母。 我的代码看起来像这样(为简洁起见,删除了错误检查): /* loop: wait for clients to connect */ for(;;) { pipe(write_to_parent); /* create pipe between parent and child */ pipe(write_to_child); /* create pipe between parent and child */ newsockfd = accept(…); pid = fork(); if (pid == 0) { /* child process */ close(write_to_parent[0]); close(write_to_child[1]); printf(“Connected!\n”); while ((rc = […]