我正在尝试让两个子进程通过管道相互通信。第一个孩子必须写入数据1和数据2,然后第二个孩子写入数据3和数据4.然后孩子们互相读取数据并打印出来。这是我到目前为止的代码。它只显示儿童发送给彼此的第一封消息而不是它挂起。
#include <stdio.h>
#include <sys/types.h>
int main(void) {
pid_t child_a, child_b;
int pipe_a[2],pipe_b[2];
char mesazhi1[] = "First message";
char mesazhi2[] = "Second message";
char buf[50];
int first_pipe = pipe(pipe_a);
int second_pipe = pipe(pipe_b);
if(first_pipe == -1 || second_pipe == -1 ){
perror("pipe");
exit(1);
}
child_a = fork();
if (child_a == 0) {
/* Child A code */
printf("%s\n","the first child is writing to pipe a" );
write(pipe_a[1],mesazhi1, sizeof(mesazhi1));
write(pipe_a[1],mesazhi2,sizeof(mesazhi2));
while( read(pipe_b[0],buf,sizeof(buf) + sizeof(buf) ) > 0 ){
printf("Reading from buffer for child 1 gives: %s \n",buf);
}
} else {
child_b = fork();
if (child_b == 0) {
/* Child B code */
printf("%s\n","the second child is writing to pipe b" );
write(pipe_b[1],mesazhi2,sizeof(mesazhi2));
while( read(pipe_a[0],buf,sizeof(buf) +sizeof(buf) ) > 0 ){
printf("Reading from buffer for child 2 gives: %s \n",buf);
}
write(pipe_b[1],mesazhi1,sizeof(mesazhi1));
printf("%s\n","the second child reads data from pipe a" );
} else {
/* Parent Code */
int returnStatusA,returnStatusB;
waitpid(child_a, &returnStatusA, 0); // Parent process waits here for child to terminate.
waitpid(child_b, &returnStatusB, 0); // Parent process waits here for child to terminate.
if (returnStatusA == 0 && returnStatusB == 0) // Verify child process terminated without error.
{
printf("%s\n", "The child processes terminated normally.\n");
}
if (returnStatusA == 1 && returnStatusB == 1)
{
printf("%s\n", "The child processes terminated with an error!. \n" );
}
printf("%s\n","The parent terminates two childs");
}
}
}
答案 0 :(得分:3)
您的代码会导致死锁。
您以错误的方式使用read()
功能。 read(pipe_a[0],buf,sizeof(buf) +sizeof(buf) )
您期望缓冲区大小的两倍,并希望将这些字节数放入缓冲区。因此,孩子A中的read
等待pipe_b
,反之亦然。因此,孩子B无法写入pipe_b
,因为它正在等待。同样,孩子A无法写入pipe_a
,因为它正在等待。
此外,您的代码与您在问题中解释的方案不同。在孩子B中,你在阅读后写作。
最后使用strlen()
来计算字符串的长度,而不是sizeof
。
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
pid_t child_a, child_b;
int pipe_a[2],pipe_b[2];
char mesazhi1[] = "First message";
char mesazhi2[] = "Second message";
char buf[50];
int first_pipe = pipe(pipe_a);
int second_pipe = pipe(pipe_b);
if(first_pipe == -1 || second_pipe == -1 ){
perror("pipe");
exit(1);
}
child_a = fork();
if (child_a == 0) {
/* Child A code */
printf("%s\n","the first child is writing to pipe a" );
write(pipe_a[1],mesazhi1, strlen(mesazhi1) + 1);
write(pipe_a[1],mesazhi2, strlen(mesazhi2) + 1);
read(pipe_b[0], buf, strlen(mesazhi1) + 1);
printf("Reading from buffer for child 1 gives: %s \n",buf);
read(pipe_b[0], buf, strlen(mesazhi2) + 1);
printf("Reading from buffer for child 1 gives: %s \n",buf);
} else {
child_b = fork();
if (child_b == 0) {
/* Child B code */
printf("%s\n","the second child is writing to pipe b" );
read(pipe_a[0],buf, strlen(mesazhi1) + 1);
printf("Reading from buffer for child 2 gives: %s \n",buf);
read(pipe_a[0],buf, strlen(mesazhi2) + 1);
printf("Reading from buffer for child 2 gives: %s \n",buf);
write(pipe_b[1],mesazhi1, strlen(mesazhi1) + 1);
write(pipe_b[1],mesazhi2, strlen(mesazhi2) + 1);
printf("%s\n","the second child reads data from pipe a" );
} else {
/* Parent Code */
int returnStatusA,returnStatusB;
waitpid(child_a, &returnStatusA, 0); // Parent process waits here for child to terminate.
waitpid(child_b, &returnStatusB, 0); // Parent process waits here for child to terminate.
if (returnStatusA == 0 && returnStatusB == 0) // Verify child process terminated without error.
{
printf("%s\n", "The child processes terminated normally.\n");
}
if (returnStatusA == 1 && returnStatusB == 1)
{
printf("%s\n", "The child processes terminated with an error!. \n" );
}
printf("%s\n","The parent terminates two childs");
}
}
}