- 论坛徽章:
- 0
|
有这样一段实验程序:
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
main()
{
int p1,p2,fd[2];
char outpipe[50];
char inpipe1[50]="Child1 is sending a message to parent!";
char inpipe2[50]="Child2 is sending a messege to parent!";
pipe(fd);
while((p1=fork())==-1);
if (p1==0)
{
lockf(fd[1],1,0);
write(fd[1],inpipe1,50);
exit(0);
}
else
{
while((p2=fork())==-1);
if (p2==0)
{
lockf(fd[1],1,0);
write(fd[1],inpipe2,50);
exit(0);
}
else
{
wait(0);
read(fd[0],outpipe,50);
lockf(fd[1],0,0);
printf("Parent has received first message:\n");
printf("%s\n",outpipe);
wait(0);
read(fd[0],outpipe,50);
lockf(fd[1],0,0);
printf("Parent has received second message:\n");
printf("%s\n",outpipe);
exit(0);
}
}
}
实验结果应该是:
parent has received first message:
child1 is sending a message to parent!
parent has received second message:
child2 is sending a message to parent!
可是我按照上面的程序编译执行后,出来下图这样结果(上面正确结果的后面二行没有显示出来),我反复找了几遍,也不知道我错在哪里,恳请各位DX指点,谢谢!
上图的那个黑方块一直在闪烁。我只有按CTRL+C强行终止。 |
|