Chinaunix首页 | 论坛 | 博客
  • 博客访问: 322764
  • 博文数量: 106
  • 博客积分: 3081
  • 博客等级: 中校
  • 技术积分: 1090
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-15 14:07
文章分类

全部博文(106)

文章存档

2009年(1)

2007年(34)

2006年(71)

我的朋友

分类: C/C++

2006-05-24 22:23:08

 #include
#include
#include
#include
#include
#include

前提:大家要知道父子进程关系.

 

 

int main(void)
{
    pid_t child;
    int errret;
    if((child=fork())<0) 产生一个子进程
    {
    perror("fork");
    exit(EXIT_FAILURE);
    }
   
    else if(child==0)
    {
    sleep(30);    子进程训下觉先
    }
    else
    {
    printf("sending SIGHILD to %d\n",child);
/*erret=0*/    errret=kill(child,SIGCHLD);          发一个信号给子进程(子进程挂起或已死)
    printf("the errret number is %d\n",errret);
    if(errret<0)
    perror("kill :SIGCHILD");
       

    else
    printf("%d still alive\n",child);   
    printf("killing %d\n",child);
    if((kill(child,SIGTERM))<0)呢度最后另子进程结束了
    perror("kill:SIGTERM");
   
    waitpid(child,NULL,0);  最后以避免kill失败,所以用waitpid等待子进程结束

    0参数:子进程种未结束时等待距结束
    }

    exit(EXIT_SUCCESS);

   
   
}


子进程sleep,因为要先给父进程运行先, 因为惊如果父进程先结束了,子进程会变成孤饵进程由init进程接管

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