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进程间通信 -- 使用命名管道 一.什么是消息队列 消息队列提 ...
随机推荐
- vmware 网络连接
解决VMware nat service等服务不能启动 虚拟机如何设置网络连接来上网?
- nginx+vaadin配置
nginx+Vaadin的特殊性在于配置WEBSOCKET或LONG_POLLING.网上资料不多,自己多次尝试配置都不成功,后来终于找到这篇说明才得以配置成功,使用效果不错,介绍如下. 1./etc ...
- Combinations 解答
Question Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. F ...
- Add and Search Word - Data structure design 解答
Question Design a data structure that supports the following two operations: void addWord(word) bool ...
- Python-memcached的基本使用 - Flynewton成长点滴 - 开源中国社区
Python-memcached的基本使用 - Flynewton成长点滴 - 开源中国社区 Python-memcached的基本使用 发表于3年前(2010-12-04 00:02) 阅读(9 ...
- cmd用到的基本操作
dir #显示当前目录中的文件和子目录 dir /a #显示当前目录中的文件和子目录,包括隐藏文件和系统文件 a = all dir c: /a:d #显示 C 盘当前目录中的目录 d = direc ...
- #include<string.h>
#include<string.h> 1 strcpy #include <string.h> char *strcpy(char *str1, const char *str ...
- 熬之滴水穿石:Spring--精简的J2EE(5)
47--Spring的MVC 在Spring的框架中也存在MVC这样的模式,在Spring下有2个这样的控制器一个叫Controller, ...
- C++ Primer 有感(异常处理)
1.异常是通过抛出对象而引发的.该对象的类型决定应该激活哪个处理代码.被选中的处理代码是调用链中与该对象类型匹配且离抛出异常位置最近的那个. 2.执行throw的时候,不会执行跟在throw后面的语句 ...
- lua编译出liblua.so
偶遇一个事情,需要在C里面嵌入Lua代码,这真是痛苦了我好久…. 不知道为啥lua默认编译没有生成.so 的动态链接库,需要修改Makefile生成liblua.so,我用的版本是5.2 一.先修改根 ...