Waiting
- This is from chapter 26. Don't worry about 26.14 or 26.3 for now.
- This figure might be helpful
-
- When a process exits, the parent process is informed.
- The simplest way to receive this message is through the wait system call
- pid_t wait(int *stat_loc);
- needs sys/wait.h
- Needs a pointer to an integer
- This will contain the exit status.
- It contains information about why the process exited.
- And the 8 bit return value of the program.
- The return value will be the process id of the child.
- or -1 on error.
- Most likely the process has no children.
- Interpreting the status variable.
- There are a set of macros set up to gain information about the process.
- These are defined in sys/wait.h
- Look at them and be in awe
- But they pick off different bits of the integer.
- USE THEM, do not write your own.
- They are :
- WIFEXITED(status) : return true if the child exited.
- WIFSIGNALED(status): return true if the child was terminated due to a signal
- WIFSTOPPED(status): return true if the child was stopped.
- WIFCONTINUED(status): return true if the child was continued
- They have corresponding information calls
- WEXITSTATUS(status) returns the exit code when WIFEXITED is true
- WTERMSIG(status) returns the signal id that killed the process if WIFSIGNALED is true
- WSTOPSIG(status) returns the signal id that suspended the process if WSTOPPED is true.
- Look at simpleWait.C
- Look at multiWait.C
- By the way, if a child has exited, the wait call will return immediately.
- A more complex call is waitpid
- pid_t waitpid(pid_t pid, int *stat_loc, int options);
- The stat_loc is the same as before
- But you can specify a specific pid to wait for.
- If pid = -1, wait for any child
- If pid > 0, wait for the specified pid.
- If pid < -1 wait for child in process group (we may discuss this later)
- Options are the real interesting part
- These are bit flags and should be combined with a bitwise or.
- WCONTINUED - report when children are continued
- WUNTRACED - report when a child process is stopped.
- WNOHANG - don't block, just check for child termination.
- If WNOHANG is specified then waitpid returns a 0 if no child has exited.
- Look at workingWait.C