Linux进程间通信——使用命名管道
- #include <sys/types.h>
- #include <sys/stat.h>
- int mkfifo(const char *filename, mode_t mode);
- int mknod(const char *filename, mode_t mode | S_IFIFO, (dev_t)0);
- open(const char *path, O_RDONLY);//1
- open(const char *path, O_RDONLY | O_NONBLOCK);//2
- open(const char *path, O_WRONLY);//3
- open(const char *path, O_WRONLY | O_NONBLOCK);//4
- #include <unistd.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <limits.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- const char *fifo_name = "/tmp/my_fifo";
- int pipe_fd = -1;
- int data_fd = -1;
- int res = 0;
- const int open_mode = O_WRONLY;
- int bytes_sent = 0;
- char buffer[PIPE_BUF + 1];
- if(access(fifo_name, F_OK) == -1)
- {
- //管道文件不存在
- //创建命名管道
- res = mkfifo(fifo_name, 0777);
- if(res != 0)
- {
- fprintf(stderr, "Could not create fifo %s\n", fifo_name);
- exit(EXIT_FAILURE);
- }
- }
- printf("Process %d opening FIFO O_WRONLY\n", getpid());
- //以只写阻塞方式打开FIFO文件,以只读方式打开数据文件
- pipe_fd = open(fifo_name, open_mode);
- data_fd = open("Data.txt", O_RDONLY);
- printf("Process %d result %d\n", getpid(), pipe_fd);
- if(pipe_fd != -1)
- {
- int bytes_read = 0;
- //向数据文件读取数据
- bytes_read = read(data_fd, buffer, PIPE_BUF);
- buffer[bytes_read] = '\0';
- while(bytes_read > 0)
- {
- //向FIFO文件写数据
- res = write(pipe_fd, buffer, bytes_read);
- if(res == -1)
- {
- fprintf(stderr, "Write error on pipe\n");
- exit(EXIT_FAILURE);
- }
- //累加写的字节数,并继续读取数据
- bytes_sent += res;
- bytes_read = read(data_fd, buffer, PIPE_BUF);
- buffer[bytes_read] = '\0';
- }
- close(pipe_fd);
- close(data_fd);
- }
- else
- exit(EXIT_FAILURE);
- printf("Process %d finished\n", getpid());
- exit(EXIT_SUCCESS);
- }
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <limits.h>
- #include <string.h>
- int main()
- {
- const char *fifo_name = "/tmp/my_fifo";
- int pipe_fd = -1;
- int data_fd = -1;
- int res = 0;
- int open_mode = O_RDONLY;
- char buffer[PIPE_BUF + 1];
- int bytes_read = 0;
- int bytes_write = 0;
- //清空缓冲数组
- memset(buffer, '\0', sizeof(buffer));
- printf("Process %d opening FIFO O_RDONLY\n", getpid());
- //以只读阻塞方式打开管道文件,注意与fifowrite.c文件中的FIFO同名
- pipe_fd = open(fifo_name, open_mode);
- //以只写方式创建保存数据的文件
- data_fd = open("DataFormFIFO.txt", O_WRONLY|O_CREAT, 0644);
- printf("Process %d result %d\n",getpid(), pipe_fd);
- if(pipe_fd != -1)
- {
- do
- {
- //读取FIFO中的数据,并把它保存在文件DataFormFIFO.txt文件中
- res = read(pipe_fd, buffer, PIPE_BUF);
- bytes_write = write(data_fd, buffer, res);
- bytes_read += res;
- }while(res > 0);
- close(pipe_fd);
- close(data_fd);
- }
- else
- exit(EXIT_FAILURE);
- printf("Process %d finished, %d bytes read\n", getpid(), bytes_read);
- exit(EXIT_SUCCESS);
- }


