Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1656011
  • 博文数量: 1493
  • 博客积分: 38
  • 博客等级: 民兵
  • 技术积分: 5834
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-19 17:28
文章分类

全部博文(1493)

文章存档

2016年(11)

2015年(38)

2014年(137)

2013年(253)

2012年(1054)

2011年(1)

分类:

2012-10-30 10:43:09

原文地址:C小程序 - pipe与fork结合 作者:niannian

                                   pipe(p)
                                   fork()
                                      |
--------------------------------------------------------------------------------
| child process                                                         | parent process
close(p[0]);                                                              close(p[1])
dup2(p[1], 1)                                                           dup2(p[0],0)
close(p[1])                                                               close(p[0])
execlp "who"                                                           exec "sort"

这里要了解一个系统调用函数 execlp 的用法
#include
int execlp(const char * file,const char * arg,...,(char *)0);

execlp()会从PATH 环境变量所指的目录中查找符合参数file的文件名,找到后便执行该文件,然后将第二个以后的参数当做该文件的argv[0]、argv[1]……,最后一个参数必须用空指针(NULL)作结束。如果用常数0来表示一个空指针,则必须将它强制转换为一个字符指针,否则将它解释为整形参数,如果一个整形数的长度与char * 的长度不同,那么exec函数的实际参数就将出错。如果调用成功,进程自己的执行代码就会变成加载程序的代码,execlp()后边的代码也就不会执行了.


  1. #include    <stdio.h>
  2. #include    <unistd.h>

  3. #define    oops(m,x)    { perror(m); exit(x); }

  4. main(int ac, char **av)
  5. {
  6.     int    thepipe[2],            /* two file descriptors    */
  7.         newfd,                /* useful for pipes    */
  8.         pid;                /* and the pid        */

  9.     if ( ac != 3 ){
  10.         fprintf(stderr, "usage: pipe cmd1 cmd2\n");
  11.         exit(1);
  12.     }
  13.     if ( pipe( thepipe ) == -1 )        /* get a pipe        */
  14.         oops("Cannot get a pipe", 1);

  15.     /* ------------------------------------------------------------ */
  16.     /*    now we have a pipe, now let's get two processes        */

  17.     if ( (pid = fork()) == -1 )            /* get a proc    */
  18.         oops("Cannot fork", 2);

  19.     /* ------------------------------------------------------------ */
  20.     /*     Right Here, there are two processes            */
  21.     /* parent will read from pipe            */

  22.     if ( pid > 0 ){            /* parent will exec av[2]    */
  23.         close(thepipe[1]);    /* parent doesn't write to pipe    */

  24.         if ( dup2(thepipe[0], 0) == -1 )
  25.             oops("could not redirect stdin",3);

  26.         close(thepipe[0]);    /* stdin is duped, close pipe    */
  27.         execlp( av[2], av[2], NULL);
  28.         oops(av[2], 4);
  29.     }

  30.     /*     child execs av[1] and writes into pipe            */

  31.     close(thepipe[0]);        /* child doesn't read from pipe    */

  32.     if ( dup2(thepipe[1], 1) == -1 )
  33.         oops("could not redirect stdout", 4);

  34.     close(thepipe[1]);        /* stdout is duped, close pipe    */
  35.     execlp( av[1], av[1], NULL);
  36.     oops(av[1], 5);
  37. }

可以这样运行:
#./pipe who sort
阅读(423) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~