使用管道同步兄弟进程
Synchronizing sibling processes using Pipes
我正在研究使用管道的 IPC。 parent 进程创建 'n' 个 child 进程并等待所有 children 进程终止。我希望第一个 child 在其所有同级进程终止时得到通知。我正在利用 read()
块直到它的所有 WRITE 结束都关闭的事实。所以,这是兄弟姐妹 close()
WRITE 在完成他们的工作后结束。
我的代码中的问题是,第一个 child 中的 read()
根本没有解锁,第一个 child 没有终止,因此 parent继续等待。
我做错了什么?
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
int fd[2]; // 0 = READ end, 1 = WRITE end
int ret = pipe(fd);
pid_t wait_pid;
int status = 0;
int n = 4;
for(volatile int i = 0;i < n;++i) {
ret = fork();
if(ret == -1) {
fprintf(stderr, "fork failed\n");
exit(1);
}
switch(ret) {
// child
case 0: {
fprintf(stderr, "Child created : %d\n", getpid());
if(i!=0) {
close(fd[0]); // close unused READ end
foo(); // do some work
close(fd[1]); // close WRITE end, the last child
// to close will cause the read()
// of first child to unblock
}
if(i==0) { // first child
close(fd[1]); // close unused WRITE end
foo(); // do some work
char c = 0;
fprintf(stderr, "1st Child's wait started %d\n",
getpid());
read(fd[0], &c, 1); // blocking call, until all
// siblings close the WRITE
// end
fprintf(stderr, "1st Child's wait over %d\n",
getpid());
close(fd[0]); // close READ end
}
fprintf(stderr, "Child %d terminating\n", getpid());
exit(0);
break;
}
}
}
// Parent waits for all childdren to finish
while ((wait_pid = wait(&status)) > 0);
fprintf(stderr, "Parent's wait over, now terminating...\n");
return 0;
}
您的技术存在问题,父级本身也有 pipe
创建的文件描述符的副本。
在 fork
循环完成后关闭描述符。
我正在研究使用管道的 IPC。 parent 进程创建 'n' 个 child 进程并等待所有 children 进程终止。我希望第一个 child 在其所有同级进程终止时得到通知。我正在利用 read()
块直到它的所有 WRITE 结束都关闭的事实。所以,这是兄弟姐妹 close()
WRITE 在完成他们的工作后结束。
我的代码中的问题是,第一个 child 中的 read()
根本没有解锁,第一个 child 没有终止,因此 parent继续等待。
我做错了什么?
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
int fd[2]; // 0 = READ end, 1 = WRITE end
int ret = pipe(fd);
pid_t wait_pid;
int status = 0;
int n = 4;
for(volatile int i = 0;i < n;++i) {
ret = fork();
if(ret == -1) {
fprintf(stderr, "fork failed\n");
exit(1);
}
switch(ret) {
// child
case 0: {
fprintf(stderr, "Child created : %d\n", getpid());
if(i!=0) {
close(fd[0]); // close unused READ end
foo(); // do some work
close(fd[1]); // close WRITE end, the last child
// to close will cause the read()
// of first child to unblock
}
if(i==0) { // first child
close(fd[1]); // close unused WRITE end
foo(); // do some work
char c = 0;
fprintf(stderr, "1st Child's wait started %d\n",
getpid());
read(fd[0], &c, 1); // blocking call, until all
// siblings close the WRITE
// end
fprintf(stderr, "1st Child's wait over %d\n",
getpid());
close(fd[0]); // close READ end
}
fprintf(stderr, "Child %d terminating\n", getpid());
exit(0);
break;
}
}
}
// Parent waits for all childdren to finish
while ((wait_pid = wait(&status)) > 0);
fprintf(stderr, "Parent's wait over, now terminating...\n");
return 0;
}
您的技术存在问题,父级本身也有 pipe
创建的文件描述符的副本。
在 fork
循环完成后关闭描述符。