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!

Wait System Call

pid_t wait(int *status);

In order to suspend the parent and handle the child properly, the wait system call is necessary.

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

int main(int argc,char *argv[])
{
    while(1)    
    {
        printf("Press Enter to execute ls");
        while(getchar() != '\n');
        if(!fork())
        { 
            execl("/bin/ls","ls",NULL); 
        } 
        else 
        { 
           wait(NULL); 
        } 
   }
    return 0;
}

wait() takes one parameter for storing the status of the child process, for example, whether it is terminated or suspended. If you are not interested, just put NULL as the parameter.

PreviousProblem 2: Zombies!Nextwaitpid

Last updated 4 months ago

Was this helpful?

This is a similar program in . However, this can get rid of zombies!

Problem 2