管道和FIFO 一
管道和FIFO
- 管道(pipe)
- #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);
- }


管道和FIFO 一的更多相关文章
- 第四章:管道与FIFO
4.1:概述 管道是最初的Unix IPC形式,可追溯到1973年的Unix第三版.尽管对于许多操作来说很有用,但它们的根本局限在于没有名字,从而只能由亲缘关系的进程使用.这一点随FIFO的加入得改正 ...
- linux进程间通信-有名管道(FIFO)
有名管道(FIFO) 命名管道也被称为FIFO文件,是一种特殊的文件.由于linux所有的事物都可以被视为文件,所以对命名管道的使用也就变得与文件操作非常统一. (1)创建命名管道 用如下两个函数中的 ...
- 第4章 管道和FIFO
4.1 管道 管道是由pipe函数创建的,提供一个单向数据流. 头文件 #include <unistd.h> 函数原型 int pipe(int fd[2]); 返回值 成功则为0,出错 ...
- Linux系统编程——进程间通信:命名管道(FIFO)
命名管道的概述 无名管道,因为没有名字,仅仅能用于亲缘关系的进程间通信(很多其它详情.请看<无名管道>).为了克服这个缺点.提出了命名管道(FIFO).也叫有名管道.FIFO 文件. 命名 ...
- 第4章 管道与FIFO
4.1 概述 管道只在亲缘进程间使用,FIFO在任意进程间使用 4.2 管道 #include <unistd.h> ]) fd[0]用来读管道,fd[1]用来写管道 1)命令who | ...
- [转] IPC之管道、FIFO、socketpair
管道和FIFO作为最初的UNIX IPC形式,现在已用得较少.SocketPair可作为全双工版本的管道,较为常用,这里简单做个笔记 管道 * 只用于有亲缘关系的进程间通信 * 单向,即半双工 (双向 ...
- linux 有名管道(FIFO)
http://blog.csdn.net/firefoxbug/article/details/8137762 linux 有名管道(FIFO) 管道的缓冲区是有限的(管道制存在于内存中,在管道创建时 ...
- UNIX环境高级编程——管道和FIFO限制
系统加于管道和FIFO的唯一限制为: OPEN_MAX 一个进程在任意时刻打开的最大描述符数: PIPE_BUF 可原子的写往一个管道或FIFO的最大数据量. OPEN_MAX的值 ...
- UNIX环境高级编程——管道和FIFO的额外属性
一个描述符能以两种方式设置成非阻塞. (1)调用open时可以指定O_NONBLOCK标志. writefd = open(FIFO1,O_WRONLY | O_NONBLOCK,0); (2)如果一 ...
随机推荐
- 微信小程序中的bindTap事件(微信小程序开发QQ群:604788754)
bindTap对应的绑定事件, 第一个:wx.navigateTo wx.navigateTo({ url:"../content/content" }) 第二个:wx.redir ...
- 快速切题 sgu123. The sum
123. The sum time limit per test: 0.25 sec. memory limit per test: 4096 KB The Fibonacci sequence of ...
- L1-022 奇偶分家
给定N个正整数,请统计奇数和偶数各有多少个? 输入格式: 输入第一行给出一个正整N(≤1000):第2行给出N个正整数,以空格分隔. 输出格式: 在一行中先后输出奇数的个数.偶数的个数.中间以1个空格 ...
- DevExpress v17.2新版亮点——CodeRush篇(二)
用户界面套包DevExpress v17.2日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了CodeRush v17.2 的新功能,快来下载试用新版本! 支持XAML 标记扩 ...
- html邮件链接和锚点链接
锚点链接: 锚点链接: 标记:<a name="XXX"></a> 取读:<a href="#XXX"></a> ...
- SharePoint 网站管理-PowerShell
1. 显示场中所有可用的网站集 Get-SPSite 2. 显示某一Web应用程序下可用的网站集 Get-SPSite –WebApplication "SharePoint – 80&qu ...
- Kali Linux更新源以及设置中文
在终端输入 gedit /etc/apt/sources.list 复制下列源替换原有的 #官方源 deb http://http.kali.org/kali sana main non-free c ...
- HTTP Header之Content-Type
HTTP Header之Content-Type 目录 1. HTTP Header 2. 文件请求和接口请求 3. 几种 Content-Type 3.1 application/x-www-f ...
- 腾讯云nginx配置PHP
腾讯云nginx配置文件 #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log ...
- HDU 1589 Stars Couple(计算几何求二维平面的最近点对和最远点对)
Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...