管道和FIFO 二
前面我们学习了一下进程,我们知道多,进程间的地址空间相对独立。进程与进程间不能像线程间通过全局变量通信。 如果想进程间通信,就需要其他机制。

1.2管道的创建

解释如下 :



- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- int main()
- {
- int n;
- int fd[2];
- int count = 0;
- char buf[100] = {0};
- if(pipe(fd) < 0)
- {
- perror("Fail to create pipe");
- exit(EXIT_FAILURE);
- }
- close(fd[1]);
- if((n = read(fd[0],buf,sizeof(buf))) < 0)
- {
- perror("Fail to read pipe");
- exit(EXIT_FAILURE);
- }
- printf("Rread %d bytes : %s.\n",n,buf);
- return 0;
- }
运行结果:

点击(此处)折叠或打开
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <string.h>
- #define N 10
- #define MAX 100
- int child_read_pipe(int fd)
- {
- char buf[N];
- int n = 0;
- while(1)
- {
- n = read(fd,buf,sizeof(buf));
- buf[n] = '\0';
- printf("Read %d bytes : %s.\n",n,buf);
- if(strncmp(buf,"quit",4) == 0)
- break;
- }
- return 0;
- }
- int father_write_pipe(int fd)
- {
- char buf[MAX] = {0};
- while(1)
- {
- printf(">");
- fgets(buf,sizeof(buf),stdin);
- buf[strlen(buf)-1] = '\0';
- write(fd,buf,strlen(buf));
- usleep(500);
- if(strncmp(buf,"quit",4) == 0)
- break;
- }
- return 0;
- }
- int main()
- {
- int pid;
- int fd[2];
- if(pipe(fd) < 0)
- {
- perror("Fail to pipe");
- exit(EXIT_FAILURE);
- }
- if((pid = fork()) < 0)
- {
- perror("Fail to fork");
- exit(EXIT_FAILURE);
- }else if(pid == 0){
- close(fd[1]);
- child_read_pipe(fd[0]);
- }else{
- close(fd[0]);
- father_write_pipe(fd[1]);
- }
- exit(EXIT_SUCCESS);
- }
运行结果:

- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <string.h>
- int main()
- {
- int pid;
- int n;
- int fd[2];
- char buf[1000 * 6] = {0};
- if(pipe(fd) < 0)
- {
- perror("Fail to pipe");
- exit(EXIT_FAILURE);
- }
- if((pid = fork()) < 0)
- {
- perror("Fail to fork");
- exit(EXIT_FAILURE);
- }else if(pid == 0){
- close(fd[1]);
- sleep(5);
- close(fd[0]);
- printf("Read port close.\n");
- sleep(3);
- }else{
- close(fd[0]);
- while(1)
- {
- n = write(fd[1],buf,sizeof(buf));
- printf("Write %d bytes to pipe.\n",n);
- }
- }
- exit(EXIT_SUCCESS);
- }
运行结果:

点击(此处)折叠或打开
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #define MAX 100
- int child_work(int pfd,char *fname)
- {
- int n,fd;
- char buf[MAX];
- if((fd = open(fname,O_WRONLY | O_CREAT | O_TRUNC,0666)) < 0)
- {
- fprintf(stderr,"Fail to open %s : %s.\n",fname,strerror(errno));
- return -1;
- }
- while( n = read(pfd,buf,sizeof(buf)) )
- {
- write(fd,buf,n);
- }
- close(pfd);
- return 0;
- }
- int father_work(int pfd,char *fname)
- {
- int fd,n;
- char buf[MAX];
- if((fd = open(fname,O_RDONLY)) < 0)
- {
- fprintf(stderr,"Fail to open %s : %s.\n",fname,strerror(errno));
- return -1;
- }
- while(n = read(fd,buf,sizeof(buf)))
- {
- write(pfd,buf,n);
- }
- close(pfd);
- return 0;
- }
- int main(int argc,char *argv[])
- {
- int pid;
- int fd[2];
- if(argc < 3)
- {
- fprintf(stderr,"usage %s argv[1] argv[2].\n",argv[0]);
- exit(EXIT_FAILURE);
- }
- if(pipe(fd) < 0)
- {
- perror("Fail to pipe");
- exit(EXIT_FAILURE);
- }
- if((pid = fork()) < 0)
- {
- perror("Fail to fork");
- exit(EXIT_FAILURE);
- }else if(pid == 0){
- close(fd[1]);
- child_work(fd[0],argv[2]);
- }else{
- close(fd[0]);
- father_work(fd[1],argv[1]);
- wait(NULL);
- }
- exit(EXIT_SUCCESS);
- }

