Linux进程间通信——使用匿名管道
- #include <stdio.h>
- FILE* popen (const char *command, const char *open_mode);
- int pclose(FILE *stream_to_close);
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- FILE *read_fp = NULL;
- FILE *write_fp = NULL;
- char buffer[BUFSIZ + 1];
- int chars_read = 0;
- //初始化缓冲区
- memset(buffer, '\0', sizeof(buffer));
- //打开ls和grep进程
- read_fp = popen("ls -l", "r");
- write_fp = popen("grep rwxrwxr-x", "w");
- //两个进程都打开成功
- if(read_fp && write_fp)
- {
- //读取一个数据块
- chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);
- while(chars_read > 0)
- {
- buffer[chars_read] = '\0';
- //把数据写入grep进程
- fwrite(buffer, sizeof(char), chars_read, write_fp);
- //还有数据可读,循环读取数据,直到读完所有数据
- chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);
- }
- //关闭文件流
- pclose(read_fp);
- pclose(write_fp);
- exit(EXIT_SUCCESS);
- }
- exit(EXIT_FAILURE);
- }

- #include <unistd.h>
- int pipe(int file_descriptor[2]);
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- int data_processed = 0;
- int filedes[2];
- const char data[] = "Hello pipe!";
- char buffer[BUFSIZ + 1];
- pid_t pid;
- //清空缓冲区
- memset(buffer, '\0', sizeof(buffer));
- if(pipe(filedes) == 0)
- {
- //创建管道成功
- //通过调用fork创建子进程
- pid = fork();
- if(pid == -1)
- {
- fprintf(stderr, "Fork failure");
- exit(EXIT_FAILURE);
- }
- if(pid == 0)
- {
- //子进程中
- //读取数据
- data_processed = read(filedes[0], buffer, BUFSIZ);
- printf("Read %d bytes: %s\n", data_processed, buffer);
- exit(EXIT_SUCCESS);
- }
- else
- {
- //父进程中
- //写数据
- data_processed = write(filedes[1], data, strlen(data));
- printf("Wrote %d bytes: %s\n", data_processed, data);
- //休眠2秒,主要是为了等子进程先结束,这样做也只是纯粹为了输出好看而已
- //父进程其实没有必要等等子进程结束
- sleep(2);
- exit(EXIT_SUCCESS);
- }
- }
- exit(EXIT_FAILURE);
- }

- #include <unistd.h>
- int dup(int file_descriptor);
- int dup2(int file_descriptor_one, int file_descriptor_two);
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- int data_processed = 0;
- int pipes[2];
- const char data[] = "123";
- pid_t pid;
- if(pipe(pipes) == 0)
- {
- pid = fork();
- if(pid == -1)
- {
- fprintf(stderr, "Fork failure!\n");
- exit(EXIT_FAILURE);
- }
- if(pid == 0)
- {
- //子进程中
- //使标准输入指向fildes[0]
- close(0);
- dup(pipes[0]);
- //关闭pipes[0]和pipes[1],只剩下标准输入
- close(pipes[0]);
- close(pipes[1]);
- //启动新进程od
- execlp("od", "od", "-c", 0);
- exit(EXIT_FAILURE);
- }
- else
- {
- //关闭pipes[0],因为父进程不用读取数据
- close(pipes[0]);
- data_processed = write(pipes[1], data, strlen(data));
- //写完数据后,关闭pipes[1]
- close(pipes[1]);
- printf("%d - Wrote %d bytes\n", getpid(), data_processed);
- }
- }
- exit(EXIT_SUCCESS);
- }

