When a process develops a child process, it’s occasionally important for the parent process to wait till the child has completed it before continuing. This is exactly what the wait () system function accomplishes.
Waiting causes the parent to wait for the child to alter its state. The status change could be due to the child process being terminated, stopped by a signal, or resumed by a signal. In some circumstances, when a child process quits or switches state, the parent process should be notified of the child’s alteration in state or termination state. In that instance, the parent process uses functions like wait () to inquire about the update in the state of the child process.
Wait () suspends the caller process till the system receives information on the ending child’s status. Wait () returns instantly if the system already has status information on a finished child process when invoked. If the caller process receives the signal with action to run a signal handler or terminate the process, wait () is also terminated.
The waitpid () system function pauses the current process until the pid argument specifies a child with an altered state. Waitpid() waits solely for terminated children by default; however, this behavior can be changed. The wait () system call accepts just one parameter, which holds the process’s information and updates. If you don’t care about the child process’s exit status and only care about making the parent wait for the child, use NULL as the value. In this guide, we will elaborate on an example for the understanding of the Wait () system call in C programming.
Pre-requisites
- Any operating system of your choice. We are using Ubuntu 20.04 Linux operating system.
- Root user access
- GCC compiler installed and configured
Example to elaborate wait system call in C
Open the terminal, and check if the GCC compiler is installed or not. If it is not installed, utilized the appended command:
On Ubuntu 20.04 system, you must first create a new file with the .c extension. To accomplish this, go to your Home Directory and generate a new empty file titled “wait1”. You can also use the nano command on your terminal to make it.
After creating the “wait1.c” file, it will open in a GNU Text Editor on your Linux system and paste the code below into it. After that, save and exit the program.
The C/C++ header file <unistd.h> is your code’s entry point to the POSIX OS API’s many constant, type, and function declarations. Several fundamental derived types are included in the <sys/types.h> header and should be utilized wherever possible. <stdio.h> is a header file that comprises the information needed to include input/output routines in our program. The symbolic constants to be used with waitpid are defined in the <sys/wait.h>. We have also used the fork system call in the program.
The fork () system call is used to start a new process, known as a child process, that runs in parallel with the fork () calling process that is the parent process. Both processes will implement the next instruction after the fork () system call when a new child process has been established. A child process shares the same pc “program counter”, CPU registers, and open files as its parent. It returns an integer value with no parameters. The process starts with the printing of “before the fork.” Then, using the fork () system call, a child process is created.
The wait () system call is introduced to the code’s parent section. As a result, the parent process is suspended as soon as the processor begins executing the parent because the initial statement is waiting (NULL). As a result, the child process runs firstly, and all of the output lines pertain to the child process. The NULL in wait () system call indicates that we will not know about the state of the child’s process’s transition. Now again, start your terminal. In Ubuntu 20.04, use the GCC instruction below, preceded by the filename.
Now run the code with the help of the following affixed command in the terminal.
As it is already explained, the parent and child id of the processes will be displayed with the help of a wait system call.
Conclusion
This article demonstrated the usage of the wait () system call in C programming. We have implemented one of the examples to show the successful implementation of the wait () system call. The whole code is also described in detail for user understanding. I hope you will easily implement and use the wait () system call whenever it is required.