linux进程篇 (三) 进程间的通信1 管道通信
通信方式分4大类:
管道通信:无名管道 有名管道
信号通信:发送 接收 和 处理
IPC通信:共享内存 消息队列 信号灯
socke 网络通信
用户空间 进程A <----无法通信----> 进程B
-----------------|--------------------------------------|--------------
| |
内核空间 |<-------------> 对象 <--------------->| ---------------------------------------------------------------------- //基于文件IO的思想
//open 打开或者创建一个文件,内核开辟一个buffer -->打开对象
//write 往buffer里面写
//read 从buffer读
//close 释放buffer
1. 进程间的管道通信
用户空间 进程A <----无法通信----> 进程B
-----------------|--------------------------------------|--------------
| |
内核空间 |<-------------> 管道 <--------------->| ---------------------------------------------------------------------- 管道文件时一个特殊的文件,由队列来实现
open --> pipe
管道中的东西读完了,就删除了、
管道中如果没有东西可读,就会 读堵塞
管道中如果写满了,就会写阻塞
1.1 无名管道
无名管道用于父子进程带有亲缘关系的进程
#include <unistd.h>
int pipe(int fildes[]); //创建
//文件描述符 filds[0]-read--出队 filds[1]-write--入队 write(); //写
read(); //读
close(fd[]);
close(fd[]); ------------------------------
fd[]write fd[]read
------------------------------
小例子
int main()
{
int fd[]; //pipe的2个文件描述符
int ret;
ret = pipe(fd); //创建管道
if(ret < ){
perror("pipe");
return -;
}
printf("fd[0] = %d, fd[1] = %d\n",fd[],fd[]);
return ;
}
#include <stdio.h>
#include <string.h>
#include <unistd.h> int main(int argc, char const *argv[])
{
int fd[];
int ret;
const char *writebuf = "hello pipe";
char readbuf[] = {}; //1,创建pipe
ret = pipe(fd);
if(ret < )
{
perror("pipe");
return -;
}
printf("fd[0] = %d,fd[1] = %d\n",fd[],fd[]); //2.write
ret = write(fd[],writebuf,strlen(writebuf));
if(ret < ){
perror("write");
}
//2.read
ret = read(fd[],readbuf,);
if(ret < ){
perror("read");
return -;
} printf("read: %s\n",readbuf); //3.close
close(fd[]);
close(fd[]);
return ;
}
1.2 有名管道
对于无名管道,pipe要在fork之前创建,这样fork的时候,会将fd[0]和fd[1]拷贝,这样两个进程就使用的是同2个设备描述符,如果pipe在fork之后创建,那个2个进程就会分别创建1个管道,操作的不是同一个管道文件,就没办法实行通信。
也就是说,无名管道只能用于fork创建这样的父子进程, 如果无亲缘关系的进程,无名管道没办法进行通信,就只能使用有名管道。
有名管道 即创建一个带有文件inode节点的管道文件 p, 该文件不占有内存空间,仅有一个文件节点, 当该节点被open时,才占用内存空间。
mkfifo 只是在用户空间创建了一个管道文件,并非在内存空间创建管道,只有在open时,才在内存空间创建一个管道。
注,有名管道两端成对打开时才会开始执行
#include <sys/types.h>
#include <sys/stat.h> int mkfifo(const char *path, mode_t mode); //文件路径 文件权限
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h> int main(int argc, char const *argv[])
{
int ret; //0=ok -1=failes ret = mkfifo("./fifo",); //创建管道文件 路径+权限
if(ret < ){
perror("mkfifo");
return -;
}
return ;
}
例子
fifo.c 创建有名管道文件
lude <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h> int main(int argc, char const *argv[])
{
int ret; ret = mkfifo("./fifo",); //创建管道文件 路径+权限
if(ret < ){
perror("mkfifo");
return -;
}
return ;
}
first.c 进程1
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h> int main(int argc, char const *argv[])
{
int fd;
int process_int = ; fd = open("./fifo",O_WRONLY); //注,有名管道两端成对打开时才会开始执行
if(fd < ){
perror("open");
return -;
}
puts("fifo open success."); for(int i=;i<;i++){
puts("我是第一个进程");
}
sleep();
process_int = ; write(fd,&process_int,);
while();
return ;
}
second.c 进程2
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h> int main(int argc, char const *argv[])
{
int fd;
int process_int = ; fd = open("./fifo",O_RDONLY); //注,有名管道两端成对打开时才会开始执行
if(fd < ){
perror("open");
return -;
}
puts("fifo open success."); read(fd,&process_int,);
while(process_int == ); for(int i=;i<;i++){
puts("我是第二个进程");
} while();
return ;
}
linux进程篇 (三) 进程间的通信1 管道通信的更多相关文章
- linux进程篇 (三) 进程间的通信2 信号通信
2. 信号通信 用户空间 进程A <----无法通信----> 进程B -----------------|--------------------------------------|- ...
- linux进程篇 (三) 进程间的通信3 IPC通信
3 IPC通信 用户空间 进程A <----无法通信----> 进程B -----------------|--------------------------------------|- ...
- c# c++通信--命名管道通信
进程间通信有很多种,windows上面比较简单的有管道通信(匿名管道及命名管道) 最近做个本机c#界面与c++服务进行通信的一个需求.简单用命名管道通信.msdn都直接有demo,详见下方参考. c+ ...
- linux进程篇 (二) 进程的基本控制
2. 进程的基本操作 接口函数 #include <unistd.h> //创建子进程 pid_t fork(void); //结束子进程 void exit(int status); / ...
- linux进程篇 (一) 进程的基本概念
进程是系统资源分配的最小单位. 1.创建和执行 父进程通过 fork 系统调用创建子进程, 子进程被创建后,处于创建状态. linux为子进程配置数据结构,如果内存空间足够,子进程就在内核中就绪,成为 ...
- linux线程篇 (三) 线程的同步
1 互斥量 pthreat_mutex_t mymutex; //1. 创建 初始化 int pthread_mutex_init(pthread_mutex_t *mutex, const pthr ...
- Linux基础篇三:文件系统
/bin 实际上是 /usr/bin /sbin 实际上是 /usr/sbin /usr/bin 里面的命令其实是依赖 /lib64 或者 /lib32 ldd /us ...
- 管道通信,王明学learn
管道通信 一.通讯目的 1.数据传输 一个进程需要将数据发送给另一个进程. 2.资源共享 多个进程之间共享同样的资源. 3.通知事件 一个进程需要向另一个/组进程发送消息,通知它们发生了某事件. 4. ...
- C#命名管道通信
C#命名管道通信 最近项目中要用c#进程间通信,以前常见的方法包括RMI.发消息等.但在Windows下面发消息需要有窗口,我们的程序是一个后台运行程序,发消息不试用.RMI又用的太多了,准备用管道通 ...
随机推荐
- 定制UITabBar显示样式
定制UITabBar显示样式 思路是这样子的: 1. 初始化UITabBarController,并装载进来几个其他的ViewController 2. 获取每个控制器的UITabBarItem 3. ...
- vsphere的P2V工具做的物理机迁移到虚拟机报错out of memory
vsphere的P2V工具做的物理机迁移到虚拟机 迁移成功,但是启动报错 进入rescue模式后发现是sysctl.conf文件的参数设大了因为虚拟机的内存没有物理机内存大 kernel.shm ...
- ZT Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性
Android布局]在程序中设置android:gravity 和 android:layout_Gravity属性 分类: [Android基础] 2011-04-19 16:06 54739人阅读 ...
- 邮件营销巧妙添加GIF让您的邮件动起来
动态图片远比静态图片要吸引人,因此近年来,一些营销人员也开始越来越频繁的使用GIF动画图片,适当的穿插和点缀动态图片,能够生动形象的表达出 主题,并且时不时令读者忍俊不禁.尤其是做邮件营销的,如果能在 ...
- 019sys模块
为了和python解释器交互,控制台执行脚本后面添加变量 import sysprint(sys.argv) def post(): print('upload')def download ...
- IOS 上传下载
下载地址:https://github.com/samsoffes/ssziparchive 注意:需要引入libz.dylib框架 // Unzipping NSString *zipPath = ...
- (一)安装Linux时的磁盘划分
Linux安装中的磁盘划分 安装Ctentos6.3的版本,它使用的默认文件系统类型是ext4. Linux的安装至少要划分为根分区/和swap分区这个两个分区才能正常安装使用. 一般来说应该分为四个 ...
- bzoj3718 [PA2014]Parking
Description 你的老板命令你将停车场里的车移动成他想要的样子.停车场是一个长条矩形,宽度为w.我们以其左下角顶点为原点,坐标轴平行于矩形的边,建立直角坐标系.停车场很长,我们可以认为它一直向 ...
- virtualbox 安装 mac os x lion 10.7实现全屏显示!
1. 启动Virtual Box虚拟机,在虚拟机里编辑 /Library/Preferences/SystemConfiguration/com.apple.Boot.plist,找到 <dic ...
- SSH 与 SSL
关于 ssh 有人已经总结得非常好了,这里推荐大家看下 阮一峰 写的 ssh原理与应用 写得简单易懂,非常赞. 关于 ssl 这里有一篇博文写得也不错,ssl协议详解 好了,那 ssh 和 ssl ...