Linux进程间通信——pipe应用实例
管道(pipe):管道可用于具有亲缘关系的进程间的通信,是一种半双工的方式,数据只能单向流动,允许一个进程和另一个与它有共同祖先的进程之间进行通信。
PIPE模块程序一
下面模块代码是在主函数中创将一个进程,在子进程中往管道中写数据,在父进程中读取数据,也就是一对一的读写操作。
/*=============================================================================
# FileName: pipe1.c
# Desc: an example of pipe communication application
# Author: Licaibiao
# Version:
# LastChange: 2017-01-09
# History: =============================================================================*/
#include <sys/wait.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
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<read_count; i++){printf("read_buffer[%d] = %d\n",i,read_buffer[i]);}}
}
程序执行结果:
root@ubuntu:/home/share/pipe# gcc pipe1.c -o test
root@ubuntu:/home/share/pipe# ./test
[parent]Close write endpoint...
[child]Close read endpoint...
father process read data
read_buffer[0] = 0
read_buffer[1] = 1
read_buffer[2] = 2
read_buffer[3] = 3
read_buffer[4] = 4
read_buffer[5] = 5
read_buffer[6] = 6
read_buffer[7] = 7
read_buffer[8] = 8
read_buffer[9] = 9
PIPE模块程序二
下面的程序是在主进程中创建了三个子进程,在子进程中写入数据,在父进程中读出数据,这里使用的是多进程写入,因此需要对文件进程加锁,这里使用的了lockf函数。
/*=============================================================================
# FileName: pipe2.c
# Desc: three process write piep and father process read data
# Author: Licaibiao
# Version:
# LastChange: 2017-01-09
# History: =============================================================================*/
#include <sys/wait.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
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;
}
这里创建了三个进程,进程执行的顺序是不确定的,当pipe被锁定的时候,其他的请求操作这个文件的进程将被阻塞,直到pipe被解除锁定之后,其他的进程之间进行竞争决定谁先谁后执行。多次运行结果下:
root@ubuntu:/home/share/pipe# gcc pipe2.c -o test
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 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 <sys/wait.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
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!
由于各种原因,后续文章内容将更新到公众号,本平台将不再做更新。
CSDN上相关文章的测试工程代码,也统一放到了公众号上,可以免费免积分下载
可以通过主页上的二维码,也可以通过搜索微信公众号 liwen01 进入公众号
liwen01 2022.08.21
Linux进程间通信——pipe应用实例相关推荐
- linux 进程间通信 dbus-glib【实例】详解四(上) C库 dbus-glib 使用(附代码)(编写接口描述文件.xml,dbus-binding-tool工具生成绑定文件)(列集散集函数)
linux 进程间通信 dbus-glib[实例]详解一(附代码)(d-feet工具使用) linux 进程间通信 dbus-glib[实例]详解二(上) 消息和消息总线(附代码) linux 进程间 ...
- linux 进程间通信 dbus-glib【实例】详解三 数据类型和dteeth(类型签名type域)(层级结构:服务Service --> Node(对象、object) 等 )(附代码)
linux 进程间通信 dbus-glib[实例]详解一(附代码)(d-feet工具使用) linux 进程间通信 dbus-glib[实例]详解二(上) 消息和消息总线(附代码) linux 进程间 ...
- linux 进程间通信 dbus-glib【实例】详解二(下) 消息和消息总线(ListActivatableNames和服务器的自动启动)(附代码)
linux 进程间通信 dbus-glib[实例]详解一(附代码)(d-feet工具使用) linux 进程间通信 dbus-glib[实例]详解二(上) 消息和消息总线(附代码) linux 进程间 ...
- linux 进程间通信 dbus-glib【实例】详解二(上) 消息和消息总线(附代码)
linux 进程间通信 dbus-glib[实例]详解一(附代码)(d-feet工具使用) linux 进程间通信 dbus-glib[实例]详解二(上) 消息和消息总线(附代码) linux 进程间 ...
- linux 进程间通信 dbus-glib【实例】详解一(附代码)(d-feet工具使用)
linux 进程间通信 dbus-glib[实例]详解一(附代码)(d-feet工具使用) linux 进程间通信 dbus-glib[实例]详解二(上) 消息和消息总线(附代码) linux 进程间 ...
- linux进程间通信:无名管道 pipe
文章目录 内核层实现 结构 通信原理 特点 使用 函数声明 使用实例 单向通信 双向通信 编程注意事项 管道中无数据时读操作会阻塞 将管道的写端句柄关闭,不会影响读端数据读取 管道中没有数据,写操作关 ...
- #Linux#进程间通信# 管道(pipe)-标准流管道pipe
在#Linux#进程间通信# 管道(pipe)-普通管道pipe中,我们很容易可以看出普通管道一是单工,即只能单向传输,而标准流管道针对匿名管道PIPE一系列封装.返回文件流.只不过返回的文件流无法使 ...
- Linux进程间通信——管道
转自:http://www.cnblogs.com/feisky/archive/2010/03/24/1693484.html Linux进程间通信机制: 1.同一主机进程间通信机制: Unix方式 ...
- Linux进程间通信[转]
一.进程间通信概述 进程通信有如下一些目的: A.数据传输:一个进程需要将它的数据发送给另一个进程,发送的数据量在一个字节到几M字节之间 B.共享数据:多个进程想要操作共享数据,一个进程对共享数据的修 ...
最新文章
- 用R语言做词频统计_R语言 | 词频统计
- 工作量证明的最长链原则到底说的是什么?
- python学习笔记(七)——类基础
- 一次SSH爆破攻击haiduc工具的应急响应
- win10任务管理器快捷键_win10系统任务管理器怎么打开
- iOS逆向之深入解析如何计算+load方法的耗时
- dotnetcore3.1 WPF 实现多语言
- 机器学习数学基础之极限
- 程序员的奋斗史(三十二)——人在囧途之应聘篇(二)
- Network: 光纤猫下无线路由器的设置
- 《结构思考力》- 书摘整理
- 六款最佳、免费的网络延迟测试工具
- [NACOS HTTP-POST] The maximum number of tolerable server reconnection errors has been reached
- C#--使用Process类kill进程
- iOS粘性拖拽红点动画研究
- 服务器无限刷钱bug,魔兽世界怀旧服出现“重大”刷金币BUG,分线的负面影响来的太快...
- 专访沃顿商学院教授张忠:“向上抑或向下”,中美数字化转型中碰撞出的哲学命题...
- 神武2服务器多少级出拍卖系统,快捷购买物品和角色 神武2拍卖系统详解
- Delphi多线程处理
- 开机一键ghost重装系统如何操作