点击(此处)折叠或打开
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- int main(int argc,char *argv[])
- {
- int fd;
- if(argc < 2)
- {
- fprintf(stderr,"usage : %s argv[1].\n",argv[0]);
- exit(EXIT_FAILURE);
- }
- if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
- {
- fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno));
- exit(EXIT_FAILURE);
- }
- if((fd = open(argv[1],O_WRONLY)) < 0)
- {
- fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno));
- exit(EXIT_FAILURE);
- }
- printf("open for write success.\n");
- return 0;
- }
B.open for read
点击(此处)折叠或打开
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- int main(int argc,char *argv[])
- {
- int fd;
- if(argc < 2)
- {
- fprintf(stderr,"usage : %s argv[1].\n",argv[0]);
- exit(EXIT_FAILURE);
- }
- if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
- {
- fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno));
- exit(EXIT_FAILURE);
- }
- if((fd = open(argv[1],O_RDONLY)) < 0)
- {
- fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno));
- exit(EXIT_FAILURE);
- }
- printf("open for read success.\n");
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #define MAX 655360
- int main(int argc,char *argv[])
- {
- int n,fd;
- char buf[MAX];
- if(argc < 2)
- {
- fprintf(stderr,"usage : %s argv[1].\n",argv[0]);
- exit(EXIT_FAILURE);
- }
- if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
- {
- fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno));
- exit(EXIT_FAILURE);
- }
- if((fd = open(argv[1],O_WRONLY )) < 0)
- {
- fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno));
- exit(EXIT_FAILURE);
- }
- printf("open for write success.\n");
- while(1)
- {
- printf(">");
- scanf("%d",&n);
- n = write(fd,buf,n);
- printf("write %d bytes.\n",n);
- }
- exit(EXIT_SUCCESS);
- }
点击(此处)折叠或打开
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #define MAX 655360
- int main(int argc,char *argv[])
- {
- int fd,n;
- char buf[MAX];
- if(argc < 2)
- {
- fprintf(stderr,"usage : %s argv[1].\n",argv[0]);
- exit(EXIT_FAILURE);
- }
- if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
- {
- fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno));
- exit(EXIT_FAILURE);
- }
- if((fd = open(argv[1],O_RDONLY )) < 0)
- {
- fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno));
- exit(EXIT_FAILURE);
- }
- printf("open for read success.\n");
- while(1)
- {
- printf(">");
- scanf("%d",&n);
- n = read(fd,buf,n);
- printf("Read %d bytes.\n",n);
- }
- exit(EXIT_SUCCESS);
- }
读者可以将这两个程序运行,然后输入read和write FIFO大小就可以看到效果。
管道和FIFO 二的更多相关文章
- 管道和FIFO 一
管道和FIFO 管道(pipe) 管道在Unix及Linux进程间通信是最基础的,很容易理解.管道就像一个自来水管,一端注入水,一端放出水,水只能在一个方向上流动,而不能双向流动.管道 ...
- linux系统编程之管道(三):命令管道(FIFO)
一,匿名管道PIPE局限性 管道的主要局限性正体现在它的特点上: 只支持单向数据流: 只能用于具有亲缘关系的进程之间: 没有名字: 管道的缓冲区是有限的(管道制存在于内存中,在管道创建时,为缓冲区分配 ...
- 第四章:管道与FIFO
4.1:概述 管道是最初的Unix IPC形式,可追溯到1973年的Unix第三版.尽管对于许多操作来说很有用,但它们的根本局限在于没有名字,从而只能由亲缘关系的进程使用.这一点随FIFO的加入得改正 ...
- linux进程间通信-有名管道(FIFO)
有名管道(FIFO) 命名管道也被称为FIFO文件,是一种特殊的文件.由于linux所有的事物都可以被视为文件,所以对命名管道的使用也就变得与文件操作非常统一. (1)创建命名管道 用如下两个函数中的 ...
- 第4章 管道和FIFO
4.1 管道 管道是由pipe函数创建的,提供一个单向数据流. 头文件 #include <unistd.h> 函数原型 int pipe(int fd[2]); 返回值 成功则为0,出错 ...
- Linux系统编程——进程间通信:命名管道(FIFO)
命名管道的概述 无名管道,因为没有名字,仅仅能用于亲缘关系的进程间通信(很多其它详情.请看<无名管道>).为了克服这个缺点.提出了命名管道(FIFO).也叫有名管道.FIFO 文件. 命名 ...
- 第4章 管道与FIFO
4.1 概述 管道只在亲缘进程间使用,FIFO在任意进程间使用 4.2 管道 #include <unistd.h> ]) fd[0]用来读管道,fd[1]用来写管道 1)命令who | ...
- [转] IPC之管道、FIFO、socketpair
管道和FIFO作为最初的UNIX IPC形式,现在已用得较少.SocketPair可作为全双工版本的管道,较为常用,这里简单做个笔记 管道 * 只用于有亲缘关系的进程间通信 * 单向,即半双工 (双向 ...
- linux 有名管道(FIFO)
http://blog.csdn.net/firefoxbug/article/details/8137762 linux 有名管道(FIFO) 管道的缓冲区是有限的(管道制存在于内存中,在管道创建时 ...
随机推荐
- ASP.NET WEB API 2: HTTP MESSAGE LIFECYLE
https://www.asp.net/media/4071077/aspnet-web-api-poster.pdf 1.You can host Web API inside IIS or ins ...
- CCNA学习指南 -开放最短路径优先OSPF(多区域部分)
在之前的介绍中,可以看到单区域OSPF对于古老的RIP的优点: 路由选择更新流量减小 使用与大型网络和链路速度不一样的网络 OSPF能够在LSDB中呈现网络拓扑结构,这使得它汇聚的速度远快于RIP. ...
- 日志_测试代码_Delphi7
1. 2.Delphi (Windows API 文件尾部添加) function LogFile(_str :string) :integer; var hFile :THandle; strFil ...
- css3 属性——calc()
其实在之前学习CSS3的时候,我并没有注意到有calc()这个属性,后来在看一个大牛的代码的时候看到了这个,然后就引发了后来的一系列的查找.学习,以及这篇博客的诞生.好了,废话不多说了,来干正事. 一 ...
- ActiveRecord 惰性加载,和使用gem faker
rails console后: 2.1.4 :001 > User # => User (call 'User.connection' to establish a connection) ...
- UVA-4288 Cat vs. Dog (最大独立集)
题目大意:猫狗大赛,s个猫和t个狗,有v个投票者,他们不是爱猫者就是爱狗者,所以爱猫者一定会对某条猫投晋级票,对某条狗投淘汰票,爱狗者亦然.现在已知这v个人的投票情况,应该使哪些畜牲晋级或淘汰才能使投 ...
- 浅谈jsonp
要谈jsonp,首先要弄明白jsonp是什么,它是用来干嘛的.jsonp其实就是我们常用的script标签,用来解决跨域的,只不过这个标签是动态创建的,为啥要动态创建涅. 举个小栗子: 假如我们远程文 ...
- Jenkins插件开发(二)-- HelloWorld
在上一篇blog中我们讲了如何搭建jenkins插件的开发环境,接下来介绍如何开发我们的插件. 创建HelloWorld插件 学习每门新语言的时候,我们都会写一个HelloWorld程序,这里介绍的是 ...
- NodeJS反序列化漏洞利用
原文来自:http://www.4hou.com/web/13024.html node.js是一个服务器端的运行环境,封装了Google V8引擎,V8引擎执行JavaScript速度非常快,性能非 ...
- HTML学习3---排版标记
上节,我们学习了boda常用的属性以及HTML的一些标记,但是图显示的效果却不是那么的好看. 原因就是没有排版好,我们这次使用居中来使这个页面更好看一点,顺便多加入几个别的标记. HTML排版标记 ( ...