当前位置: 技术问答>linux和unix
这是一个关于Linux的管道的程序,请问一下这个程序是什么意思,能不能解释一下
来源: 互联网 发布时间:2016-04-23
本文导语: 代码如下: #include #include #include int pid1,pid2; void main() { int fd[2]; char OutPipe[100],InPipe[100]; pipe(fd); while((pid1=fork()==-1)) ; if(pid2==0) { lockf(fd[1],1,0); sprintf(OutPipe,"n Child process...
代码如下:
#include
#include
#include
int pid1,pid2;
void main()
{
int fd[2];
char OutPipe[100],InPipe[100];
pipe(fd);
while((pid1=fork()==-1))
;
if(pid2==0)
{
lockf(fd[1],1,0);
sprintf(OutPipe,"n 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,"n 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("%sn",InPipe);
wait(0);
read(fd[0],InPipe,50);
printf("%sn",InPipe);
exit(0);
}
}
}
#include
#include
#include
int pid1,pid2;
void main()
{
int fd[2];
char OutPipe[100],InPipe[100];
pipe(fd);
while((pid1=fork()==-1))
;
if(pid2==0)
{
lockf(fd[1],1,0);
sprintf(OutPipe,"n 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,"n 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("%sn",InPipe);
wait(0);
read(fd[0],InPipe,50);
printf("%sn",InPipe);
exit(0);
}
}
}
|
两个进程间通过管道通信
|
建议你看看 Unix进程间通信 第二卷
csdn资源区有的下载
csdn资源区有的下载