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 ...
随机推荐
- [转]freemaker格式化日期
转至:http://wentao365.iteye.com/blog/2047234 1.格式化日期 ${updated?string("yyyy-MM-dd HH:mm:ss") ...
- java 泛型中 T、E ... 和 问号(通配符)的区别
一.泛型中T.E ... 是泛型类.泛型方法定义时候用的. 1.泛型类定义在类后面 紧跟类名后面 public class TestClassDefine<T>{} 2.泛型方法定义在方 ...
- jenkens构建脚本
Build Root POM Goals and options Command # consts SERVER="192.168.60.209" DEPLOY=" ...
- MySQL 统计信息
200 ? "200px" : this.width)!important;} --> 介绍 数据库维护统计信息的目的主要是为了优化器进行更好的执行优化,首先统计信息是建立在 ...
- iOS 检测网络状态
一般有两种方式,都是第三方的框架,轮子嘛,能用就先用着,后面再优化. 一:Reachability 1.首先在AppDelegate.h添加头文件"Reachability.h", ...
- Script Form
Script Form 是SAP所提供的一款强大的报表设设计工具. 一.Script Form主要工具包括如下: 1)Form Painter:格式绘制器,用于格式的设定.TCoce:SE71. 2) ...
- 13 引用WINAPI
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetWind ...
- Linux守护进程的编程实现
Linux 守护进程的编程方法 守护进程(Daemon)是执行在后台的一种特殊进程.它独立于控制终端而且周期性地执行某种任务或等待处理某些发生的事件.守护进程是一种非常实用的进程.Linux的大多数s ...
- mysql init_connect 参数的其他用处
http://blog.itpub.net/133735/viewspace-691196/ init_connect 是可以动态在线调整的,这样就有了一些其他的用处 经过测试init_conne ...
- mysql 如何修改字符串为 utf8
1.命令行输入mysl 密码之后,然后输入 status可以查看当前的编码 2.来到安装目录X:\%path%\MySQL\MySQL Server 5.0\bin\MySQLInstanceConf ...