设为首页 加入收藏

TOP

C小程序 - pipe与fork结合
2012-11-01 09:15:43 】 浏览:621
Tags:程序   pipe fork 结合
  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<unistd.h>
  
  int execlp(const char * file,const char * arg,…,(char *)0);
  
  execlp()会从PATH 环境变量所指的目录中查找符合参数file的文件名,找到后便执行该文件,然后将第二个以后的参数当做该文件的argv[0]、argv[1]……,最后一个参数必须用空指针(NULL)作结束。如果用常数0来表示一个空指针,则必须将它强制转换为一个字符指针,否则将它解释为整形参数,如果一个整形数的长度与char * 的长度不同,那么exec函数的实际参数就将出错。如果调用成功,进程自己的执行代码就会变成加载程序的代码,execlp()后边的代码也就不会执行了。
  
  #include    <stdio.h>
  
  #include    <unistd.h>
  
  #define    oops(m,x)    { perror(m); exit(x); }
  
  main(int ac, char **av)
  
  {
  
  int    thepipe[2],            /* two file descriptors    */
  
  newfd,                /* useful for pipes    */
  
  pid;                /* and the pid        */
  
  if ( ac != 3 ){
  
  fprintf(stderr, “usage: pipe cmd1 cmd2\n”);
  
  exit(1);
  
  }
  
  if ( pipe( thepipe ) == -1 )        /* get a pipe        */
  
  oops(“Cannot get a pipe”, 1);
  
  /* ------------------------------------------------------------ */
  
  /*    now we have a pipe, now let's get two processes        */
  
  if ( (pid = fork()) == -1 )            /* get a proc    */
  
  oops(“Cannot fork”, 2);
  
  /* ------------------------------------------------------------ */
  
  /*     Right Here, there are two processes            */
  
  /* parent will read from pipe            */
  
  if ( pid > 0 ){            /* parent will exec av[2]    */
  
  close(thepipe[1]);    /* parent doesn't write to pipe    */
  
  if ( dup2(thepipe[0], 0) == -1 )
  
  oops(“could not redirect stdin”,3);
  
  close(thepipe[0]);    /* stdin is duped, close pipe    */
  
  execlp( av[2], av[2], NULL);
  
  oops(av[2], 4);
  
  }
  
  /*     child execs av[1] and writes into pipe            */
  
  close(thepipe[0]);        /* child doesn't read from pipe    */
  
  if ( dup2(thepipe[1], 1) == -1 )
  
  oops(“could not redirect stdout”, 4);
  
  close(thepipe[1]);        /* stdout is duped, close pipe    */
  
  execlp( av[1], av[1], NULL);
  
  oops(av[1], 5);
  
  }
  
  可以这样运行:
  
  #./pipe who sort
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C语言中变量定义的位置 下一篇pragma pack(n)用法

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目