天天看点

6进程原语:wait()和waitpid()函数



1 wait/waitpid

僵尸进程:

子进程退出,父进程没有回收子进程资源(pcb),则子进程变成僵尸进程

孤儿进程:

父进程先于子进程结束,则子进程成为孤儿进程,子进程的父进程成为1号

进程init进程,称为init进程领养孤儿进程

2依赖的头文件

#include <sys/types.h>

#include <sys/wait.h>

3函数声明

pid_t wait(int *status);

pid_t waitpid(pid_t pid, int *status, intoptions);

the value of pid can be:

      < -1  

meaning wait for anychild process whose  process 

group id  is

equal to the absolute value ofpid.

      -1    

meaning wait for any childprocess.

meaning  wait for 

any  child process whose process group id is

equal to that of the callingprocess.

      > 0   

meaning wait for thechild whose process  id 

is equal  to 

the

value of pid.

< -1

回收指定进程组内的任意子进程

-1

回收任意子进程

回收和当前调用waitpid一个组的所有子进程

> 0

回收指定id的子进程

3.说明:

一个进程在终止时会关闭所有文件描述符,释放在用户空间分配的内存,但它的pcb还

保留着,内核在其中保存了一些信息:如果是正常终止则保存着退出状态,如果是异常终止

则保存着导致该进程终止的信号是哪个。这个进程的父进程可以调用wait或waitpid获取这

些信息,然后彻底清除掉这个进程。我们知道一个进程的退出状态可以在shell中用特殊变

量$?查看,因为shell是它的父进程,当它终止时shell调用wait或waitpid得到它的退出状

态同时彻底清除掉这个进程。

如果一个进程已经终止,但是它的父进程尚未调用wait或waitpid对它进行清理,这时

的进程状态称为僵尸(zombie)进程。任何进程在刚终止时都是僵尸进程,正常情况下,僵

尸进程都立刻被父进程清理了,为了观察到僵尸进程,我们自己写一个不正常的程序,父进

程fork出子进程,子进程终止,而父进程既不终止也不调用wait清理子进程:

#include <unistd.h>

#include <stdlib.h>

int main(void)

{

pid_t pid=fork();

if(pid<0) {

perror("fork");

exit(1);

}

if(pid>0) { /* parent */

while(1);

/* child */

return 0;

若调用成功则返回清理掉的子进程id,若调用出错则返回-1。父进程调用wait或

waitpid时可能会:

*

阻塞(如果它的所有子进程都还在运行)。

带子进程的终止信息立即返回(如果一个子进程已终止,正等待父进程读取其终止信

息)。

出错立即返回(如果它没有任何子进程)。

这两个函数的区别是:

如果父进程的所有子进程都还在运行,调用wait将使父进程阻塞,而调用waitpid时如果在options参数中指定wnohang可以使父进程不阻塞而立即返回0。

* wait等待第一个终止的子进程,而waitpid可以通过pid参数指定等待哪一个子进程。

可见,调用wait和waitpid不仅可以获得子进程的终止信息,还可以使父进程阻塞等待子进

程终止,起到进程间同步的作用。如果参数status不是空指针,则子进程的终止信息通过

这个参数传出,如果只是为了同步而不关心子进程的终止信息,可以将status参数指定为

null。

例waitpid

6进程原语:wait()和waitpid()函数

the value 

of  options is an or of zero or more of the following con‐

      stants:

      wnohang    

return immediately ifno child has exited.

      wuntraced  

also return if a childhas  stopped 

(but not  traced 

via

ptrace(2)).   status for traced children which havestopped

is provided even if thisoption is not specified.

      wcontinued (since linux 2.6.10)

also return if a stoppedchild has been resumed by delivery

of sigcont.

      (for linux-only options, see below.)

if status is not null, wait() and waitpid()store status information in

      the int to which it points. 

thisinteger can  be  inspected with 

      following 

macros  (which take the integer itself as anargument, not a

      pointer to it, as is done in wait() and waitpid()!):

      wifexited(status)

returns true if the childterminated normally, that is, by call‐

ing exit(3) or _exit(2), or byreturning from main().

      wexitstatus(status)

returns  the exit 

status  of  thechild. 

this consists of the

least significant 8 bits of thestatus argument that 

the  child

specified  in a 

call to exit(3) or _exit(2) oras the argument

for a return statement inmain().  this macro should be employed

only if wifexited returned true.

      wifsignaled(status)

returns true if the child processwas terminated by a signal.

      wtermsig(status)

returns  the number of the signal that caused the child process

to terminate.  this macro should be employed only ifwifsignaled

returned true.

wcoredump(status)

         returns  true if 

the  child produced a core dump. 

this macro

should be employed only  if wifsignaled 

returned  true.  this

macro  is  notspecified in posix.1-2001 and is not available on

some unix implementations (e.g.,aix,  sunos).  

only use  this

enclosed in #ifdef wcoredump ...#endif.

      wifstopped(status)

returns  true if the child process was stopped by delivery of a

signal; this is possible only ifthe call was  done 

using wun‐

traced or when the child is beingtraced (see ptrace(2)).

      wstopsig(status)

returns the number of the signalwhich caused the child to stop.

this macro should be employedonly if wifstopped returned true.

      wifcontinued(status)

(since linux 2.6.10) returns  true if 

the  child process 

was

resumed by delivery of sigcont.

      #include <stdlib.h>

      #include <unistd.h>

      #include <stdio.h>

      int

      main(int argc, char *argv[])

      {

          pid_t cpid, w;

          int status;

          cpid = fork();

          if (cpid == -1) {

exit(exit_failure);

          }

          if (cpid == 0) {           

/*code executed by child */

printf("child pid is%ld\n", (long) getpid());

if (argc == 1)

pause();                    /* wait for signals */

_exit(atoi(argv[1]));

} else {                   

/*code executed by parent */

do {

w = waitpid(cpid,&status, wuntraced | wcontinued);

if (w == -1) {

perror("waitpid");

if (wifexited(status)) {

printf("exited,status=%d\n", wexitstatus(status));

} else if(wifsignaled(status)) {

                  printf("killed by signal%d\n", wtermsig(status));

} else if(wifstopped(status)) {

printf("stopped bysignal %d\n", wstopsig(status));

} else if(wifcontinued(status)) {

                      printf("continued\n");

} while (!wifexited(status)&& !wifsignaled(status));

exit(exit_success);

      }