Chinaunix首页 | 论坛 | 博客
  • 博客访问: 685749
  • 博文数量: 240
  • 博客积分: 3616
  • 博客等级: 大校
  • 技术积分: 2663
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-21 23:59
文章分类

全部博文(240)

文章存档

2013年(6)

2012年(80)

2011年(119)

2010年(35)

分类: LINUX

2010-10-25 17:57:02

#include
#include
#include
#include
#include
int main(void)
{
 pid_t child1,child2,child;
 child1 = fork();//创建两个子进程
 child2 =fork ();
 if(child ==-1)//子进程1的出错处理
 {
  perror("child fork") ;
  exit(1);
 } 
 else if(child1==0)//在子进程1中调用execlp函数
 {
  printf("in child1:execute 'ls -l'\n");
  if(execlp("ls","ls","-1",NULL)<0)
  {
   perror("child1 execlp"); 
  } 
 }
 if(child2==-1)////子进程2的出错处理
 {
  perror("child2 fork");
  exit(1); 
 }
 else if(child2==0)//在子进程2中使其睡眠5s,然后退出
 {
  printf("in child2:sleep for 5 seconds and then exit\n");
  sleep(5);
  exit(0);  
 }
 else//在父进程中等待子进程2的退出
 {
  printf("in father process:\n");
  do
  {
   child=waitpid(child2,NULL,WNOHANG);
   if(child==0)
   {
    printf("the child2 process has not exited !\n");
    sleep(1); 
   } 
   
  }
  while(child==0);
  if(child==child2)
  {
   printf("get child2\n"); 
  }
  else
  {
   printf("error occured !\n"); 
  }
   
 }
 
}
阅读(675) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-10-26 19:01:34

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com