|
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
int pid1,pid2;
main() {
int fd[2];
char OutPipe[100],InPipe[100];
pipe(fd);
while((pid1 = fork()) == -1);
if(pid1 == 0) {
lockf(fd[1],1,0);
sprintf(OutPipe,"Child process 1 is sending message!\n");
write(fd[1],OutPipe,50);
sleep(5);
lockf(fd[1],0,0);
exit(0);
}
else {
while((pid2 = fork()) == -1);
if(pid2 == 0) {
lockf(fd[1],1,0);
sprintf(OutPipe,"Child process 2 is sending message!\n");
write(fd[1],OutPipe,50);
sleep(5);
lockf(fd[1],0,0);
exit(0);
}
else {
wait(0);
read(fd[0],InPipe,50);
printf("%s\n",InPipe);
wait(0);
read(fd[0],InPipe,50);
printf("%s\n",InPipe);
exit(0);
}
}
}
这段代码的输出是这样的
here!here!Child process 1 is sending message!
Child process 2 is sending message!
为什么 是这样的阿
流程是什么样的 ?
为什么有两个here?
谢谢回帖的大哥们 |
|