Linux进程间通信——pipe应用实例
发布日期:2021-06-29 12:42:16
浏览次数:2
分类:技术文章
本文共 6485 字,大约阅读时间需要 21 分钟。
管道(pipe):管道可用于具有亲缘关系的进程间的通信,是一种半双工的方式,数据只能单向流动,允许一个进程和另一个与它有共同祖先的进程之间进行通信。
PIPE模块程序一
下面模块代码是在主函数中创将一个进程,在子进程中往管道中写数据,在父进程中读取数据,也就是一对一的读写操作。
/*============================================================================= # FileName: pipe1.c# Desc: an example of pipe communication application# Author: Licaibiao # Version: # LastChange: 2017-01-09 # History: =============================================================================*/#include程序执行结果:#include #include #include #include #include int main(int argc, char *argv[]){ pid_t pid; int fd[2]; int read_count = 0; int i; char read_buffer[10] = {0}; char write_buffer[10] = {0}; /*create pipe*/ if (pipe(fd) < 0) { printf("Create pipe failed\n"); return -1; } /*create process*/ if ((pid = fork()) < 0) { printf("Fork failed\n"); return -1; } /* child */ if (pid == 0) { printf("[child]Close read endpoint...\n"); /* close read */ close(fd[0]); for(i=0;i<10;i++) { write_buffer[i]=i; } write(fd[1],write_buffer,i); } /*father*/ else { printf("[parent]Close write endpoint...\n"); /* close write */ close(fd[1]); read_count = read(fd[0], read_buffer, 10); printf("father process read data\n"); for(i=0; i
root@ubuntu:/home/share/pipe# gcc pipe1.c -o testroot@ubuntu:/home/share/pipe# ./test [parent]Close write endpoint...[child]Close read endpoint...father process read dataread_buffer[0] = 0read_buffer[1] = 1read_buffer[2] = 2read_buffer[3] = 3read_buffer[4] = 4read_buffer[5] = 5read_buffer[6] = 6read_buffer[7] = 7read_buffer[8] = 8read_buffer[9] = 9PIPE模块程序二
下面的程序是在主进程中创建了三个子进程,在子进程中写入数据,在父进程中读出数据,这里使用的是多进程写入,因此需要对文件进程加锁,这里使用的了lockf函数。
/*============================================================================= # FileName: pipe2.c# Desc: three process write piep and father process read data# Author: Licaibiao # Version: # LastChange: 2017-01-09 # History: =============================================================================*/#include这里创建了三个进程,进程执行的顺序是不确定的,当pipe被锁定的时候,其他的请求操作这个文件的进程将被阻塞,直到pipe被解除锁定之后,其他的进程之间进行竞争决定谁先谁后执行。多次运行结果下:#include #include #include #include #include #include #include #include int main(void){ int fd[2]; int i; int pid[3]={1,1,1}; char outpipe[100],inpipe[100]; pipe(fd); /* create three process */ for(i=0;i<3;i++) { pid[i]=fork( ); if(pid[i]==0) break; } if(pid[0]==0) { lockf(fd[1],F_LOCK,0); sprintf(outpipe,"child 1 process is sending message!"); write(fd[1],outpipe,50); sleep(5); lockf(fd[1],F_ULOCK,0); exit(0); } if(pid[1]==0) { lockf(fd[1],F_LOCK,0); sprintf(outpipe,"child 2 process is sending message!"); write(fd[1],outpipe,50); sleep(5); lockf(fd[1],F_ULOCK,0); exit(0); } if(pid[2]==0) { lockf(fd[1],F_LOCK,0); sprintf(outpipe,"child 3 process is sending message!"); write(fd[1],outpipe,50); sleep(5); lockf(fd[1],F_ULOCK,0); exit(0); } wait(0); read(fd[0],inpipe,50); printf("%s\n",inpipe); read(fd[0],inpipe,50); printf("%s\n",inpipe); read(fd[0],inpipe,50); printf("%s\n",inpipe); return 0;}
root@ubuntu:/home/share/pipe# gcc pipe2.c -o testroot@ubuntu:/home/share/pipe# ./test child 3 process is sending message!child 2 process is sending message!child 1 process is sending message!root@ubuntu:/home/share/pipe# ./test child 3 process is sending message!child 2 process is sending message!child 1 process is sending message!root@ubuntu:/home/share/pipe# ./test child 3 process is sending message!child 2 process is sending message!child 1 process is sending message!root@ubuntu:/home/share/pipe# ./test child 2 process is sending message!child 3 process is sending message!child 1 process is sending message!root@ubuntu:/home/share/pipe# ./test child 3 process is sending message!child 2 process is sending message!child 1 process is sending message!root@ubuntu:/home/share/pipe# ./test child 3 process is sending message!child 2 process is sending message!child 1 process is sending message!root@ubuntu:/home/share/pipe# ./test child 3 process is sending message!child 2 process is sending message!child 1 process is sending message!root@ubuntu:/home/share/pipe# ./test child 3 process is sending message!child 2 process is sending message!child 1 process is sending message!root@ubuntu:/home/share/pipe# ./test child 1 process is sending message!child 3 process is sending message!child 2 process is sending message!root@ubuntu:/home/share/pipe#如果要固定执行先后顺序,可以把子进程一个一个的分开来创建,每创建一个子进程就请求操作pipe,最后在父进中读取就可以了。
PIPE模块程序三
下面的程序是两个进程写入,两个进程读取,在pipe中写入的数据,其他的进程再读取的时候,将读取不到数据。程序代码如下:
/*============================================================================= # FileName: pipe3.c# Desc: two process write into pipe and two process read from pipe# Author: Licaibiao # Version: # LastChange: 2017-01-09 # History: =============================================================================*/#include执行结果如下:#include #include #include #include #include #include #include #include int main(void){ int fd[2], i; int pid[3] = {1,1,1}; char outpipe[100],inpipe[100]; pipe(fd); for(i=0;i<3;i++){ pid[i]=fork( ); if(pid[i]==0) break; } if(pid[0]==0) { lockf(fd[1],F_LOCK,0); sprintf(outpipe,"child 1 process is sending message!"); write(fd[1],outpipe,50); sleep(5); lockf(fd[1],F_ULOCK,0); exit(0); } if(pid[1]==0) { lockf(fd[1],F_LOCK,0); sprintf(outpipe,"child 2 process is sending message!"); write(fd[1],outpipe,50); sleep(5); lockf(fd[1],F_ULOCK,0); exit(0); } if(pid[2]==0) { lockf(fd[0],F_LOCK,0); read(fd[0],inpipe,50); printf("Child 3 read:\n%s\n",inpipe); lockf(fd[0],F_ULOCK,0); exit(0); } wait(0); read(fd[0],inpipe,50); printf("Parent read:\n%s\n",inpipe); exit(0);}
root@ubuntu:/home/share/pipe# vim pipe3.c root@ubuntu:/home/share/pipe# gcc pipe3.c -o test root@ubuntu:/home/share/pipe# ./test Child 3 read:child 2 process is sending message!Parent read:child 1 process is sending message!root@ubuntu:/home/share/pipe# ./test Child 3 read:child 2 process is sending message!Parent read:child 1 process is sending message!root@ubuntu:/home/share/pipe# ./test Child 3 read:child 2 process is sending message!Parent read:child 1 process is sending message!root@ubuntu:/home/share/pipe# ./test Child 3 read:child 2 process is sending message!Parent read:child 1 process is sending message!
转载地址:https://caibiao-lee.blog.csdn.net/article/details/54290123 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年04月15日 18时01分22秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
树莓派WIFI设置
2019-04-29
nanopi2 启动信息
2019-04-29
phpstudy https
2019-04-29
Linux下EasyPanel版本安装及升级
2019-04-29
raspberry pi(树莓派) + easycap d60 视频采集
2019-04-29
WebRTC
2019-04-29
rfc5766-turn-server NAT
2019-04-29
webrtc详细教程
2019-04-29
Android IOS WebRTC 音视频开发总结
2019-04-29
报表图表样式
2019-04-29
android模板图例
2019-04-29
树莓派网线直连
2019-04-29
复合材料培训(I第七期)
2019-04-29
复合材料生活中的应用
2019-04-29
ABAQUS复合材料(适合小白)
2019-04-29
ABAQUS高级案例解析
2019-04-29
人工智能药物研发
2019-04-29
【超级干货+福利】AIDD最全面的学习教程
2019-04-29
最新通知:AIDD与网络药理学资料大全
2019-04-29