管道和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)如果一 ...
 
随机推荐
- VC++ 报错:Heap corruption detected
			
今天在写代码时,发现莫名其妙的错误: std::string strName = L“testtest”; char* pOutString = new char(len + 1); Decrypt( ...
 - 模拟QQ分组
			
package com.lixu.fenzu; import java.util.ArrayList; import java.util.HashMap; import android.app.Lis ...
 - 清除 eclipse svn 账号密码
			
进入目录 C:\Documents and Settings\administrator\Application Data\Subversion\auth 删除目录下所有文件,然后重新刷新svn地址就 ...
 - WebGL编程指南高级技术篇(常见需求的处理)
			
一.鼠标控制模型旋转 实质的根据鼠标移动前后的位置比较得出x,y轴的旋转角度: 图中是一个屏幕,有一个模型(恩,他是一个模型),鼠标由P点移动到P1点,我们假定移动单位步长旋转β角度: P(x1,y1 ...
 - 玩转TypeScript(3)--类型转换
			
使用强类型变量常常需要从一种类型向另一种类型转换,通常使用ToString或ParseInt可以来实现一些简单的转换,但是有时候需要像.NET语言中那样将一种类型显示的转换为另一种类型,在TypeSc ...
 - js中准确判断数据类型的方法
			
一 通用的typeof 方法 typeof ture 输出 Boolean typeof 123 输出 number ..... 但是 typeof 无法判断 nu ...
 - 拒绝了对对象 '****'(数据库 '******',所有者 '***')的 SELECT 权限
			
数据库(xxx) --->安全性---->架构---->dbo(属性)--->权限--->添加--->浏览-->public 添加 select 等您需要的权 ...
 - dockerk个人学习(0)
			
接下来几篇记录学习docker和ks的部署搭建环境和应用部署等
 - 系统有专门画图的api
 - ECHO不换行
			
我想用批处理实现向s.txt中多次分别导入文本例如:“aaaa","bbbb","cccc","dddd"实现s.txt内效果如: ...