管道的一种常见用法:在父进程创建子进程后向子进程传递参数。例如,一个应用软件有一个主进程和很多个不同子进程。

主进程创建子进程后,在子进程调用exec函数执行一个新程序前,通过管道给即将执行的程序传递命令行参数,子进程根据传来

的参数进行初始化或其他操作。

大致思路:

The child can then exec() another program, which inherits the standard streams.

父进程关闭 管道读端  close( fd[0] );  调用  dup2(fd[1], STDOUT_FILENO);  将管道的写端重定向到标准输出

子进程关闭 管道写端 close( fd[1] ); 调用 dup2(fd[0], STDIN_FILENO); exec调用的进程中读取标准输入

main程序:

#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h> int main(int argc, char* argv[], char** environ)
{
char* str = "from parent's message";
int stat;
pid_t pid;
int fd[]; pipe(fd); pid = fork();
if( == pid)//child read
{
close(fd[]);
dup2(fd[], STDIN_FILENO); execve("myprocess", argv, environ); }
else//parent write
{ close(fd[]);
int old = dup(STDOUT_FILENO);
int new = dup2(fd[], STDOUT_FILENO); write(fd[], str, strlen(str)+); dup2(old, new);//恢复重定向 wait(&stat);//
if ( WIFEXITED(stat) )
{
printf("child exited with code:%d\n", WEXITSTATUS(stat));
} close(fd[]); exit(); } }

myprocess程序:

#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h> int main(int argc, char* argv[])
{
printf("myprocess begin\n");
char buf[];
bzero(buf, sizeof(buf)); read(STDIN_FILENO, buf, sizeof(buf)); printf("recv message:%s\n", buf); exit(); }

main程序的执行结果:

myprocess begin
recv message:from parent's message
child exited with code:33

------------------------------------------------------------------------

可见,exec调用的程序获取到了主进程写入管道的数据。

这在实际项目中是经常用到的,主进程启动多个不同功能的exec调用,并通过管道的方式传递数据给启动的程序。

管道pipe与dup结合使用,应用实例的更多相关文章

  1. 管道pipe与dup结合使用

    前面的例子中,子进程可以直接共享父进程的文件描述符.但是如果子进程调用exec函数执行另一个应用程序时,就不能再共享了. 这种情况下可以将子进程中的文件描述符重定向到标准输入,当新执行的程序从标准输入 ...

  2. Linux简单程序实例(GNU工具链,进程,线程,无名管道pipe,基于fd的文件操作,信号,scoket)

    一, GNU工具链简介: (1)编译代码步骤: 预处理 -> 编译 -> 汇编 -> 链接: 预处理:去掉注释,进行宏替换,头文件包含等工作: gcc -E test.c -o te ...

  3. linux中管道(pipe)一谈

    /*********************************************** 管道(pipe)是Linux上进程间通信的一种方式,其是半双工(数据流只能在一个方向上流动(还需要经过 ...

  4. 管道Pipe

    管道Pipe java.nio.channels包中含有一个名为Pipe(管道)的类.广义上讲,管道就是一个用来在两个实体之间单向传输数据的导管.管道的概念对于Unix(和类Unix)操作系统的用户来 ...

  5. Linux进程间通信之管道(pipe)、命名管道(FIFO)与信号(Signal)

    整理自网络 Unix IPC包括:管道(pipe).命名管道(FIFO)与信号(Signal) 管道(pipe) 管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道 ...

  6. Linux 进程间通信之管道(pipe),(fifo)

     无名管道(pipe) 管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允许无亲缘关系进程间的通信: 定义函数: int pipe(int f ...

  7. 进程间通信之管道--pipe和fifo使用

    匿名管道pipe 函数原型: #include <unistd.h> int pipe(int fildes[2]); 参数说明 fildes是我们传入的数组,也是一个传出参数.filde ...

  8. Python--线程队列(queue)、multiprocessing模块(进程对列Queue、管道(pipe)、进程池)、协程

    队列(queue) 队列只在多线程里有意义,是一种线程安全的数据结构. get与put方法 ''' 创建一个“队列”对象 import queue q = queue.Queue(maxsize = ...

  9. 【IPC第二个进程间通信】管道Pipe

    IPC进程间通信+管道Pipe                IPC(Inter-Process Communication,进程间通信).         管道用于进程间共享数据,事实上质是共享内存 ...

随机推荐

  1. jdk自带的ThreadLocal和netty扩展的FastThreadLocal比较总结

    最近在分析一潜在内存泄露问题的时候,jmap出来中有很多的FastThreadLocalThread实例,看了下javadoc,如下: A special variant of ThreadLocal ...

  2. 计算两个集合的差集——第六期 Power8 算法挑战赛

    第六期Power8大赛 1.1 比赛题目 题目: 计算两个集合的差集: 详细说明: 分别有集合A和B两个大数集合,求解集合A与B的差集(A中有,但B中无的元素),并将结果保存在集合C中,要求集合C中的 ...

  3. 20145220韩旭飞《网络对抗》Exp6 信息搜集与漏洞扫描

    20145220韩旭飞<网络对抗>Exp6 信息搜集与漏洞扫描 信息搜集 whois查询 以百度的网址为例,使用whois查询域名注册信息: 从上图中可以得到3R注册信息,包括注册人的名字 ...

  4. 20144303石宇森《网络对抗》注入shellcode和Return-to-libc攻击

    20144303石宇森<网络对抗>PC平台逆向破解 实验1:shellcode注入 实验基础 1.Linux下有两种基本构造攻击buf的方法:retaddr+nop+shellcode,n ...

  5. 用Win32 实现进度条

    转载:http://www.cctry.com/thread-238862-1-1.html #include <windows.h> #include <commctrl.h> ...

  6. python数据库编程小应用

    python DB api 数据库连接对象connection数据库交互对象cursor数据库异常类exceptions 流程:开始创建connection获取cursor执行查询.执行命令.获取数据 ...

  7. sql 题目

    1.自增列 通用: ) from table b where b.sid<a.sid) ,* from table a; ,),* from ... 第二个已经有主键自增列的就不可以用了 还有就 ...

  8. BZOJ2724 [Violet]蒲公英 分块

    题目描述 经典区间众数题目 然而是权限题,所以题目链接放Luogu的 题解 因为太菜所以只会$O(n*\sqrt{n}+n*\sqrt{n}*log(n))$的做法 就是那种要用二分的,并不会clj那 ...

  9. Java异常类复习总结

    个人理解先行: 异常类是当在程序出现问题时抛出的一个警告.提示你程序设计或者代码有存在错误的地方. 异常类和Error都继承自Throwable, Throwable继承自Object类. Runti ...

  10. Python Sip [RuntimeError: the sip module implements API v11.0 to v11.2 but the PyQt5.QtCore module requires API v11.3]

    不知道原因,尝试卸载.编译安装均失败.只有这样曲线救国 import matplotlib matplotlib.use("WXAgg",warn=True) import mat ...