Linux进程实践(4) --wait避免僵尸进程

来源:互联网 发布:淘宝古琴哪家好 编辑:程序博客网 时间:2024/05/22 00:36

Wait的背景

   当子进程退出的时候,内核会向父进程发送SIGCHLD信号,子进程的退出是个异步事件(子进程可以在父进程运行的任何时刻终止)

   子进程退出时,内核将子进程置为僵尸状态,这个进程称为僵尸进程,它只保留最小的一些内核数据结构,以便父进程查询子进程的退出状态

   父进程查询子进程的退出状态可以用wait/waitpid函数

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <sys/types.h>  
  2. #include <sys/wait.h>  
  3. pid_t wait(int *status);  
  4. pid_t waitpid(pid_t pid, int *status, int options);  

Wait

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. pid_t wait(int *status);  

  当我们用fork启动一个进程时,子进程就有了自己的生命,并将独立地运行。有时,我们需要知道某个子进程是否已经结束了,这样我们可以通过wait安排父进程在子进程结束之后。

  status:该参数可以获得你等待子进程的信息

返回值:

  on success, returns the process ID of the terminated child;  on  error,  -1  is returned.

特征:

  1.wait系统调用会使父进程暂停执行,直到它的任意一个(并不是所有的)子进程结束为止。

  2.返回的是子进程的PID,它通常是已经结束了的子进程;

  3.状态信息允许父进程判定子进程的退出状态,即从子进程的main函数返回的值或子进程中exit/_exit语句的退出码。

  4.如果status不是一个空指针,状态信息将被写入它指向的位置

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //示例  
  2. int main()  
  3. {  
  4.     pid_t pid = fork();  
  5.     if (pid == -1)  
  6.         err_exit("fork");  
  7.     else if (pid == 0)  
  8.     {  
  9.         cout << "In Child, pid = " << getpid() << endl;  
  10.         sleep(5);  
  11.         exit(10);  
  12.     }  
  13.   
  14.     int status;  
  15.     int returnPid = wait(&status);  
  16.     //两个pid的值相同,但是status的值根本不是10  
  17.     cout << "In Parent, returnPid = " << returnPid  
  18.          << ", status = " << status << endl;  
  19. }  


Wait获取status

宏定义

描述

WIFEXITED(status)

     WEXITSTATUS(status)

如果子进程正常结束,返回一个非零值

    如果WIFEXITED非零,返回子进程退出码

WIFSIGNALED(status)

     WTERMSIG(status)

子进程因为捕获信号而终止,返回非零值

    如果WIFSIGNALED非零,返回信号代码

WIFSTOPPED(status)

     WSTOPSIG(status)

如果子进程被暂停,返回一个非零值

    如果WIFSTOPPED非零,返回一个信号代码

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //示例  
  2. void printStatus(int status)  
  3. {  
  4.     if (WIFEXITED(status))  
  5.     {  
  6.         cout << "normal termination, exit status = " << WEXITSTATUS(status) << endl;  
  7.     }  
  8.     else if (WIFSIGNALED(status))  
  9.     {  
  10.         cout << "abnormal termination, signal number = " << WTERMSIG(status);  
  11. #ifdef WCOREDUMP  
  12.         if (WCOREDUMP(status))  
  13.             cout << " (core file generated)" << endl;  
  14. #else  
  15.         cout << endl;  
  16. #endif // WCOREDUMP  
  17.     }  
  18.     else if (WIFSTOPPED(status))  
  19.     {  
  20.         cout << "child stopped, status number = " << WSTOPSIG(status) << endl;  
  21.     }  
  22.     else  
  23.     {  
  24.         cout << "Unknow Stop!" << endl;  
  25.     }  
  26. }  
  27.   
  28. //测试代码  
  29. int main()  
  30. {  
  31.     pid_t pid = fork();  
  32.     if (pid == -1)  
  33.         err_exit("fork");  
  34.     else if (pid == 0)  
  35.         exit(7);  
  36.   
  37.     int status;  
  38.     if (wait(&status) == -1)  
  39.         err_exit("first wait error");  
  40.     printStatus(status);  
  41.   
  42.     pid = fork();  
  43.     if (pid == -1)  
  44.         err_exit("fork");  
  45.     else if (pid == 0)  
  46.         abort();  
  47.   
  48.     if (wait(&status) == -1)  
  49.         err_exit("first wait error");  
  50.     printStatus(status);  
  51.   
  52.     pid = fork();  
  53.     if (pid == -1)  
  54.         err_exit("fork");  
  55.     else if (pid == 0)  
  56.         status /= 0;  
  57.   
  58.     if (wait(&status) == -1)  
  59.         err_exit("first wait error");  
  60.     printStatus(status);  
  61.   
  62.     return 0;  
  63. }  

查看信号值


Waitpid

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. pid_t waitpid(pid_t pid, int *status,int options)  

等待某个特定进程的结束

参数:

Pid:The value of pid can be:

  <-1    meaning wait for any child process whose process group ID is equal to the absolute value of pid.

  -1      meaning wait for any child process(任一子进程).

   0       meaning wait for any child process whose process group ID is equal to that of  the calling process(与调用者进程同在一个组的进程).

  >0     meaning wait for the child whose process ID is equal to the value of pid.

status:如果不是空,会把状态信息写到它指向的位置(同wait)

options:允许改变waitpid的行为,最有用的一个选项是WNOHANG,它的作用是防止waitpid把调用者的执行挂起

返回值:

  如果成功返回等待子进程的ID,失败返回-1

 

wait与waitpid的区别:

  1.在一个子进程终止前, wait 使其调用者阻塞,而waitpid 有一选择项,可使调用者不阻塞。

  2.waitpid并不等待第一个终止的子进程:它有若干个选择项,可以控制它所等待的特定进程。

  3.wait函数相当于是waitpid函数的一个特例。

waitpid(-1, &status, 0);

 

僵尸进程(如果不等待...)

  当一个子进程结束运行时,它与其父进程之间的关联还会保持到父进程也正常地结束运行或者父进程调用了wait才告终止。

  进程表中代表子进程的数据项是不会立刻释放的,虽然不再活跃了,可子进程还停留在系统里,因为它的退出码还需要保存起来以备父进程中后续的wait调用使用。它将称为一个“僵尸进程”

 

如何避免僵尸进程

  方法1:调用wait或者waitpid函数查询子进程退出状态。

  方法2:如果不想让父进程挂起,可以在父进程中加入一条语句:signal(SIGCHLD,SIG_IGN);表示父进程忽略SIGCHLD信号,该信号是子进程退出的时候向父进程发送的(表明父进程忽略SIGCHLD信号,一切让Linux内核管理)。


-利用man手册,减少开发难度,提高开发技能

如:Man 7 signal 查找有什么信号可以使应用程序暂停



0 0
原创粉丝点击