全文预览

实验二 进程间通信

上传者:梦&殇 |  格式:doc  |  页数:5 |  大小:49KB

文档介绍
( )Р{ int fd [3];Р char OutPipe [100], Inpipe [100];Р pipe (fd);Р while (pid1=fork ( ))= = -1);Р if (pid1 = =0)Р {Р printf (“p1\n”);Р lockf (fd[1], 1,0 );Р sprintf ( OutPipe, “child 1 process is sending a message!”);Р write (fd [1], OutPipe, 50);Р sleep (1);Р lockf (fd [1], 0,0);Р exit (0);Р }Р elseР {Р while (pid2=fork ( ))= = -1);Р if (pid2 = =0)Р {Р printf (“p2\n”);Р lockf (fd[1], 1,0 );Р sprintf ( OutPipe, “child 2 process is sending a message!”);Р write (fd [1], OutPipe, 50);Р sleep (1);Р lockf (fd [1], 0,0);Р exit (0);Р }Р }Р elseР {Р printf (“parent \n”);Р wait (0);Р read (fd [0], InPipe, 50);Р printf (“%s\n”, InPipe);Р wait (0);Р read (fd [0], InPipe, 50);Р printf (“%s\n”, InPipe);Р exit (0);Р }Р }Р}Р<运行结果>Рchild 1 process is sending a message!Рchild 2 process is sending a message!

收藏

分享

举报
下载此文档