Three More Things
- Wait
- This is a family of functions to let a parent know when a child process exited.
- This is an extremely primitive form of signal handling.
- When a child process exits, a signal (SIGCHLD) is sent to the parent.
- By default processes ignore SIGCHLD
- SIGCHLD
- The child terminated
- The child was stopped (SIGSTOP) (ctrl-z in shell)
- The cild was continued. (SIGCONT)
- run zombie.c and top -u dbennett
- sys/types.h and sys/wait.h
- pid_t wait(int *wstatus);
- This waits for any child to exit.
- The pid of the child process is returned
- And information about the exit is stored in status.
- pid_t waitpid(pid_t pid, int *wstatus, int options);
- Waits for a specific pid, or set of pids
- the pid argument is
- Positive, then wait for that pid
- 0 wait for any child pid.
- negative: wait for pids in a process group.
- The options argument is
- WNOHANG - non blocking, only check to see if a child process has exited.
- WCONTINUED - check for continued children.
- For both, status is an int and can be interpreted by the followign macros
- WIFEXITED(wstatus) : returns true for normal termination
- WEXITSTATUS(wstatus): returns the least significant 8 bits of the exit value.
- WIFSIGNALED(wstatus): returns true if the child was terminated with a signal.
- WTERMSIG(wstatus): The signal that caused the process to exit.
- WIFSTOPPED(wstatus): true if stopped
- WIFCONTINUED(wstatus): true if continued
- dup
- A family of functions which duplicate an open file descriptor
- unistd.h
- int dup(int oldfd);
- int dup2(int oldfd, int newfd);
- dup creates a copy of the given file descriptor at the next available location.
- dup2 will close newfd if necessary and open duplicate the file descriptor there.
- return -1 on failure and the new file descriptor on success.
- Why DUP? - pipe
- unistd.h
- int pipe(int pipefd[2]);
- Creates a unidirectional data channel
- On success a 0, error -1.
- pipefd[0] is the read end of the pipe.
- pipefd[1] is the write end of the pipe.
- Look at cppIOTest.C
- Look at dupTest.C