进程间通信(一)管道的pipe函数 FIFO的mkfifo函数
进程间通信
进程间通信(IPC,InterProcess Communication)是指在不同进程之间传播或交换信息。
IPC的方式通常有管道(包括无名管道和命名管道)、消息队列、信号量、共享存储、Socket、Streams等。其中 Socket和Streams支持不同主机上的两个进程IPC。
以Linux中的C语言编程为例。
一、无名管道
一、管道
管道,通常指无名管道,是 UNIX 系统IPC最古老的形式。
1、特点:
它是半双工的(即数据只能在一个方向上流动),具有固定的读端和写端。它只能用于具有亲缘关系的进程之间的通信(也是父子进程或者兄弟进程之间)。它可以看成是一种特殊的文件,对于它的读写也可以使用普通的read、write 等函数。但是它不是普通的文件,并不属于其他任何文件系统,并且只存在于内存中。
2、原型:
1.#include <unistd.h>
2.int pipe(int fd[2]); // 返回值:若成功返回0,失败返回-1
1. 父进程调用pipe函数创建管道,得到两个文件描述符fd[0]、fd[1]指向管道的读端和写端。2. 父进程调用fork创建子进程,那么子进程也有两个文件描述符指向同一管道。3. 父进程关闭管道读端,子进程关闭管道写端。父进程可以向管道中写入数据,子进程将管道中的数据读出。由于管道是利用环形队列实现的,数据从写端流入管道,从读端流出,这样就实现了进程间通信。
若要数据流从父进程流向子进程,则关闭父进程的读端(fd[0]
)与子进程的写端(fd[1]
);反之,则可以使数据流从子进程流向父进程。
无名管道:管道是半双工的,数据只能向一个方向流动;需要双方通信时,需要建立起两个管道;只能用于父子进程或者兄弟进程之间(具有亲缘关系的进程)
该函数创建的管道的两端处于一个进程中间,在实际应用中没有太大意义,因此,一个进程在由 pipe()创建管道后,一般再fork一个子进程,然后通过管道实现父子进程间的通信(因此也不难推出,只要两个进程中存在亲缘关系,这里的亲缘关系指的是具有共同的祖先,都可以采用管道方式来进行通信)。
向管道中写入数据时,linux将不保证写入的原子性,管道缓冲区一有空闲区域,写进程就会试图向管道写入数据。如果读进程不读走管道缓冲区中的数据,那么写操作将一直阻塞。
注:只有在管道的读端存在时,向管道中写入数据才有意义。否则,向管道中写入数据的进程将收到内核传来的SIFPIPE信号,应用程序可以处理该信号,也可以忽略(默认动作则是应用程序终止)。
pipetest2.c
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{int fd[2];int pid;char buf[128];
// int pipe(int pipefd[2]);if(pipe(fd) == -1){printf("creat pipe filed\n");}pid = fork();if(pid < 0){printf("creat child failed\n");}else if(pid > 0){sleep(3);printf("this is father\n");close(fd[0]);write(fd[1],"hello from father",strlen("hello from father"));wait(NULL);}else{printf("this is child\n");close(fd[1]);read(fd[0],buf,128);printf("read from father :%s\n",buf);exit(0);}return 0;
}
运行结果为:
二、有名管道
FIFO
FIFO,也称为命名管道,它是一种文件类型。
1、特点
1.FIFO可以在无关的进程之间交换数据,与无名管道不同。2.FIFO有路径名与之相关联,它以一种特殊设备文件形式存在于文件系统中。
2、原型
1 #include <sys/stat.h>
2 // 返回值:成功返回0,出错返回-1
3 int mkfifo(const char *pathname, mode_t mode);
其中的 mode 参数与open
函数中的 mode 相同。一旦创建了一个 FIFO,就可以用一般的文件I/O函数操作它。
当 open 一个FIFO时,是否设置非阻塞标志(O_NONBLOCK
)的区别:
若没有指定
O_NONBLOCK
(默认),只读 open 要阻塞到某个其他进程为写而打开此 FIFO。类似的,只写 open 要阻塞到某个其他进程为读而打开它。若指定了
O_NONBLOCK
,则只读 open 立即返回。而只写 open 将出错返回 -1 如果没有进程已经为读而打开该 FIFO,其errno置ENXIO。
mkfifotest4.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
int main()
{
// int mkfifo(const char *pathname, mode_t mode);if(mkfifo("./file",0600) == -1 && errno == EEXIST)//can read can write{printf("mkfifo failed\n");perror("why");}else{if(errno == EEXIST){printf("file have\n");}else{printf("mkfifo success\n");}}return 0;
}
~
运行结果为:
mkfifotest5.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
int main()
{
// int mkfifo(const char *pathname, mode_t mode);if(mkfifo("./file",0600) == -1 && errno != EEXIST)//can read can write{printf("mkfifo failed\n");perror("why");}return 0;
}
运行结果为:
===============================================================================================================================================
read write 配合管道的使用
usefifotest6.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
int main()
{
// int mkfifo(const char *pathname, mode_t mode);if(mkfifo("./file",0600) == -1 && errno != EEXIST)//can read can write{printf("mkfifo failed\n");perror("why");}int fd = open("./file",O_RDONLY);//only readprintf("open success\n");return 0;
}
writefifo6.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
int main()
{int fd = open("./file",O_WRONLY);//only readprintf("write open success\n");return 0;
}
运行结果为:
===================================================================================================================================================
一直读取 一直写
fiforead7.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
// int mkfifo(const char *pathname, mode_t mode);char buf[30] = {0};int nread = 0;if(mkfifo("./file",0600) == -1 && errno != EEXIST)//can read can write{printf("mkfifo failed\n");perror("why");}int fd = open("./file",O_RDONLY);//only readprintf("read open success\n");while(1){nread = read(fd,buf,30);printf("read %d byte from fifo,context: %s\n",nread,buf);}close(fd);return 0;
}
fifowrite7.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main()
{int cnt = 0;char *str = "message from fifo";int fd = open("./file",O_WRONLY);//only readprintf("write open success\n");while(1){write(fd,str,strlen(str));sleep(1);if(cnt == 5){break;}}close(fd);return 0;
}
运行结果为:
上述例子可以扩展成 客户进程—服务器进程 通信的实例,write_fifo
的作用类似于客户端,可以打开多个客户端向一个服务器发送请求信息,read_fifo
类似于服务器,它适时监控着FIFO的读端,当有数据时,读出并进行处理,但是有一个关键的问题是,每一个客户端必须预先知道服务器提供的FIFO接口,下图显示了这种安排:
进程间通信(一)管道的pipe函数 FIFO的mkfifo函数相关推荐
- linux系统编程之管道(三):命名管道FIFO和mkfifo函数
进程间通信必须通过内核提供的通道,而且必须有一种办法在进程中标识内核提供的某个通道,前面讲过的匿名管道是用打开的文件描述符来标识的.如果要互相通信的几个进程没有从公共祖先那里继承文件描述符,它们怎么通 ...
- java mkfifo_命名管道FIFO和mkfifo函数
进程间通信必须通过内核提供的通道,而且必须有一种办法在进程中标识内核提供的某个通道,前面讲过的匿名管道是用打开的文件描述符来标识的.如果要互相通信的几个进程没有从公共祖先那里继承文件描述符,它们怎么通 ...
- linux操作系统进程间通信IPC之管道pipe及FIFO
linux环境下,各进程相互独立,如果想要交换两个进程之间的数据,需要通过内核,在内存中提供一个缓存区,一个进程往缓存区中写数据,一个往缓存区读数据,内核提供的这种机制称为进程间通信(IPC),常见的 ...
- 进程间通信:管道和命名管道(FIFO)
目录 概述 IPC 对象的持续性 什么是管道 读取外部程序的输出 将输出送往 popen 传递更多的数据 如何实现 popen pipe 调用 跨越 fork 调用管道 父进程和子进程 管道关闭后的读 ...
- Linux 3.进程间通信(IPC)(pipe 无名管道、mkfifo 有名管道、ftok、msgget、msgrcv、msgsnd、msgctl 消息队列)
Linux 3.进程间通信(IPC) 进程间通信: 进程间方式: pipe 管道(无名管道) 头文件及原型 特点 pipe 示例 FIFO(有名管道) 管道文件的创建 mkfifo 头文件及原型 mk ...
- python编程函数_python编程中函数和参数你必须知道的
今天我们先聊少一点,就聊聊函数是什么和函数的参数,以及对这两个东西的思考 起源于抽象 在程序员写代码的时候,通常都会提到一个词叫抽象, 很多人都知道在面向对象中,将一个对象抽象为一个类,其实这就是我们 ...
- T-SQL里数据库工程师都不知道的秘密之SQL Server自定义函数UDF
T-SQL SQL Server UDF自定义函数概念与案例实战 函数的定义 这里的函数指的是用户自定义函数(UDF)全名为(user-defined function),以下简称为函数. 它是数据库 ...
- linux c语言内核函数手册,Linux C函数实例速查手册
函数学习目录: 第1章 初级I/O函数 1.1 close函数:关闭已经打开的文件 1.2 creat函数:创建一个文件 1.3 dup函数:复制文件描述符 1.4 dup2函数:复制文件描述符到指定 ...
- linux文件拷贝函数,linux:文件操作函数(open,close,read,write)
1. open()函数 功能描述:用于打开或创建文件,在打开或创建文件时可以指定文件的属性及用户的权限等各种参数. 所需头文件:#include ,#include ,#include (我们用的时候 ...
最新文章
- nonatomic, retain,weak,strong用法详解
- 游戏设计行业标准测试:秘密收集
- WPF入门教程系列九——布局之DockPanel与ViewBox(四)
- Mybatis-Plus升级完成! 我的系统到底更新了什么?Mybatis-Plus比Mybatis好在了哪里?
- springBoot AOP环绕增强、自定义注解、log4j2、MDC
- C语言课后习题(4)
- 两轮差速驱动机器人运动模型及应用分析
- sitemap.xml文件生成工具
- 浏览器配置异常_IE浏览器异常 修复/重置ie设置方法
- vs 编译nmake工程
- require.js的用法-阮一峰
- 桌面计算机1008桌面计算机,windows桌面精灵
- Matlab中meshgrid的用法简介
- [svn] TortoisSVN的Blam功能
- 梦幻西游手游:工坊进阶考试题目攻略—考古、乐艺篇
- php实现验证码正确输入_PHP实现验证码
- 博弈论之:薪酬激励机制
- OpManager邮件服务:使用qq邮箱发送邮件
- 科视Christie HS系列激光投影机为江苏园博园的夜晚增添亮丽风景线
- 如何成为一名优秀的软件测试人员,如何成为一名杰出的软件测试人员