C – The wait() function in Ubuntu

The wait() function in Ubuntu… here is a solution to the problem.

The wait() function in Ubuntu

I’m learning about processes in Ubuntu and their behavior, but I’m a bit confused about wait(). So my question is:

  1. declare while(wait(NULL)>0); How does it work?
  2. What is NULL in wait() for?

I’ve seen the output in the terminal, but even when the wait() function is executed, the parent is still executing and spawning the child. Shouldn’t the execution of the parent stop? Here is the code:

int main(int argc, char *argv[])
{
pid_t childid;

if(argc!=2)
{
    printf("Please provide an argument in terminal! \n");
    return 0;
}

int i,n=atoi(argv[1]);

for(i=0; i<n; i++)
{
    childid=fork();

if(childid==0)
    {
        printf("Inside Child process! \n My id is %d\n",getpid());
        break;  creating fan process structure
    }
    else
        if(childid>0)
        {
            printf("Inside Parent Process! \n My id is %d\n",getpid());
        }
}

while(wait(NULL)>0); 

printf("After While Statment!\n My id is %d\n My parent ID is %d\n Child id is %d\n",getpid(),getppid(),childid);

I know it’s a very lame question, but that’s how people learn 🙂

Thanks

Solution

declare while(wait(NULL)>0); How does it work?

The function wait, waiting for the termination of any child process, and if successful, returns the process identifier of the terminated child process. If there are no child processes, -1 is returned.

In your code, the parent process basically waits for all child processes it created to terminate.

For any child processes, this call will immediately return -1 because they have not created any processes yet.

What is the purpose of NULL in wait()?
If you see the prototype of wait, it looks like this

pid_t wait(int *status);

THE PARENT PROCESS CAN GET THE EXIT STATUS OF THE

CHILD PROCESS IN THE VARIABLE STATUS (WE NEED TO PASS THE ADDRESS OF AN INTEGER IN THE WAIT FUNCTION TO UPDATE THE INTEGER), AND THEN THE WEXITSTATUS MACRO EXTRACTS THE VALUE.

Passing NULL

(we pass NULL because the variable is a pointer type) means that the programmer is not interested in the value and is notifying the waiting call for this.

I modified your code slightly to explain the use of return values and non-NULL parameters for wait functions. All children now return a value (loop index i) that is taken by the parent.

See if this helps

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

int main(int argc, char *argv[])
{
  pid_t childid;
  int ret;
  int status;

if(argc!=2)
 {
    printf("Please provide an argument in terminal! \n");
    return 0;
 }

int i,n=atoi(argv[1]);

for(i=0; i<n; i++)
{
  childid=fork();

if(childid==0){
    printf("Inside Child process! \n My id is %d\n",getpid());
    break;  creating fan process structure
  }
  else if(childid > 0){
        printf("Inside Parent Process! \n My id is %d\n",getpid());
  }
}

The line below, will be immediatly false for all  the children
while ((ret= wait(&status))>0)
{
  printf("After While My id is %d and my  child with pid =%d exiting with return value=%d\n" ,
  getpid(),ret, WEXITSTATUS(status));
}
The if below will be true for the children only
if ((0 > ret) && (childid == 0)){
   printf("Child with id %d exiting and my parent's id is %d\n", getpid(), getppid());
   exit(i);
 }
else{
   printf("Parent Finally Exiting\n");
 }
}

Related Problems and Solutions