linux进程间通信--有名管道
有名管道
列子:
读文件
#include<sys/types.h>
2 #include<sys/stat.h>
3 #include<errno.h>
4 #include<fcntl.h>
5 #include<stdio.h>
6 #include<string.h>
7 #include<stdlib.h>
8
9 #define FIFO "/tmp/myfifo"
10
11 int main(int argc , char**argv)
12 {
13 char buf_r[100];
14 int fd;
15 int nread;
16 if(( mkfifo(FIFO,O_CREAT|O_EXCL) < 0)&&(errno != EEXIST))
17 {
18 printf("can't create fifoserver \n");
19 }
20 printf("preparing for reading bytes..\n");
21 memset(buf_r, 0,sizeof(buf_r));
22 fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);
23 if(fd == -1)
24 {
25 perror("open");
26 exit(1);
27 }
28 while(1)
29 {
30 memset(buf_r, 0,sizeof(buf_r));
31 if(nread=read(fd,buf_r ,100)== -1)
32 {
33 if(errno==EAGAIN)
34 printf("no data \n");
35
36 }
37 printf("read%sfrom FIFO\n",buf_r);
38 sleep(1);
39 }
40 pause();
41 unlink(FIFO);
42 }
写文件
1 #include<sys/types.h>
2 #include<sys/stat.h>
3 #include<errno.h>
4 #include<fcntl.h>
5 #include<stdio.h>
6 #include<stdlib.h>
7 #include<string.h>
8
9 #define FIFO_SERVER "/tmp/myfifo"
10
11 int main(int argc , char** argv)
12 {
13 int fd;
14 char w_buf[100]={'\0'};
15 int nwrite;
16 if( fd == -1 )
17 {
18 if(errno==ENXIO)
19 printf("open error\n");
20 }
21 fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);
22 if(argc == 1)
23 printf("please send something\n");
24 strcpy(w_buf,argv[1]);
25 if( (nwrite = write(fd,w_buf,100) )== -1 )
26 {
27 if( errno == EAGAIN)
28 printf("The FIFO has not been read yet\n");
29 }
30 else
31 printf("write %s to the FIFO\n",w_buf);
linux进程间通信--有名管道的更多相关文章
- linux进程间通信-有名管道(FIFO)
有名管道(FIFO) 命名管道也被称为FIFO文件,是一种特殊的文件.由于linux所有的事物都可以被视为文件,所以对命名管道的使用也就变得与文件操作非常统一. (1)创建命名管道 用如下两个函数中的 ...
- Linux 进程间通信 有名管道(fifo)
有名管道特点: 1)无名管道只能用于具有亲缘关系的进程之间,这就限制了无名管道的使用范围 2)有名管道可以使互不相关的两个进程互相通信. 3)有名管道可以通过路径名来指出,并且在文件系统中可见,但内容 ...
- Linux进程间通信之管道
1,进程间通信 (IPC ) Inter-Process Communication 比较好理解概念的就是进程间通信就是在不同进程之间传播或交换信息. 2,linux下IPC机制的分类:管道.信号.共 ...
- Linux -- 进程间通信之管道
管道是 Linux 里的一种文件类型,同时也是 Linux 系统下进程间通信的一种方式 创建一个管道文件有两种方式: Shell 下命令 mkfifo + filename,即创建一个有名管道 ...
- Linux 进程间通信(二) 管道
Linux 进程间通信-管道 进程是一个独立的资源分配单位,不同进程之间的资源是相互独立的,没有关联,不能在一个进程中直接访问另一个进程中的资源.但是,进程不是孤立的,不同的进程之间需要信息的交换以及 ...
- linux进程间通信之管道篇
本文是对http://www.cnblogs.com/andtt/articles/2136279.html管道一节的进一步阐释和解释 1 管道 1.1 管道简介 管道是unix系统IPC的最古老的形 ...
- Linux进程间通信之管道(pipe)、命名管道(FIFO)与信号(Signal)
整理自网络 Unix IPC包括:管道(pipe).命名管道(FIFO)与信号(Signal) 管道(pipe) 管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道 ...
- Linux 进程间通信之管道(pipe),(fifo)
无名管道(pipe) 管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允许无亲缘关系进程间的通信: 定义函数: int pipe(int f ...
- linux之有名管道
有名管道1.查看命令:man 3 mkfifo 2.头文件:#include <sys/types.h> #include <sys/stat.h> 3.函数原型:int mk ...
随机推荐
- requirejs 打包参数
https://github.com/requirejs/r.js/blob/master/build/example.build.js
- zend studio-字体大小设置
在使用zend studio的过程中为了方便我们编码,很多时候需要设置编码的字体的大小,设置步骤如下: 选择[Windows]-[preference]-[general]-[appearance]- ...
- Delphi中DLL的其他应用
http://blog.csdn.net/zhenghui1/article/details/6618273 1.DLL的入口函数和出口函数 在编写DLL时可以在DLL项目文件的begin..end之 ...
- 【点滴积累】通过特性(Attribute)为枚举添加更多的信息
转:http://www.cnblogs.com/IPrograming/archive/2013/05/26/Enum_DescriptionAttribute.html [点滴积累]通过特性(At ...
- phpmailer使用gmail SMTP的方法
终于能够通过phpmailer使用gmail账号发送邮件了phpmailer(现在的版本是1.73)是一个很好用的工具,可以很方便的使用php语言发送邮件,支持smtp及验证,我们一直都用它. 但是, ...
- WPF柱状图(支持数据库动态更新)之组件的数据动态化
WPF柱状图(支持数据库动态更新) 在这片文章中我们介绍了如何将柱状图包装成一个组件,将这个组件的属性对外开放和组件的外部属性根内部属性绑定以及非轮询动态更新数据的方式. 非轮询更新数据感觉介绍的不够 ...
- Android5.0之TextInputLayout、FloatingActionButton的使用
TextInputLayout和FloatingActionButton都属于MD风格的控件,比起普通的EditText和Button.ImageButton,TextInputLayout和Floa ...
- skynet启动过程_bootstrap
这遍摘自skynet 的wiki skynet 由一个或多个进程构成,每个进程被称为一个 skynet 节点.本文描述了 skynet 节点的启动流程. skynet 节点通过运行 skynet 主程 ...
- 请编程实现:产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复(百度了一下,get一种高性能算法,非递归)
网上找到一种更好的实现方式: (1)把N个数放到容器A(int数组)中. (2)从N个数中随机取出1个数放入容器B(int数组)中. (3)把容器A中最后一个数与随机抽取的数对调 或者 把容器A中最后 ...
- mysql左外连接,右外连接,全连接