Wait and Waitpid
Wait and Waitpid
AIM: Write a C program to block a parent process before termination of child process using
wait and waitpid.
THEORY:
pid_t data type represents process IDs. Get the process ID of a process by calling getpid.
The wait() system call suspends execution of the calling process until one of its children
terminates.
The syntax of waitpid: pid_t waitpid (child_pid, &status, options);
The waitpid() system call suspends execution of the calling process until a child specified
by pid argument has changed state. By default, waitpid() waits only for terminated
children, but this behavior is modifiable via the options argument.
The interpretation of the pid argument for waitpid depends on its value:
Options Parameter
WNOHANG: Return immediately if no child has exited.
WIFEXITED: Returns true if the child terminated normally by returning
from main().
WEXITSTATUS: Returns the exit status of the child. It employs only if
WIFEXITED returned true.
WIFSIGNALED: Returns true if the child process was terminated by a
signal.
CONCLUSION: The waitpid wait for one particular process, whereas the wait function returns
the status of any terminated child process. Hence blocked a parent process before terminating the
chid process using wait() and waitpid() system call.
PROGRAM:
#define _POSIX_SOURCE
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
main() {
pid_t pid;
time_t t;
int status;
OUTPUT:-