【发布时间】:2020-05-22 13:22:06
【问题描述】:
我需要使用管道在父级和分叉子级之间创建通信。父进程发送0,子进程返回1,父进程回复2,以此类推,直到计数器达到5。然后两个进程都将终止。
我尝试使用两个管道来实现这一点:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
int fd[2];
int fd2[2];
int val = 0;
// create pipe descriptors
pipe(fd);
pipe(fd2);
// fork() returns 0 for child process, child-pid for parent process.
if (fork() != 0) {
///0. Send: 0
// parent: writing only, so close read-descriptor.
close(fd[0]);
// send the value on the write-descriptor.
val = 0;
resend:
write(fd[1], &val, sizeof(val));
printf("0.Parent(%d) send value: %d\n", getpid(), val);
// close the write descriptor
close(fd[1]);
///3. Receive 1;
close(fd2[1]);
// now read the data
read(fd2[0], &val, sizeof(val));
printf("3.Parent(%d) received value: %d\n", getpid(), val);
// close the read-descriptor
close(fd2[0]);
wait(NULL);
val++;
if (val < 6)
goto resend;
else
return 0;
} else { // child: reading only, so close the write-descriptor
childStart:
wait(NULL);
///1. receive: 0
close(fd[1]);
// now read the data (will block)
read(fd[0], &val, sizeof(val));
printf("1.Child(%d) received value: %d\n", getpid(), val);
// close the read-descriptor
//close(fd[0]);
///2. Send: 1
// parent: writing only, so close read-descriptor.
close(fd2[0]);
// send the value on the write-descriptor.
val = val + 1;
write(fd2[1], &val, sizeof(val));
printf("2.Child(%d) send value: %d\n", getpid(), val);
// close the write descriptor
close(fd2[1]);
if(val <5) {
wait(NULL);
goto childStart;
}
else
return 0;
}
///////////////////////////////////////////////////////////////////////////////////
return 0 ;
}
我得到以下输出:
0.Parent(120978) send value: 0
1.Child(120979) received value: 0
2.Child(120979) send value: 1
1.Child(120979) received value: 1
2.Child(120979) send value: 2
1.Child(120979) received value: 2
2.Child(120979) send value: 3
1.Child(120979) received value: 3
2.Child(120979) send value: 4
1.Child(120979) received value: 4
2.Child(120979) send value: 5
3.Parent(120978) received value: 1
0.Parent(120978) send value: 2
3.Parent(120978) received value: 2
0.Parent(120978) send value: 3
3.Parent(120978) received value: 3
0.Parent(120978) send value: 4
3.Parent(120978) received value: 4
0.Parent(120978) send value: 5
3.Parent(120978) received value: 5
如何让进程等待对方的消息?
【问题讨论】:
-
resend: if (val < 6) goto resend;只需使用while循环...您先使用close(fd[1]);然后goto resend并重复使用write(fd[1],。好吧,一旦你关闭了fd[1],你以后就不能再使用它了。 -
Re "如何让进程等待对方的消息?" 什么意思?您清楚地看到父母在发送下一个号码之前等待回复。孩子也一样
-
您正在关闭循环中的文件描述符,这会破坏一切;而且您没有检查读/写的退出代码,因此在关闭文件描述符后,每次读/写都是非阻塞的并返回失败,这意味着变量不会被发送或覆盖。
-
你为什么叫
wait在孩子?
标签: c linux pipe interprocess