21.6 C
Jaipur
Monday, October 18, 2021

Waitpid Syscall in C

Must read

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.

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

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.

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.

The output can be seen in the attached image.

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.

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

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.

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.

The output can be seen in the attached image.

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.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article