Linux进程间通信——使用匿名管道的更多相关文章
- Linux进程间通信——使用命名管道
在前一篇文章——Linux进程间通信——使用匿名管道中,我们看到了如何使用匿名管道来在进程之间传递数据,同时也看到了这个方式的一个缺陷,就是这些进程都由一个共同的祖先进程启动,这给我们在不相关的的进程 ...
- 练习--LINUX进程间通信之无名管道PIPE
IBM上放的这个系统不错,刚好可以系统回温一下LINUX的系统知识. http://www.ibm.com/developerworks/cn/linux/l-ipc/part1/ 感觉年纪大了,前几 ...
- Linux进程通信----匿名管道
Linux进程通信中最为简单的方式是匿名管道 匿名管道的创建需要用到pipe函数,pipe函数参数为一个数组表示的文件描述字.这个数组有两个文件描 述字,第一个是用于读数据的文件描述符第二个是用于写数 ...
- 《Linux 进程间通信》命名管道:FIFO
命名管道的主要用途:不相关的进程之间交换数据. 命令行上创建命名管道: $ mkfifo filename 程序中创建命名管道: #include <sys/types.h> #incl ...
- Linux进程间通信(四):命名管道 mkfifo()、open()、read()、close()
在前一篇文章—— Linux进程间通信 -- 使用匿名管道 中,我们看到了如何使用匿名管道来在进程之间传递数据,同时也看到了这个方式的一个缺陷,就是这些进程都由一个共同的祖先进程启动,这给我们在不相关 ...
- IPC——匿名管道
Linux进程间通信——使用匿名管道 在前面,介绍了一种进程间的通信方式:使用信号,我们创建通知事件,并通过它引起响应,但传递的信息只是一个信号值.这里将介绍另一种进程间通信的方式——匿名管道,通过它 ...
- Linux进程间通信(三):匿名管道 popen()、pclose()、pipe()、close()、dup()、dup2()
在前面,介绍了一种进程间的通信方式:使用信号,我们创建通知事件,并通过它引起响应,但传递的信息只是一个信号值.这里将介绍另一种进程间通信的方式——匿名管道,通过它进程间可以交换更多有用的数据. 一.什 ...
- linux的IPC进程通信方式-匿名管道(一)
linux的IPC进程通信-匿名管道 什么是管道 如果你使用过Linux的命令,那么对于管道这个名词你一定不会感觉到陌生,因为我们通常通过符号"|"来使用管道,但是管道的真正定义是 ...
- Linux进程间通信(七):消息队列 msgget()、msgsend()、msgrcv()、msgctl()
下面来说说如何用不用消息队列来进行进程间的通信,消息队列与命名管道有很多相似之处.有关命名管道的更多内容可以参阅我的另一篇文章:Linux进程间通信 -- 使用命名管道 一.什么是消息队列 消息队列提 ...
随机推荐
- C# 文件/文件夹压缩
一.ZipFile ZipFile类用于选择文件或文件夹进行压缩生成压缩包. 常用属性: 属性 说明 Count 文件数目(注意是在ComitUpdat之后才有) Password 压缩包密码 Siz ...
- WPF/ArcGIS Engine三维开发和EVC3/4升级到VS项目建议(转)
系统环境:Windows 7 专业版,Visual Studio 2010,ArcGIS Engine 9.3 1.创建项目 创建一个WPF的项目,必须选择.Net framework 3.5(AE9 ...
- UESTC_Big Brother 2015 UESTC Training for Graph Theory<Problem G>
G - Big Brother Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) ...
- UESTC_Sliding Window 2015 UESTC Training for Data Structures<Problem K>
K - Sliding Window Time Limit: 18000/6000MS (Java/Others) Memory Limit: 131072/131072KB (Java/Ot ...
- LeeCode-Linked List Cycle
Given a linked list, determine if it has a cycle in it. /** * Definition for singly-linked list. * s ...
- 棋盘覆盖(大数阶乘,大数相除 + java)
棋盘覆盖 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 在一个2k×2k(1<=k<=100)的棋盘中恰有一方格被覆盖,如图1(k=2时),现用一缺角的 ...
- Android应用程序组件Content Provider的共享数据更新通知机制分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6985171 在Android系统中,应用程序组 ...
- UVA 12232 - Exclusive-OR(带权并查集)
UVA 12232 - Exclusive-OR 题目链接 题意:有n个数字.一開始值都不知道,每次给定一个操作,I a v表示确认a值为v,I a b v,表示确认a^b = v,Q k a1 a2 ...
- C# 移动端与PC端的数据交互
小记:针对目前功能越来越强大的智能手机来说,在PC端支持对手机中的用户数据作同步.备份以及恢复等保护措施的应用已经急需完善.不仅要对数据作保护,而且用户更希望自己的手机跟PC能够一体化,以及和远程服务 ...
- 数据库学习之ADO.NET五大对象
1 [ADO.NET] ado.net 是一种数据访问技术,使得应用程序能够连接到数据存储,并以各种方式操作存储在里面的数据. 2 [ADO.NET五大常用对象] Connec ...