Linux进程间通信——使用命名管道的更多相关文章
- 《Linux 进程间通信》命名管道:FIFO
命名管道的主要用途:不相关的进程之间交换数据. 命令行上创建命名管道: $ mkfifo filename 程序中创建命名管道: #include <sys/types.h> #incl ...
- Linux进程间通信——使用匿名管道
在前面,介绍了一种进程间的通信方式:使用信号,我们创建通知事件,并通过它引起响应,但传递的信息只是一个信号值.这里将介绍另一种进程间通信的方式——匿名管道,通过它进程间可以交换更多有用的数据. 一 ...
- Linux系统编程——进程间通信:命名管道(FIFO)
命名管道的概述 无名管道,因为没有名字,仅仅能用于亲缘关系的进程间通信(很多其它详情.请看<无名管道>).为了克服这个缺点.提出了命名管道(FIFO).也叫有名管道.FIFO 文件. 命名 ...
- Linux学习记录--命名管道通信
命名管道通信 什么是命名管道 一个主要的限制是,它是匿名管道的应用还没有名字,因此,只有它可以用于进程间通信的方式与亲缘关系.在命名管道(named pipe或FIFO)提出后,该限制得到了克服.FI ...
- 练习--LINUX进程间通信之无名管道PIPE
IBM上放的这个系统不错,刚好可以系统回温一下LINUX的系统知识. http://www.ibm.com/developerworks/cn/linux/l-ipc/part1/ 感觉年纪大了,前几 ...
- 进程间通信:命名管道FIFO(2)
一.命名管道 如果我们想在不相关的进程之间交换数据,可以用FIFO文件来完成这项工作,它通常也被称为命名管道.命名管道是一种特殊类型的文件,它在文件系统中以文件名的形式存在,但是它的行为却和我们已经见 ...
- 进程间通信___命名管道(FIFO)
命名管道(FIFO) 基本概念 命名管道和一般的管道基本相同,但也有一些显著的不同: 命名管道是在文件系统中作为一个特殊的设备文件而存在的. 不同祖先的进程之间可以通过管道共享数据. 当共享管道的进程 ...
- Linux进程间通信(三):匿名管道 popen()、pclose()、pipe()、close()、dup()、dup2()
在前面,介绍了一种进程间的通信方式:使用信号,我们创建通知事件,并通过它引起响应,但传递的信息只是一个信号值.这里将介绍另一种进程间通信的方式——匿名管道,通过它进程间可以交换更多有用的数据. 一.什 ...
- Linux进程间通信(七):消息队列 msgget()、msgsend()、msgrcv()、msgctl()
下面来说说如何用不用消息队列来进行进程间的通信,消息队列与命名管道有很多相似之处.有关命名管道的更多内容可以参阅我的另一篇文章:Linux进程间通信 -- 使用命名管道 一.什么是消息队列 消息队列提 ...
随机推荐
- LeetCode_Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- Silverlight 结合ArcGis 在地图画点
原文 http://www.dotblogs.com.tw/justforgood/archive/2012/05/10/72076.aspx 我们直接来看完成后的画面读者会比较有兴趣 如上,小马在地 ...
- java设计模式--创建模式--工厂方法
工厂方法定义: 工厂方法 概述 定义一个用于创建对象的接口,让子类决定实例化哪一个类.FactoryMethod使一个类的实例化延迟到其子类. 适用性 .当一个类不知道它所必须创建的对象的类的时候. ...
- Liferay门户网站portal
转自:http://www.oschina.net/p/liferay+portal Liferay 是一个完整的门户解决方案,基于J2EE的应用,使用了EJB以及JMS等技术,前台界面部分使用Str ...
- CentOS 6.3安装Nginx 搭建文件服务器
转自:http://www.linuxidc.com/Linux/2012-09/70596.htm 1.配置CentOS 6.2 第三方yum源(CentOS默认的标准源里没有nginx软件包): ...
- 那些年的那些事CISC和RISC发展中的纠缠
本文来自http://www.cnbeta.com/articles/224544.htm ARM.ARM.ARM,没错ARM仿佛一夜之间就火了,平板.手机等领域随处可见它的影子,甚至已经有人预言未来 ...
- WebBrowserProgramming - Python Wiki
WebBrowserProgramming - Python Wiki Web Browser Programming in Python
- Beauty of Array(模拟)
M - M Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit Status P ...
- 使用公用表表达式(CTE)
本文来自:http://blog.csdn.net/songjie521/article/details/3321030 通用表表达式(CTEs)是SQL Server 2005的一项新功能.它们类似 ...
- LeetCode——Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...