使用 2 个管道时同步 parent 和 child
Synchronizing parent and child when using 2 pipes
这是我的代码的目的:
child 应该:
1.发送一个字符到parent
2.从parent接收整数并打印
parent应该:
1.读取child发送的字符并打印
2. 将其转换为整数并将结果发送到 child
这是我写的代码:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>
int main()
{
int fd1[2];
int fd2[2];
pid_t p;
if (pipe(fd1)==-1)
{
fprintf(stderr, "Pipe Failed" );
return 1;
}
if (pipe(fd2)==-1)
{
fprintf(stderr, "Pipe Failed" );
return 1;
}
p = fork();
if (p<0)
{
fprintf(stderr, "fork Failed" );
return 1;
}
if (p==0){
char c='a';
int received;
close(fd1[0]);
write(fd1[1], c, sizeof(char));
close(fd1[1]);
close(fd2[1]);
read(fd2[0], received, sizeof(int));
printf("Printing from child ");
printf(" ");
printf("%d", received);
close(fd2[0]);
}
if (p >0)
{
char received;
close(fd1[1]);
read(fd1[0], received, sizeof(char));
printf("Printing from parent ");
printf(" ");
printf("%c", received);
close(fd1[0]);
close(fd2[0]);
int test=(int)received;
write(fd2[1], test, sizeof(test));
close(fd2[1]);
}
}
我当前的输出如下: Printing from parent Printing from child 0
我假设 parent 在 child 写入之前从管道读取,如何解决?
I am assuming the parent is reading from the pipe before the child writes to it, how to fix that?
这个假设是错误的。这个错误是一个好的编译器应该警告过的——你错过了不是变量 c
、received
和 test
的值必须传递给 write
和read
,但他们的地址:
write(fd1[1], &c, sizeof(char));
…
read(fd2[0], &received, sizeof(int));
…
read(fd1[0], &received, sizeof(char));
…
write(fd2[1], &test, sizeof(test));
May I ask how the computer ensures the scenario I assumed doesn't happen?
来自管道的 read
,就像终端设备一样,只是阻塞直到有东西可读(前提是文件描述符没有明确设置为非阻塞)。
这是我的代码的目的:
child 应该:
1.发送一个字符到parent
2.从parent接收整数并打印
parent应该:
1.读取child发送的字符并打印
2. 将其转换为整数并将结果发送到 child
这是我写的代码:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>
int main()
{
int fd1[2];
int fd2[2];
pid_t p;
if (pipe(fd1)==-1)
{
fprintf(stderr, "Pipe Failed" );
return 1;
}
if (pipe(fd2)==-1)
{
fprintf(stderr, "Pipe Failed" );
return 1;
}
p = fork();
if (p<0)
{
fprintf(stderr, "fork Failed" );
return 1;
}
if (p==0){
char c='a';
int received;
close(fd1[0]);
write(fd1[1], c, sizeof(char));
close(fd1[1]);
close(fd2[1]);
read(fd2[0], received, sizeof(int));
printf("Printing from child ");
printf(" ");
printf("%d", received);
close(fd2[0]);
}
if (p >0)
{
char received;
close(fd1[1]);
read(fd1[0], received, sizeof(char));
printf("Printing from parent ");
printf(" ");
printf("%c", received);
close(fd1[0]);
close(fd2[0]);
int test=(int)received;
write(fd2[1], test, sizeof(test));
close(fd2[1]);
}
}
我当前的输出如下: Printing from parent Printing from child 0
我假设 parent 在 child 写入之前从管道读取,如何解决?
I am assuming the parent is reading from the pipe before the child writes to it, how to fix that?
这个假设是错误的。这个错误是一个好的编译器应该警告过的——你错过了不是变量 c
、received
和 test
的值必须传递给 write
和read
,但他们的地址:
write(fd1[1], &c, sizeof(char));
…
read(fd2[0], &received, sizeof(int));
…
read(fd1[0], &received, sizeof(char));
…
write(fd2[1], &test, sizeof(test));
May I ask how the computer ensures the scenario I assumed doesn't happen?
来自管道的 read
,就像终端设备一样,只是阻塞直到有东西可读(前提是文件描述符没有明确设置为非阻塞)。