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进程间通信 -- 使用命名管道 一.什么是消息队列 消息队列提 ...
随机推荐
- EXW_FOB_CIF_CFR 外贸报价方式&条款之间的区别与联系
一.工厂交货价(EXW=Ex Works): 交货地点:出口国工厂或仓库: 运 输:买方负责: 保 险:买方负责: 出口手续:买方负责: 进口手续:买方负责: 风险转移:交货地: 所有权转 ...
- keil TEA
http://bbs.mydigit.cn/read.php?tid=545086 #include "reg52.h" void send_char(unsigned char ...
- 《how to design programs》12章函数复合
我们写代码时要学会适应辅助函数.作者提出了一个问题,如何对一个表排序.排序函数读取一个表,产生另一个表.排序函数的合约和用途如下: (sort empty) ;; expected value: em ...
- RabbitMQ中文入门教程
原文地址:http://adamlu.net/dev/2011/09/rabbitmq-get-started/ 这系列教程是翻译官方入门教程. 第一部分:Hello World第二部分:工作队列(W ...
- 【HDU 2586 How far away?】LCA问题 Tarjan算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵n个节点的无根树,每条边有各自的权值.给出m个查询,对于每条查询返回节点u到v的最 ...
- sroHOBOorz来自HOBO的高精类
/* bigint() bigint(long long) bigint(bigint) bigint(char*) bigint(string) +, +=, ++ -, -=, -- *, *= ...
- HDU 4508 湫湫系列故事——减肥记I(全然背包)
HDU 4508 湫湫系列故事--减肥记I(全然背包) http://acm.hdu.edu.cn/showproblem.php?pid=4508 题意: 有n种食物, 每种食物吃了能获得val[i ...
- ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法
题目链接:problemCode=1372">ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds ...
- 2台linux机器免密码互相登陆
一,如何使2台机器不需要密码互相登陆到对方呢? 这个和使用密钥的登陆Linux系统非常相似,也是将自己的公钥传到要登录的服务器上去修改权限即可. 1,A机器: 执行ssh-keygen命令,一路回车. ...
- css系列教程--简介及基础语法和注意事项
css简介:css指的是层叠样式表,cascading style sheets.用来定义html中的dom节点如何展示在页面中的问题.解决了内容与表现形式的分离问题.常见的样式表有外部链接样式表和内 ...