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);}elseexit(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);}elseexit(EXIT_FAILURE);printf("Process %d finished, %d bytes read\n", getpid(), bytes_read);exit(EXIT_SUCCESS);
}
Linux进程间通信——使用命名管道相关推荐
- linux命名管道进程间通信,《Linux 进程间通信》命名管道:FIFO
命名管道的主要用途:不相关的进程之间交换数据. 命令行上创建命名管道: $ mkfifo filename 程序中创建命名管道: #include #include int mkfifo(const ...
- linux 命名管道通信速度,《Linux 进程间通信》命名管道:FIFO
命名管道的主要用途:不相关的进程之间交换数据. 命令行上创建命名管道: $ mkfifo filename 程序中创建命名管道: #include #include int mkfifo(const ...
- linux进程间通信:命名管道FIFO
文章目录 FIFO 通信特点 系统调用接口 应用 拥有亲缘关系之间的进程通信 非亲缘关系进程之间的通信 总结 FIFO 通信特点 FIFO文件有文件名 可以像普通文件一样存储在文件系统之中 可以像普通 ...
- c语言系统编程六:Linux进程间通信之无名管道
Linux进程间通信之无名管道 一 文件描述符复制 1.1 dup函数(复制文件描述符) 1.2 dup2函数(复制文件描述符) 二 无名管道的概述 三 无名管道的特点 四 无名管道的创建和使用 4. ...
- python命名管道通讯_Python进程间通信的命名管道详解(Windows)
本文和大家分享的主要是windows系统下,python进程间通信的命名管道相关内容 ,一起来看看吧,希望对大家学习python有所帮助. 在 Windows 上的命名管道主要是通过调用win32 a ...
- 【Linux】进程间通信 - 匿名/命名管道与System V共享内存
目录 前言 一.管道 0.什么是管道 1).管道的概念 2).管道的本质 3).管道指令: "|" 1.匿名管道 1).如何创建匿名管道 2).如何使用匿名管道进行通信 3).匿名 ...
- 【Linux系统编程】进程间通信之命名管道
00. 目录 文章目录 00. 目录 01. 命名管道概述 02. 命名管道创建 03. 命名管道特性 04. 命名管道非阻塞 05. 附录 01. 命名管道概述 无名管道,由于没有名字,只能用于亲缘 ...
- Linux系统编程——进程间通信:命名管道(FIFO)
命名管道的概述 无名管道,由于没有名字,只能用于亲缘关系的进程间通信(更多详情,请看<无名管道>).为了克服这个缺点,提出了命名管道(FIFO),也叫有名管道.FIFO 文件. 命名管道( ...
- linux c之命名管道简单使用
1.介绍FIFO(命名管道) 管道有局限性,只能在具有亲缘关系的进程间通信,但是命名管道克服了这个问题,可以实现无亲缘关系的进程之间的通信 API介绍: /* FIFO 命名管道 */ /* * 函数 ...
最新文章
- UE4风格化场景设计入门指南 Stylized Station – The Environment Artist’s Survival Kit
- Decoders对于语义分割的重要性 | CVPR 2019
- angular之表单
- 关于raid5 5e 5ee的性能 对比及其数据恢复原理
- 【CodeForces - 1096D】Easy Problem(dp,思维)
- 动态ram依靠什么存储信息_处理器内存系统1(ROM,RAM,数据存储顺序)
- 简单的企业网站后台的实现之流程
- Vmware之锁定文件失败
- java分布式缓存memcached_分布式缓存系统Memcached学习心得
- 创建 VSTO 工作簿的 ClickOnce 安装包
- lol服务器维护2021,英雄联盟客户端清理:2021进度与后续计划
- 江苏省发布我国首个公路行业BIM省地方标准
- 什么是测试用例?什么是测试脚本?两者有什么关系?为什么要编写测试用例?测试用例的作用是什么?
- linux驱动开发:mma7660 sensor的配置
- css设置十六进制背景色和透明度
- 项目审查图片报404找不到
- 用ssh方法scp,从本地传输文件到服务器
- coreldraw带圈字符_coreldraw中如何使字体围绕一个圆形排列?
- 天津室内设计培训班:3分钟带你了解室内设计的6大原则
- BEA CEO庄思浩清华大学演讲(2002/12)