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又用的太多了,准备用管道通 ...
随机推荐
- NET(C#):使用HttpWebRequest头中的Range下载文件片段
转自:http://www.mgenware.com/blog/?p=220 HTTP请求包头信息中有一个Range属性可以指定索取部分HTTP请求的文件.在.NET中则通过HttpWebReques ...
- 实验5&期中考试后两题
实验内容1: #include <iostream> #include <vector> #include <string> using namespace std ...
- .NET Core学习之路
1.NET Core环境搭建 安装.NET Core: .NET Core 包括.NET Core Runtime 和 .NET Core SDK: NET Core = 应用运行依赖的 .NET C ...
- html5 css3新特性了解一下
html5: 用于绘画的 canvas 元素 以及SVG 用于媒介回放的 video 和 audio 元素 拖拽(Drag 和 drop) 地理定位(Geolocation) 对本地离线存储的更好的支 ...
- node express 跨域问题
express = require('express'); var app = express(); //设置跨域访问 app.all('*', function(req, res, next) { ...
- luogu P3801 红色的幻想乡
嘟嘟嘟 首先人人都能想到是线段树,不过二维线段树肯定会MLE+TLE的. 我们换一种想法,不去修改整个区间,而是修改一个点:开横竖两个线段树,分别记录哪些行和列被修改了.因为如果两阵红雾碰撞,则会因为 ...
- vs使用libevent
1.下载最新libevent-2.1.8-stable,并解压 2.使用vs2013 工具这里使用x64,这里更新一下,改为使用x86 进入到libevent目录 运行 nmake /f Makefi ...
- 【转】XZip and XUnzip - Add zip and/or unzip to your app with no extra .lib or .dll
原文:http://www.codeproject.com/Articles/4135/XZip-and-XUnzip-Add-zip-and-or-unzip-to-your-app-w Downl ...
- 【xampp】windows下XAMPP集成环境中,MySQL数据库的使用
在已经安装了XAMPP之后,会在你安装的目录下面出现”XAMPP“文件夹,这个文件夹就是整个XAMPP集成环境的目录. 我们先进入这个目录,然后会看到带有XAMPP标志的xampp-control.e ...
- Hibernate之openSession与getCurrentSession的区别
openSession 与 getCurrentSession的区别(1)openSession 每一次获得的是一个全新的session对象,而getCurrentSession获得的是与当前线程绑定 ...