IPC——命名管道
Linux进程间通信——使用命名管道
转载:http://blog.csdn.net/ljianhui/article/details/10202699
#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));
open(const char *path, O_RDONLY);//
open(const char *path, O_RDONLY | O_NONBLOCK);//
open(const char *path, O_WRONLY);//
open(const char *path, O_WRONLY | O_NONBLOCK);//
#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 = -;
int data_fd = -;
int res = ;
const int open_mode = O_WRONLY;
int bytes_sent = ;
char buffer[PIPE_BUF + ]; if(access(fifo_name, F_OK) == -)
{
//管道文件不存在
//创建命名管道
res = mkfifo(fifo_name, );
if(res != )
{
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 != -)
{
int bytes_read = ;
//向数据文件读取数据
bytes_read = read(data_fd, buffer, PIPE_BUF);
buffer[bytes_read] = '\0';
while(bytes_read > )
{
//向FIFO文件写数据
res = write(pipe_fd, buffer, bytes_read);
if(res == -)
{
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);
}
源文件fiforead.c的代码如下:
#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 = -;
int data_fd = -;
int res = ;
int open_mode = O_RDONLY;
char buffer[PIPE_BUF + ];
int bytes_read = ;
int bytes_write = ;
//清空缓冲数组
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, );
printf("Process %d result %d\n",getpid(), pipe_fd); if(pipe_fd != -)
{
do
{
//读取FIFO中的数据,并把它保存在文件DataFormFIFO.txt文件中
res = read(pipe_fd, buffer, PIPE_BUF);
bytes_write = write(data_fd, buffer, res);
bytes_read += res;
}while(res > );
close(pipe_fd);
close(data_fd);
}
else
exit(EXIT_FAILURE); printf("Process %d finished, %d bytes read\n", getpid(), bytes_read);
exit(EXIT_SUCCESS);
}


IPC——命名管道的更多相关文章
- 域渗透:IPC$ 命名管道
介绍:IPC$(Internet Process Connection) 是共享 " 命名管道 " 的资源,它是为了让进程间通信而开放的命名管道,通过提供可信任的用户名和口令,连接 ...
- 进程通信类型 管道是Linux支持的最初Unix IPC形式之一 命名管道 匿名管道
管道 Linux环境进程间通信(一) https://www.ibm.com/developerworks/cn/linux/l-ipc/part1/index.html 管道及有名管道 郑彦兴200 ...
- 【windows 操作系统】进程间通信(IPC)简述|无名管道和命名管道 消息队列、信号量、共享存储、Socket、Streams等
一.进程间通信简述 每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都看不到,所以进程之间要交换数据必须通过内核,在内核中开辟一块缓冲区,进程1把数据从用户空间拷到内核缓冲区,进 ...
- SQL Server 连接问题圣经-命名管道
SQL Server 连接问题圣经-命名管道 (1) APGC DSD Team 12 Jan 2011 1:24 AM 3 一.前言 在使用SQL Server 的过程中,用户遇到的最多的莫过于连接 ...
- [杂]SQL Server 之命名管道连接
命名管道是通过进程间通信(IPC)机制实现通信.具体来说,命名管道建立在服务器的IPC$共享基础上,通过IPC$共享来进行通信. SQL Server命名管道 SQL Server 首先在服务器上创建 ...
- 【LINUX/UNIX网络编程】之使用消息队列,信号量和命名管道实现的多进程服务器(多人群聊系统)
RT,使用消息队列,信号量和命名管道实现的多人群聊系统. 本学期Linux.unix网络编程的第三个作业. 先上实验要求: 实验三 多进程服务器 [实验目的] 1.熟练掌握进程的创建与终止方法: 2 ...
- SQL Server 连接问题-命名管道
原文:SQL Server 连接问题-命名管道 出自:http://blogs.msdn.com/b/apgcdsd/archive/2011/01/12/sql-server-1.aspx 一.前言 ...
- Linux环境进程间通信(一):管道及命名管道
linux下进程间通信的几种主要手段: 管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...
- Linux中的pipe(管道)与named pipe(FIFO 命名管道)
catalogue . pipe匿名管道 . named pipe(FIFO)有名管道 1. pipe匿名管道 管道是Linux中很重要的一种通信方式,是把一个程序的输出直接连接到另一个程序的输入,常 ...
随机推荐
- C语言基础(不断更新)
1.memcpy. memmove.memccpy的区别 字符串函数功能查询 memcpy要求源串和目的串不能重叠 memccpy:copy直至遇到由参数指定的ch. memmove: 源串和目的串可 ...
- 理解OAuth 2.0 -摘自网络
OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. 本文对OA ...
- LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS Memor ...
- 查询processlist具体信息
SELECT * FROM information_schema.PROCESSLIST WHERE HOST LIKE '%172.16.10.22%' AND COMMAND <> ' ...
- HDU2227Find the nondecreasing subsequences(树状数组+DP)
题目大意就是说帮你给出一个序列a,让你求出它的非递减序列有多少个. 设dp[i]表示以a[i]结尾的非递减子序列的个数,由题意我们可以写出状态转移方程: dp[i] = sum{dp[j] | 1&l ...
- Excel合并单元格数据
1.=A1&B1 2.=CONCATENATE(A1,B1)
- A*算法为什么是最优的
图搜索的A*算法有两种情况: hn是可采纳的,但是不是满足一致性 如果满足一致性,A*算法的实现要简单一些:即使不检查closed节点的状态重复,也能得到最优的结果 下面是证明最优性的一些关键点: 1 ...
- 关于TabControl的Trigger【项目】
我有一个TabControl <TabControl x:Name="ToolSystemSection" Grid.Row="4" ContentTem ...
- Hadoop集群基准测试
hadoop jar ./share/hadoop/mapreduce/hadoop-mapreduce-client-jobclient-2.2.0-tests.jar TestDFSIO -wri ...
- centos 没有可用的网络设备
在重装的时候记得查看网络情况,提示没有可用的网络设备. 系统是64位的,我再创建虚拟机选择客户机操作系统的时候,选择成了 centos .不是centos 64位.改成centos 64位后,就显示了 ...