wait() and waitpid() API

  • wait() API
  • waitpid() API
  • Sample Code
  • Difference between wait() and waitpid()

wait() API()

  • The wait() function suspends the execution of the calling process until one of its child terminates or was stopped by a signal or resumed by a signal.
  • In the case of fork() API, a child process is created which has its life cycle and runs independently.
  • If the parent process happens to terminate before the child process, the child process becomes orphan and gets re-parented by the Init process.
  • If the child process terminates and the parent process does not remove its entry is still there in the process control block(PCB), it is considered as a Zombie process.
  • Using wait(), parents process can get the exit status of the child process and can remove its entry and can avoid zombie process.
  • If the parent process calls wait() API, and the child has already terminated it returns immediately without blocking the caller, and the return value is -1.
  • If the parent process calls wait() and none of the child processes has terminated, the parent process is blocked indefinitely. Thus wait() API is a blocking call.
  • On success, it returns the process ID(PID) of the terminated child.

Syntax:

pid_t wait (int *status);

The stat_loc points to a location where the exit status of the child process will be written if it is not NULL.

This integer can be inspected using the WIFEXITED and WEXITSTATUS macros.

WIFEXITED(stat_var) : Non-zero if the child is terminated normally and

WEXITSTATUS(sta_var) : It holds exit status if WIFEXITED is non-zero.

Return Value:

  • It returns -1 in case of failure that is there is no child process.
  • It returns the PID of the child terminated if there is a child and anyone has terminated.
  • If non of the child has terminated, return nothing and the caller is blocked.

Sample Program

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
  int pid = fork();
  switch(pid)
  {
     case -1:
           printf("\n There is a failure in creating a child process");
           exit(1);
     case 0:
           printf("\n This is a child Process\n");
           break;
     default:
           printf("\n This is a parent Process\n");
           break;
   }

/* This section of program waits for the child to finish */
   if (pid !=0) /* It is parent process */
   {
        int stat_val;
        int child_pid = wait(&stat_val);
        if (child_pid == -1)
        {
           printf("\n There is no child process \n");
           exit(1);
        }
        
        if (WIFEXITED(stat_val))
            printf("\n Child exited normally with return status %d\n", WEXITSTATUS(stat_val));
        else
            printf("\n Child terminated Abnormally");
    }
  }  

waitpid()

  • The waitpid() API suspends the execution of the caller process until a child specified by PID argument has either terminated or signaled to stop or resume.
  • The call wait() is equivalent to waitpid ( -1, &status, 0).
  • By default, waitpid() waits only for the terminated child, not for the signaled child but this can be changed by the use of options.
  • The most important option is WNOHANG, which makes waitpid() a non-blocking call.

Syntax:

int waitpid( int pid, int * stat_var, int options);

Where

PID is the process id of the child to wait for termination or signaled.

The stat_loc points to a location where the exit status of the child process will be written if it is not NULL.

This integer can be inspected using the WIFEXITED and WEXITSTATUS macros.

WIFEXITED(stat_var): Non-zero if the child is terminated normally and

WEXITSTATUS(sta_var): It holds exit status if WIFEXITED is non-zero.

The most important option is WNOHANG which makes it non-blocking.

Return Value:

  • It returns -1 in the case of failure that is there is no child process.
  • It returns PID of the child terminated on success and
  • Return 0 if the child exists but not terminated or stopped(used with WNOHANG)

Pid passed to function can have the following values:

  • If the pid >0, means waits for specific process with value equal to pid.
  • If the pid is 0, waits for child whose process group id equals that of calling process.
  • If the pid is -1, wait for any child that is first terminating child.
  • If the pid <-1, waits for child process whose group id is equals to absolute value of pid.

Difference between wait and waitpid():

  • Wait() is a blocking call whereas waipid() can be made non-blocking with WNOHANG option.
  • Wait() waits for any child process but waitpid() waits for a specific child equal to pid.
  • By default waitpid() waits for the only terminated child where as wait() waits for both terminated or a signaled child.

Diag-1: wait() and waitpid()

untitled image

Related Posts:



Categories: Operating system (OS)

5 replies

Trackbacks

  1. Index of Operating System - Tech Access
  2. Join a thread: pthread_join() API - Tech Access
  3. Pipes - Tech Access
  4. Exec Family System Call - Tech Access
  5. Create a thread: pthread_create() API - Tech Access

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: