What is the wait() system call?
A system call is referred to as a low-level function that is used to communicate with the operating system kernel. They are used in programs that perform tasks that require elevated privileges or need to communicate with the underlying hardware via the kernel.
The wait() system call
This system call is used in processes that have a parent-child relationship. It makes a parent process stop its execution till the termination of the child process. We can create a child process using the fork() system call.
Syntax
Below, we can see the syntax for the wait() system call. To use this in our C programs, we will have to include the Standard C library sys/wait.h.
pid_t wait(int *status);
Parameters and return value
The system call takes one argument named status. This argument represents a pointer to an integer that will store the exit status of the terminated child program.
When the system call completes, it can return the following.
Process ID: The process ID of the child process that is terminated, which has the object type
pid_t.Error value: If there is any error during system call execution, it will return
-1, which can be used for error handling.
Example C program
Now that we have understood the syntax of the wait() system call, we will look at the C program below that demonstrates how we can use this system call.
The program creates a child process via the fork() system call and then calls the wait() system call to wait for the child process to finish its execution.
Upon termination of the child process, the parent process prints the pid of the child process and its exit status using the macro function WEXITSTATUS().
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
// create a child process
int child = fork();
int exitStatus;
int childPid;
// code for the child process
if(child == 0){
// display running message and make child sleep
printf("Child: I am running!!\n\n");
printf("Child: I have PID: %d\n\n", getpid());
sleep(4);
// child exits with code 100
exit(100);
} // code for the parent process
else{
// print parent running message
printf("Parent: I am running and waiting for child to finish!!\n\n");
// call wait system call
childPid = wait(&exitStatus);
// print the details of the child process
printf("Parent: Child finished execution!, It had the PID: %d, Exit Status: %d\n\n", childPid, WEXITSTATUS(exitStatus));
}
return 0;
}Code explanation
Line 8: We create a child process via the
fork()system call.Lines 13–20: Here, we write the code for the child process.
Line 16: We print the child's process ID using the
getpid()function.Line 17: We make the child process sleep for 4 seconds via the
sleep()function.Line 19: We exit the child process with the exit status of 100 via the
exit()function.Lines 21–28: Here, we write the code for the parent process.
Line 25: We call the
wait()function, so the parent process waits for the child process to finish, and we pass an integer pointer namedexitStatusthat stores the exit code returned via theexit()function. It returns the process id of the terminated child process, which we store in an integer variablechildPid.Line 27: We print the details of the child process that we retrieved via the
wait()system call.
Exercise
Now that we have understood the wait() system call and its use in C programs, let's attempt the quiz below.
Quiz
What is the header file to include when using the wait() system call?
stdio.h
stdlib.h
sys/wait.h
unistd.h
Free Resources