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中很重要的一种通信方式,是把一个程序的输出直接连接到另一个程序的输入,常 ...
随机推荐
- Azure Cloud中的Log4Net设置
这里的Cloud包含Worker Role和Web Role,Role是运行在云主机中的,这里的主机和VM有所不同,Windows Azure Role Architecture.我们并没有和本地服务 ...
- URAL-1998 The old Padawan 二分
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1998 题意:有n个石头,每个石头有个重量,每个时间点你能让一个石头飞起来,但有m个时间点 ...
- 老 base64 for xe8
not recommend ,only for study procedure TForm1.Button3Click(Sender: TObject); var ssi, sso: TStringS ...
- c# spring aop的简单例子
刚刚完成了一个c#的spring aop简单例子,是在mac下用Xamarin Studio开发的.代码如下: 接口 using System; using System.Collections.Ge ...
- [网络]远程访问局域网svn服务器[转]
转至:http://8474832.blog.51cto.com/8464832/1555449 打开路由器访问界面 选择转发规则->端口映射-新建 在弹出的界面中填写相应的端口号了内网ip 填 ...
- jquery ajax 序列化表单传参提交实体对象到后台action
========action后台我这里使用的是SpringMVC如果用ssh用法一致=============== @Controller@RequestMapping("PubjobCon ...
- java解析属性文件
-----------------------解析属性文件----------------------------- /** * 获取src下属性文件 * @param params * ...
- 递归模式学习(recursion)
所谓递归,就是方法调用自身.对于递归模式来说,要有一个出口来让递归结束,避免出现死循环. 实例全排列: 从n中拿出m个元素进行排列,当n==m时为全排列. 利用递归就是:把n个元素轮流放入第一个位置, ...
- 关于heritrix安装配置时出现”必须限制口令文件读取访问权限”的解决方法
转载:http://www.floatinglife.cn/关于heritrix安装配置时出现必须限制口令文件读取访问 最近开始写一个RSS聚合程序,需要爬虫支持,于是就整来heritrix,没想到, ...
- HTTP 错误 405.0 - Method Not Allowed
如果A页面通过表单(form)向B页面传递参数,而B页面是以“.htm or .html ”为扩展名的话,通过IIS解析会出现“HTTP 错误 405 -禁止访问资源”错误的提示. 原因:IIS解析文 ...