[英]How to write/read multiple times to a pipe
我试图使两个子进程通过管道相互通信。第一个子进程必须写入数据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");
}
}
}
您的代码导致死锁 。
您以错误的方式使用read()
函数。 read(pipe_a[0],buf,sizeof(buf) +sizeof(buf) )
您期望缓冲区大小的两倍,并希望将那个字节数放入缓冲区。 因此, read
入子A等待pipe_b
,反之亦然。 因此小孩B不能写pipe_b
,因为它是waiting.Similarly,孩子不能写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");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.