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 ...
随机推荐
- svn2git使用小记
Github强烈推荐使用svn2git工具将svn repository转成git repository: https://help.github.com/articles/importing-fro ...
- zabbix邮件报警脚本
#!/usr/bin/python #coding:utf-8 import smtplib from email.mime.text import MIMEText import sys mail_ ...
- jbpm4.4 demo3
package cn.itcast.c_processinstance; import java.util.List; import org.jbpm.api.Configuration; impor ...
- 拼接<a>标签相关属性
<s:iterator value="#session.funcs" id="sonbean"> <s:if test="%{#so ...
- Mac 解决SSH登录服务器终端乱码
一.Mac自带的终端 ssh 连接Linux 乱码,可用如下方法解决 终端 --> 偏好设置 --> 描述文件 --> 高级 --> 设为GBK 即可 二.secureCRT ...
- Wbemtest查询
运行wbemtest,打开后连接命名空间,默认为“root\cimv2”,可以连接到”IIS管理命名空间(此为Windows Server 2008 R2)“ 查看该命名空间下所有可用的类:单击“枚举 ...
- Windows Server 2012 R2中的网络诊断命令
Get-NetAdapter Get-NetIPAddress Get-NetIPConfiguration(GIP) TNC :Pinging Servers and Trace Route tnc ...
- 13 引用WINAPI
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetWind ...
- 【转】GitHub删除一个仓库——2013-08-27 21
http://xiacijian.blog.163.com/blog/static/849931902012111195041170/ 1.进入要删除的仓库 2.找到 导航栏 Code NetWor ...
- Java基础知识强化之IO流笔记55:IO流练习之 自定义类模拟LineNumberReader的获取行号功能案例
1. 自定义类模拟LineNumberReader的获取行号功能案例 2. 代码实现: (1)MyBufferedReader.java: package cn.itcast_08; import j ...