我的问题很简单直接。在这里我试图在管道的一端发送数据并尝试从另一端读取数据。我正在尝试学习 IPC 机制,但在执行这个简单的程序时我被卡住了。如果我然后在父进程中使用 print()[1] ,
o/p is
In the child process
IN the parent process and its sleeping
SUBI IS IN LOVE WITH PUTHALATH
但是如果我在父进程中使用 write()[2 commented in the following program]
o/p is
In the child process
IN the parent process and its sleeping
SUBI IS IN LOVE WITH PUTHALATHIN the parent process and its sleeping
为什么“在父进程及其睡眠中”这一行被打印了两次?
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
int main(){
int fd[2];
pipe(fd);
if(!fork()){
printf("In the child process\n");
close(1);
dup(fd[1]);
close(fd[0]);
write(1,"SUBI IS IN LOVE WITH PUTHALATH", 200);
} else {
sleep(1);
printf("IN the parent process and its sleeping \n");
char* stream;
close(fd[1]);
read(fd[0],stream,200);
printf("%s",stream);------>(1)
// write(1,stream,200);---->(2)
}
return 0;
}
请提供任何帮助,因为我被困在这里。