Wait System Call in C
Prerequisite : Fork System call
A call to wait() blocks the calling process until one of its child processes exits or a signal is received. After child process terminates, parent continues its execution after wait system call instruction.
Child process may terminate due to any of these:
- It calls exit();
- It returns (an int) from main
- It receives a signal (from the OS or another process) whose default action is to terminate.
Syntax in c language:
#include #include // take one argument status and returns // a process ID of dead children. pid_t wait(int *stat_loc);
If any process has more than one child processes, then after calling wait(), parent process has to be in wait state if no child terminates.
If only one child process is terminated, then return a wait() returns process ID of the terminated child process.
If more than one child processes are terminated than wait() reap any arbitrarily child and return a process ID of that child process.
When wait() returns they also define exit status (which tells our, a process why terminated) via pointer, If status are not NULL.
If any process has no child process then wait() returns immediately “-1”.
NOTE: “This codes does not run in simple IDE because of environmental problem so use terminal for run the code”
Parent pid = 12345678 Child pid = 89546848
Output: depend on environment
HC: hello from child Bye HP: hello from parent CT: child has terminated (or) HP: hello from parent HC: hello from child HC: Bye CT: child has terminated // this sentence does // not print before HC // because of wait. Bye
Child status information:
Status information about the child reported by wait is more than just the exit status of the child, it also includes
- normal/abnormal termination
- termination cause
- exit status
For find information about status, we use
1. WIFEXITED(status): child exited normally
• WEXITSTATUS(status): return code when child exits
2. WIFSIGNALED(status): child exited because a signal was not caught
• WTERMSIG(status): gives the number of the terminating signal
3. WIFSTOPPED(status): child is stopped
• WSTOPSIG(status): gives the number of the stop signal
/*if we want to prints information about a signal */ void psignal(unsigned sig, const char *s);
Check output of the following program.
Exit status: 1
We know if more than one child processes are terminated, then wait() reaps any arbitrarily child process but if we want to reap any specific child process, we use waitpid() function.
Syntax in c language:
pid_t waitpid (child_pid, &status, options);
- If 0 means no option parent has to wait for terminates child.
- If WNOHANG means parent does not wait if child does not terminate just check and return waitpid().(not block parent process)
- If child_pid is -1 then means any arbitrarily child, here waitpid() work same as wait() work.
Return value of waitpid()
- pid of child, if child has exited
- 0, if using WNOHANG and child hasn’t exited.
Child 50 terminated with status: 100 Child 51 terminated with status: 101 Child 52 terminated with status: 102 Child 53 terminated with status: 103 Child 54 terminated with status: 104
Here, Children pids depend on the system but in order print all child information.
This article is contributed by Kadam Patel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.