The waitpid() system call monitors a child of the caller process for state changes and retrieves information about the child whose behavior has changed. The child was halted by a signal or resumed by a signal regarded as a state shift. Waiting for a terminated child enables the system to free the resources associated with the child; if no wait is conducted, the terminated child will remain in a “zombie” condition.

The waitpid() system function pauses the current process until the PID argument specifies a child who has changed. The calling process is paused until a child’s process completes or is terminated. Waitpid() halts the calling process till the system receives information about the child’s status. Waitpid() returns quickly if the system already has status information on a suitable child when it is called. If the caller process gets a signal with the action of either executing a signal handler or terminating the process, waitpid() is terminated. The waitpid() function will pause the caller thread’s execution until it receives information and updates for one of its terminated child processes or a signal that will either run a signal-catching procedure or terminate the process.

Example 1

We are going to elaborate on our first example in the terminal of the Ubuntu 20.04 operating system. Create a file with the nano command in the console, as revealed below. It is completely up to you to select a file name.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image5-2.png" data-lazy- height="66" src="data:image/svg xml,” width=”725″>

You can check that file has appeared in GNU nano editor. Simply put the code shown in the attached image.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image7-1.png" data-lazy- height="453" src="data:image/svg xml,” width=”727″>

In this example, we have used for loop for incrementing the process. Fork() system call has been used. The fork() system call is used to start a new process, known as a child process, that runs parallel with the one that made the fork() call to the parent process. After fork() system call creates a new child process, all processes will perform the next instruction. It accepts no input and returns an integer value. After that, sleep function was used.

The sleep() function suspends the operation of the calling thread until the range of real-time seconds provided by the parameter seconds has passed, or a signal is given to the calling thread with the action of invoking a signal-catching function or terminating the process. We have added “1” as an argument. WIFEXITED checks the child termination status returned by the wait and waitpid() methods to see if the child process was completed successfully.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image6-2.png" data-lazy- height="203" src="data:image/svg xml,” width=”736″>

In the main function of this program, the wait() system call has been called. Once you understand this code, save and exit the file and get back to the terminal. Write the below-appended instructions for the execution of code. We have used the GCC compiler in Ubuntu 20.04 operating system.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image9.png" data-lazy- height="32" src="data:image/svg xml,” width=”729″>

The output can be seen in the attached image.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image8-1.png" data-lazy- height="115" src="data:image/svg xml,” width=”731″>

Example 2

We are going to elaborate on our second example in the terminal of the Ubuntu 20.04 operating system. Create a file with the nano command in the console as presented below. It is totally up to you to select a file name.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image2-2.png" data-lazy- height="30" src="data:image/svg xml,” width=”724″>

You can check that file has appeared in GNU nano editor. Simply put the code shown in the attached image.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image1-3.png" data-lazy- height="404" src="data:image/svg xml,” width=”732″>

In the above-displayed code, again, fork () system call has been used. The purpose of the fork () system call is to start a new process, known as a child process, that runs in parallel with the one that made the fork () call to the parent process. After fork () system call creates a new child process, all processes will perform the next instruction. It accepts no input and returns an integer value. EXIT_FAILURE means the ineffective implementation of a program. The pause () function will wait for the signals.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image4-2.png" data-lazy- height="408" src="data:image/svg xml,” width=”724″>

If a child has halted but has not been traced using ptrace() system call, WUNTRACED will return. Though this parameter is not selected, the status of traced children who have stopped is supplied. WCONTINUED return if a halted child has been restarted by SIGCONT broadcast. WEXITSTATUS(status) provides the child’s exit status. If WIFEXITED returns true, this expression should only be used. The frequency of the signal that prompted the child process to end is returned by WTERMSIG(status). If WIFSIGNALED gave the “true”, this expression should only be used.

The signal number that prompted the child to stop is returned by WSTOPSIG(status). If WIFSTOPPED returns true, this expression should only be used. WIFSIGNALED(status) returns true if a signal ended the child’s process. Once you understand this code, save and exit the file and get back to the terminal. Write the below-appended instructions for the execution of code. We have used the GCC compiler in Ubuntu 20.04 operating system.

$ gcc waitpid.c

$ ./a.out

The output can be seen in the attached image.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image3-1.png" data-lazy- height="86" src="data:image/svg xml,” width=”734″>

Conclusion

This guide was about the usage of the waitpid() system call in C programming. We have stated two examples. One of them is simpler and easy. Once you get the understanding, you can easily move to the complex one. Execute both examples on your system to make it easy for you to experience the usage of the waitpid() system call in C programming.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Akhtar

Hello, I am a freelance writer and usually write for Linux and other technology related content