Chinaunix首页 | 论坛 | 博客
  • 博客访问: 986219
  • 博文数量: 200
  • 博客积分: 5011
  • 博客等级: 大校
  • 技术积分: 2479
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-27 15:07
文章分类

全部博文(200)

文章存档

2009年(12)

2008年(190)

我的朋友

分类:

2008-11-26 17:58:55

8.6 wait and waitpid function

1.子进程停止回向父进程发送SIGCHLD信号

2.父进程调用wait会有如下情况:

·         Block, if all of its children are still running

·         Return immediately with the termination status of a child, if a child has terminated and is waiting for its termination status to be fetched

·         Return immediately with an error, if it doesn't have any child processes

3.一般如果如进程收到了SIGCHLD信号后再去wait,一般都会立刻返回,否则就要block

4.如果子进程先结束了,父进程后wait,也可以得到结束status

5Waitpid函数与wait函数的区别:

       (1) waitpid可以等待单独一个process, 具体是何种pid,可以看接口定义

       (2)waitpid可以不必block

       (3)waitpid可以支持job control,因为他不但可以等待到进程的结束,还可以等待到进程被stopped或者被continued

 

6.提出了一种建立一个进程,我们不用对其进行wait,而且他不会成为僵尸进程的方法,我觉得这个方法没啥意思,可能在特定场合会有用。即,fork一个子进程A,然后A立马再fork一个子进程B,然后A立刻结束。B就成了init的孩子。当然原始父进程要wait操作在A进程上,否则A会成为僵尸进程。这里其实还是进行了一个wait操作。如下:

Figure 8.8. Avoid zombie processes by calling fork twice
#include "apue.h"
#include 
 
int
main(void)
{
    pid_t   pid;
 
    if ((pid = fork()) < 0) {
        err_sys("fork error");
    } else if (pid == 0) {     /* first child */
        if ((pid = fork()) < 0)
            err_sys("fork error");
        else if (pid > 0)
            exit(0);    /* parent from second fork == first child */
        /*
         * We're the second child; our parent becomes init as soon
         * as our real parent calls exit() in the statement above.
         * Here's where we'd continue executing, knowing that when
         * we're done, init will reap our status.
         */
        sleep(2);
        printf("second child, parent pid = %d\n", getppid());
        exit(0);
    }
    
    if (waitpid(pid, NULL, 0) != pid)  /* wait for first child */
        err_sys("waitpid error");
 
    /*
     * We're the parent (the original process); we continue executing,
     * knowing that we're not the parent of the second child.
     */
    exit(0);
}

结果:

$ ./a.out
$ second child, parent pid = 1

Pid=1就是init进程。

 

72个等待函数只能被父进程用来等待子进程

82个等待函数一旦就收到了某个进程的status信息,就会将其破坏。

 

8.7 waitid function

Waitid简直可以说是在waitpid的基础上的在扩展,增加了灵活性和易用性。它属于The XSI extension of the Single UNIX Specification

#include 
 
int waitid(idtype_t idtype, id_t id, siginfo_t
 *infop, int options);

 

Figure 8.9. The idtype constants for waitid

Constant

Description

P_PID

Wait for a particular process: id contains the process ID of the child to wait for.

P_PGID

Wait for any child process in a particular process group: id contains the process group ID of the children to wait for.

P_ALL

Wait for any child process: id is ignored.

 

Figure 8.10. The options constants for waitid

Constant

Description

WCONTINUED

Wait for a process that has previously stopped and has been continued, and whose status has not yet been reported.

WEXITED

Wait for processes that have exited.

WNOHANG

Return immediately instead of blocking if there is no child exit status available.

WNOWAIT

Don't destroy the child exit status. The child's exit status can be retrieved by a subsequent call to wait, waitid,or waitpid.

WSTOPPED

Wait for a process that has stopped and whose status has not yet been reported.

 

可见,多了一个WNOWAIT,它允许我们获取了一个子进程的status后,让系统依然将其保留,那么下次还可以再此获取。

8.8 wait3 and wait4 functions

在以前的waitpid等函数的基础上增加了可以获取进程及其子进程所占用的resources的情况的功能。

阅读(897) | 评论(0) | 转发(0) |
0

上一篇:8.4 vfork function

下一篇:8.9 race condition

给主人留下些什么吧!~~