Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1695621
  • 博文数量: 362
  • 博客积分: 10587
  • 博客等级: 上将
  • 技术积分: 4098
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-10 18:15
文章分类

全部博文(362)

文章存档

2014年(1)

2013年(58)

2011年(115)

2010年(112)

2009年(76)

分类: C/C++

2010-01-14 16:43:26

#include
#include
#include
#include
int main()
{
 int n;
  int exit_code;
 char *message;
 pid_t new_pid;
 new_pid = fork();
 
 printf("fork is starting...............\n");
 switch(new_pid)
 {
  case -1: /* Error */
   perror("fork failed.\n");
   exit(1);
  
  case 0: /* This is child */
   message = "This is a child thread.";
   n = 3;
    exit_code = 37;
   break;
  
  default: /* This is parent */
   message = "This is a parent thread.";
   n = 5;
    exit_code = 0;
   break;
 }
 
 for(;n>0;n--)
 {
  puts(message);
  sleep(1);
 }
 
  
 if(new_pid !=0)
 {
  int stat_val;
  pid_t child_pid;
  
  child_pid = wait(&stat_val);
  printf("Child has finished: PID = %d\n",child_pid);
  if(WIFEXITED(stat_val))
   printf("Child exited with code %d\n",WEXITSTATUS(stat_val));
  else
   printf("Child terminated abnormally\n");
  
 }
 exit(exit_code);
  
 
}
阅读(1053) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~