日期:2014-05-16  浏览次数:20721 次

使用wait(&status)获取子进程异常信号问题
使用wait(&status)函数等待子进程信号,我们可以通过分析status来得到子进程的状态。但是具体用法不是很清楚,所以请大家帮帮忙。
我现在就是想确定子进程是否发生异常,如果发生异常,异常的类型是什么(比如ESIGV段异常等等)?用哪些函数和宏来分析status?谢谢

------解决方案--------------------
Advanced Programming in the UNIX® Environment: Second Edition

8.6. wait and waitpid Functions
------解决方案--------------------
When a process terminates, either normally or abnormally, the kernel notifies the parent by sending the SIGCHLD signal to the parent. Because the termination of a child is an asynchronous eventit can happen at any time while the parent is runningthis signal is the asynchronous notification from the kernel to the parent. The parent can choose to ignore this signal, or it can provide a function that is called when the signal occurs: a signal handler. The default action for this signal is to be ignored. We describe these options in Chapter 10. For now, we need to be aware that a process that calls wait or waitpid can

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

If the process is calling wait because it received the SIGCHLD signal, we expect wait to return immediately. But if we call it at any random point in time, it can block.


#include <sys/wait.h>

pid_t wait(int *statloc);

pid_t waitpid(pid_t pid, int *statloc, int options);



Both return: process ID if OK, 0 (see later), or 1 on error



The differences between these two functions are as follows.

The wait function can block the caller until a child process terminates, whereas waitpid has an option that prevents it from blocking.

The waitpid function doesn 't wait for the child that terminates first; it has a number of options that control which process it waits for.

If a child has already terminated and is a zombie, wait returns immediately with that child 's status. Otherwise, it blocks the caller until a child terminates. If the caller blocks and has multiple children, wait returns when one terminates. We can always tell which child terminated, because the process ID is returned by the function.

For both functions, the argument statloc is a pointer to an integer. If this argument is not a null pointer, the termination status of the terminated process is stored in the location pointed to by the argument. If we don 't care about the termination status, we simply pass a null pointer as this argument.

Traditionally, the integer status that these two functions return has been defined by the implementation, with certain bits indicating the exit status (for a normal return), other bits indicating the signal number (for an abnormal return), one bit to indicate whether a core file was generated, and so on. POSIX.1 specifies that the termination status is to be looked at using various macros that are defined in <sys/wait.h> . Four mutually exclusive macros tell us how the process terminated, and they all begin with WIF. Based on which of these four macros is true, other macros are used to obtain the exit status, signal number, and the like. The four mutually-exclusive macros are shown in Figure 8.4.

We 'll discuss how a process can be stopped in Section 9.8 when we discuss job control.

------解决方案--------------------
8.5. exit Functions


Figure 8.4. Macros to examine the termination status returned by wait and waitpid Macro
Description

WIFEXITED(s