Wait ( ) System Call in C Program with Examples

Wait System Call in C

A call to wait() blocks the calling process until one of its child processes exits or a signal is received. After the child process terminates, the parent continues its execution after wait system call instruction.
The child process may terminate due to any of these:

  • It calls exit();
  • It returns (an int) from main
  • It receives a signal (from the OS or another process) whose default action is to terminate.
  • If any process has more than one child process, then after calling wait(), the parent process has to be in a wait state if no child terminates.
  • If only one child process is terminated, then return a wait() returns the process ID of the terminated child process.
  • If more than one child processes are terminated than wait() reap any arbitrarily child and return a process ID of that child process.
  • When wait() returns they also define exit status (which tells our, a process why terminated) via a pointer, If status are not NULL.

If any process has no child process then wait() returns immediately “-1”.


// C program to demonstrate working of wait() 
int main() 
    if (fork()== 0) 
        printf("HC: hello from child\n"); 
        printf("HP: hello from parent\n"); 
        printf("CT: child has terminated\n"); 
    return 0; 

Output: (Depend on Environment)

HC: hello from child
HP: hello from parent
CT: child has terminated
HP: hello from parent
HC: hello from child
CT: child has terminated    // this sentence does 
                            // not print before HC 
                            // because of wait.

Leave a Comment