Linux进程间通信——管道通信
目录
一、Linux进程间通信概述
1.UNIX平台进程通信方式
2.常用的进程间通信方式
二、无名管道PIPE
1.无名管道的特点:
2.无名管道的创建与关闭
创建无名管道
无名管道的读写
无名管道的读写特性
管道破裂例子
无名管道的大小
三、有名管道FIFO
1.有名管道的创建
2.有名管道进行通信例子
一、Linux进程间通信概述
1.UNIX平台进程通信方式
信进程主要局限在单个计算机内
2.常用的进程间通信方式
二、无名管道PIPE
1.无名管道的特点:
2.无名管道的创建与关闭
创建无名管道
首先来看创建函数
eg:
int main()
{
int pfd[2];
if (pipe(pfd) < 0) /*创建无名管道*/
{
printf("pipe create error\n");//利用返回值判断是否创建成功
return -1;
}
else
{
printf("pipe create success\n");
}
close(pfd[0]); /*关闭管道描述符*/
close(pfd[1]);
}
无名管道的读写
先举个例子实现子进程写入父进程读出
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{ int pfd[2] = {0};//创建无名管道int ret = pipe(pfd);if(ret < 0){perror("pipe");exit(-1);}pid_t pid = fork();//创建子进程if(pid < 0){perror("fork");exit(-1);}if(pid == 0){char buf[64] = {0};//自定义缓冲区,用于存入终端输入信息while(1){fgets(buf, 64, stdin);buf[strlen(buf)-1] = '\0'; write(pfd[1], buf, strlen(buf));//子进程执行写操作}}else{char buf1[64] = {0};while(1)//父进程进行读操作{memset(buf1, 0, 64);//清空缓冲区操作,因为自定义缓冲区可能存在上次读入的数据read(pfd[0], buf1, 64);printf("father ---- %s\n", buf1);//展示读入的数据}wait(NULL);//回收子进程资源exit(0);}return 0;
}
无名管道的读写特性
读特性:
当写端存在时:
管道有数据:返回读到的字节数
管道无数据:阻塞
当写端不存在时:
管道有数据:返回读到的数据
管道无数据:返回0;
写特性:
当读端存在时:
管道有空间:返回写入的字节数
管道无空间:阻塞,直到有空间为止
当读端不存在时:
无论管道是否有空间,管道破裂 SIGPIPE
管道破裂例子
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>int main(int argc, char *argv[])
{ int pfd[2] = {0};int ret = pipe(pfd); //创建无名管道if(ret < 0){perror("pipe");exit(-1);}close(pfd[0]); //关闭无名管道的读端pid_t pid = fork(); //创建子进程if(pid == 0){write(pfd[1], "hello", 5);sleep(5);}else{int status;wait(&status);printf("%d %d %d\n", WIFEXITED(status),WIFSIGNALED(status),WTERMSIG(status));//展示错误信息exit(0);}return 0;
}
无名管道的大小
可以以下方式计算
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{ int pfd[2] = {0};pipe(pfd);int ret = 0;int size = 0;char buf[1] = {0};while(1)//一直往管道里写数据{ret = write(pfd[1], buf, 1);//利用write返回值来计算,write会返回写入的字节数,这里我们一次就写一个0,然后循环进行累加,直到管道被塞满size = size+ret;printf("size = %d\n", size);}return 0;
}
这里注意几个要点
三、有名管道FIFO
1.有名管道的创建
因为有名管道是一个文件,所以在操作有名文件时,要先创建一个有名文件,且操作时也有open和close操作
创建例子
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{ int ret = mkfifo("fifo", 0664);//文件属性一般为0664if(ret < 0){perror("mkfifo");exit(-1);}return 0;
}
2.有名管道进行通信例子
读操作进程
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>int main(int argc, char *argv[])
{ int fd = open("fifo", O_RDONLY);if(fd < 0){perror("open");exit(-1);}printf("open fifo ok!\n");char buf[64] = {0};while(1){memset(buf, 0, 64);read(fd, buf, 64);printf("%s\n", buf);}close(fd);return 0;
}
写操作进程
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>int main(int argc, char *argv[])
{ int fd = open("fifo", O_WRONLY);if(fd < 0){perror("open");exit(-1);}printf("open fifo ok!\n");char buf[64] = {0};while(1){fgets(buf, 64, stdin);buf[strlen(buf)-1] = '\0';write(fd, buf, strlen(buf));}close(fd);return 0;
}
注:两个进程要同在运行状态,管道的读写操作才能执行,如果只有一方运行,程序会一直程阻塞状态
Linux进程间通信——管道通信相关推荐
- 【流媒体服务器Mediasoup】 NodeJs与C++信令通信详解及Linux下管道通信的详解(五)
目录 前言 匿名管道进程间通信 进程间管道 的创建与图解 MediaSoup中的管道创建 MediaSoup Channel的创建 NodeJs和 C++ 管道通信的过程 MediaSoup 消息确认 ...
- #Linux#进程间通信# 管道(pipe)-标准流管道pipe
在#Linux#进程间通信# 管道(pipe)-普通管道pipe中,我们很容易可以看出普通管道一是单工,即只能单向传输,而标准流管道针对匿名管道PIPE一系列封装.返回文件流.只不过返回的文件流无法使 ...
- linux程序间管道通信,linux进程间通信——管道 详解
管道是Linux中很重要的一种通信方式,是把一个程序的输出直接连接到另一个程序的输入.常说的管道多是指无名管道, 无名管道只能用于具有亲缘关系的进程之间,这是它与有名管道的最大区别. 有名管道叫nam ...
- Linux进程间通信——管道
转自:http://www.cnblogs.com/feisky/archive/2010/03/24/1693484.html Linux进程间通信机制: 1.同一主机进程间通信机制: Unix方式 ...
- Linux进程间通信(管道、消息队列、共享内存、信号、信号量)
目录 Linux进程间通信概述 1.管道 无名管道(pipe) 有名管道(fifo) 2.消息队列(msg) 消息队列的通信原理 消息队列相关api 消息队列收发数据 键值生成 消息队列移除 3.共享 ...
- 【Linux】管道通信
现在Linux进程间通信方式包括:无名管道(pipe)及有名管道(fifo).信号(signal).消息队列(message queue).共享内存(shared memory).信号量(semaph ...
- linux进程管道通信缺点,Linux 进程间通信(1) -- 管道
进程间通信(IPC - InterProcess Communication) 通信的方式有很多: 文件, 管道, 信号, 共享内存, 消息队列, 套接字, 命名管道等等; 但是由于设计缺陷现在常用的 ...
- Linux下进程间通信-------管道通信
先来看原理: 特点 : 1.半双工的通信方式(通信期间双方都可以发送/接收文件,但是不能双方同时发送/接收数据) 2.pipe只能用于父子进程间的通信 3.mkfifo可用于任意进程间的通信 代码逐步 ...
- linux 命名管道通信速度,《Linux 进程间通信》命名管道:FIFO
命名管道的主要用途:不相关的进程之间交换数据. 命令行上创建命名管道: $ mkfifo filename 程序中创建命名管道: #include #include int mkfifo(const ...
最新文章
- 【J2SE】学习基础
- 重大里程碑!VOLO屠榜CV任务,无需额外数据,首次在ImageNet 上达到87.1%
- [转]使用批处理设置、启动和停止服务
- c++引用专题之常引用
- SQL Server分页查询存储过程
- Java并发:隐藏线程死锁
- 关闭TCP连接的学问
- escilpe mysql,wordpress函数esc_sql()用法示例
- 高通平台printk输出log到串口
- python函数的及函数的参数的基本使用
- 新罗马字体linux,WPS文字办公—将阿拉伯数字替换为新款Times New Roman字体
- Google Guice依赖注入框架使用
- SWFUpload使用指南
- 电脑间通过串口传输数据【串口练习】
- 什么是逻辑主键和业务主键
- lm283_飞利浦LED泛光灯具 BVP283 LED户外照明灯具350W 超高功率泛光灯
- IP,域名,DNS,端口
- 虹科资讯| 虹科AR荣获汽车后市场“20佳”维修工具评委会提名奖!
- 【Mapreduce】Mapreduce实例——WordCount
- 湘潭大学计算机考研复试题,湘潭大学信息工程学院2019考研复试程序设计练习题...
热门文章
- 易查查:外贸通过这个方法找采购商邮箱百发百中
- Mysql出现问题:ERROR 1160 ( 08S01 (ER_NET_ERROR_ON_WRITE)): Got an error writing communication packe解决方案
- fragment简介
- 日本人为何离开名片就寸步难行?
- 如何查看文件的MD5值?
- getPath(),getAbsolutePath(),getCanonicalPath() 返回文件的路径
- python爬取豆瓣书籍_python爬虫学习,爬取豆瓣各分类书单
- 昝同学的Python 5.24
- 联想yoga 14s 2021标压版怎么样?测评值得买吗?详细性能点评
- EasyExcel合并列(横向合并)