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进程间通信 -- 使用命名管道 一.什么是消息队列 消息队列提 ...
随机推荐
- Ext4报错Uncaught Ext.Loader is not enabled
提示: Uncaught Ext.Loader is not enabled, so dependencies cannot be resolved dynamically. Missing requ ...
- C语言经典程序190例
[程序1] 题目:809*??=800*??+9*??+1 其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数.求??代表的两位数,及809*??后的结果. 1.程序分析: 2.程序 ...
- selenium webdriver使用过程中出现Element is not currently visible and so may not be interacted with的处理方法
参考文章: http://blog.csdn.net/passionboyxie/article/details/28661107 http://www.spasvo.com/ceshi/open/k ...
- Course Schedule II 解答
Question There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may ...
- The Java™ Tutorials下载地址
1.The Java™ Tutorials下载地址: http://www.oracle.com/technetwork/java/javase/java-tutorial-downloads-200 ...
- ubuntu下的Samba配置:使每个用户可以用自己的用户名和密码登录自己的home目录
http://blog.csdn.net/fly_qj/article/details/21744797 1.先要安装Samba sudo apt-get install samba openssh- ...
- linux 之 tar 命令
通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大. tar 命令可以为linu ...
- 单调队列-hdu-4193-Non-negative Partial Sums
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4193 题目大意: 给n个数,a0,a1,...an,求ai,ai+1,...an,a1,a2,... ...
- Base64编码和解码算法
Base64么新鲜的算法了.只是假设你没从事过页面开发(或者说动态页面开发.尤其是邮箱服务),你都不怎么了解过,仅仅是听起来非常熟悉. 对于黑客来说,Base64与MD5算法有着相同的位置.由于电子邮 ...
- 为net-snmp添加读readTimeTicks
function readTimeTicks(time){ if(time === 0) return ''; var d = 0, h = 0, m = 0, s = 0; d = parseInt ...