Wait System Call in C

Prerequisite : Fork System call

A call to wait() blocks the calling process until one of its child processes exits or a signal is received. After child process terminates, parent continues its execution after wait system call instruction.
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.

Syntax in c language:

#include
#include

// take one argument status and returns 
// a process ID of dead children.
pid_t wait(int *stat_loc);   

If any process has more than one child processes, then after calling wait(), parent process has to be in wait state if no child terminates.



If only one child process is terminated, then return a wait() returns 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 pointer, If status are not NULL.

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

NOTE: “This codes does not run in simple IDE because of environmental problem so use terminal for run the code”

Examples:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to demonstrate working of wait()
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
  
int main()
{
    pid_t cpid;
    if (fork()== 0)
        exit(0);           /* terminate child */
    else
        cpid = wait(NULL); /* reaping parent */
    printf("Parent pid = %d\n", getpid());
    printf("Child pid = %d\n", cpid);
  
    return 0;
}

chevron_right


Output:

Parent pid = 12345678 
Child pid = 89546848 
filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to demonstrate working of wait()
#include<stdio.h>
#include<sys/wait.h>
#include<unistd.h>
  
int main()
{
    if (fork()== 0)
        printf("HC: hello from child\n");
    else
    {
        printf("HP: hello from parent\n");
        wait(NULL);
        printf("CT: child has terminated\n");
    }
  
    printf("Bye\n");
    return 0;
}

chevron_right


Output: depend on environment

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

Child status information:
Status information about the child reported by wait is more than just the exit status of the child, it also includes

  • normal/abnormal termination
  • termination cause
  • exit status

For find information about status, we use
WIF….macros



1. WIFEXITED(status): child exited normally
WEXITSTATUS(status): return code when child exits

2. WIFSIGNALED(status): child exited because a signal was not caught
WTERMSIG(status): gives the number of the terminating signal

3. WIFSTOPPED(status): child is stopped
WSTOPSIG(status): gives the number of the stop signal

/*if we want to prints information about a signal */
void psignal(unsigned sig, const char *s);

Examples:
Check output of the following program.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to demonstrate working of status
// from wait.
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
  
void waitexample()
{
    int stat;
  
    // This status 1 is reported by WEXITSTATUS
    if (fork() == 0)
        exit(1);
    else
        wait(&stat);
    if (WIFEXITED(stat))
        printf("Exit status: %d\n", WEXITSTATUS(stat));
    else if (WIFSIGNALED(stat))
        psignal(WTERMSIG(stat), "Exit signal");
}
  
// Driver code
int main()
{
    waitexample();
    return 0;
}

chevron_right


Output:

Exit status: 1              

We know if more than one child processes are terminated, then wait() reaps any arbitrarily child process but if we want to reap any specific child process, we use waitpid() function.

Syntax in c language:
pid_t waitpid (child_pid, &status, options);

Options Parameter

  • If 0 means no option parent has to wait for terminates child.
  • If WNOHANG means parent does not wait if child does not terminate just check and return waitpid().(not block parent process)
  • If child_pid is -1 then means any arbitrarily child, here waitpid() work same as wait() work.

Return value of waitpid()

  • pid of child, if child has exited
  • 0, if using WNOHANG and child hasn’t exited
filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to demonstrate waitpid()
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
  
void waitexample()
{
    int i, stat;
    pid_t pid[5];
    for (i=0; i<5; i++)
    {
        if ((pid[i] = fork()) == 0)
        {
            sleep(1);
            exit(100 + i);
        }
    }
  
    // Using waitpid() and printing exit status
    // of children.
    for (i=0; i<5; i++)
    {
        pid_t cpid = waitpid(pid[i], &stat, 0);
        if (WIFEXITED(stat))
            printf("Child %d terminated with status: %d\n",
                   cpid, WEXITSTATUS(stat));
    }
}
  
// Driver code
int main()
{
    waitexample();
    return 0;
}

chevron_right


Output:

Child 50 terminated with status: 100
Child 51 terminated with status: 101
Child 52 terminated with status: 102
Child 53 terminated with status: 103
Child 54 terminated with status: 104

Here, Children pids depend on the system but in order print all child information.

This article is contributed by Kadam Patel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.