Chinaunix首页 | 论坛 | 博客
  • 博客访问: 743347
  • 博文数量: 130
  • 博客积分: 2951
  • 博客等级: 少校
  • 技术积分: 1875
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-04 18:32
文章分类

全部博文(130)

文章存档

2013年(1)

2012年(129)

分类: C/C++

2012-10-29 13:15:14

fork,在子进程中重定向输入,然后执行shell程序

  1. /* create a new process or quit */
  2.     if( (pid = fork() ) == -1 )
  3.         oops("fork", 1);

  4.     /* child does the work */
  5.     if ( pid == 0 ){
  6.         /* close then open */
  7.         close(0);
  8.         if ( open(file,O_RDONLY) != 0 )
  9.             oops(file,2);
  10.         execlp( "sort", "sort", NULL );    /* and run    */
  11.         oops("sort",3);
  12.     }
  13.     /* parent waits then reports */
  14.     if ( pid != 0 ){
  15.         wait(NULL);
  16.         printf("Done running sort < %s\n", file);
  17.     }


阅读(1487) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~