|
发表于 2005-6-26 00:10:36
|
显示全部楼层
建议看看《unix进程间通讯》这本书
[code:1]
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
void currtime()
{
printf(".........diy........\n");
}
main()
{
int p1[2],p2[2],i;
char buf[7];
if (pipe(p1)==-1 || pipe(p2)==-1)
{
exit(1);
}
switch(fork())
{
case -1:
exit(1);
case 0://子进城
close(p1[1]);
close(p2[0]);
for(i=0;i<5;i++)
{
read(p1[0],buf,6);//等待
currtime();
sleep(1);
currtime();
sleep(1);
currtime();
sleep(1);
write(p2[1],"aaaaaa",6);//往管道里写点东西让父进程进行下去
}
close(p1[0]);
close(p2[1]);
break;
default://父进城
close(p1[0]);
close(p2[1]);
for(i=0;i<5;i++)
{
printf("current time\n");
write(p1[1],"bbbbbb",6);//往管道里写点东西让子进程进行下去
read(p2[0],buf,6);//等待
}
close(p1[1]);
close(p2[0]);
break;
}
}
[/code:1] |
|