lab4 Process
  • Introduction
  • Knowing about process
    • Checking System Process
    • Process Identification
  • Let's Fork! Process Creation
    • Background of Process Creation
    • fork system call
    • Distinguish Parent and Child Process
    • Exercises
  • Process Execution
    • First exec example
    • Environment Variables
    • exec Family
    • exec Family: execlp
    • exec Family: execle
    • exec Family: execv
    • exec Family: execvp
    • exec Family: execve
    • exec error handling
  • Wait!
    • Problem 1: Without Wait?
    • Sleep
    • Problem 2: Zombies!
    • Wait System Call
    • waitpid
Powered by GitBook
On this page

Was this helpful?

  1. Wait!

waitpid

waitpid() is an advanced version of wait() system call. You can specify more in the parameters.

pid_t waitpid(pid_t pid, int *status, int options);

Take a note of the following code to see how it works:

/* Wait/waitpid.c */
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

int main(int argc,char *argv[])
{
    int pid;
    int status;
    if(!(pid = fork()))
    {
        printf("My PID: %d\n",getpid()); 
        exit(0); 
    }
    waitpid(pid,&status,WUNTRACED);
    if(WIFEXITED(status)) 
    { 
        printf("Exit Normally\n");
        printf("Exit status: %d\n",WEXITSTATUS(status)); 
    } 
    else 
    { 
        printf("Exit NOT Normal\n"); 
    } 
    return 0;
}

waitpid() takes 3 parameters. The first one is child pid, therefore this is more specific than the previous wait().

The second one stores the status of the child. Whenever the child terminates/suspends, it will store the status code in the middle parameter. Note that it accepts a pointer only.

The third is the options for waitpid(). You can change various behavior, for example, returns immediately if no child has exited: WNOHANG; also returns if the child is stopped (e.g.: by Ctrl-Z): WUNTRACED.

There are macros to further tell you more information about the child. You can use WIFEXITED(status) to check if the child is returned normally (i.e. by return or exit()). In addition, you even can know the exit code (Only least significant 8 bits of status code are available to parents) by using WEXITSTATUS(status).

PreviousWait System Call

Last updated 4 months ago

Was this helpful?