Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use wait in C

Tags:

c

wait

How do i use wait ? It just baffles me to no end. I fork a tree of procs with recursion and now the children have to pause(wait/sleep) while I run pstree so I can print the proc tree.

Should i use

int status;
wait(&status);

or rather

wait(NULL)

and where should i put this? in the parent if(pid > 0) or in the children if(pid==0)? Maybe at the end of ifs, so I store all the pids in array and then run a for over them and use wait?

my code template:

void ProcRec(int index)
{
     pid_t pid;
     int noChild = getNChild(index);

     int i= 0;
     for(i = 0; i < noChild; i++)
     { 
          pid = fork();

        if (pid > 0)
        {
            /* parent process */
        }
        else if (pid == 0)
        {
            /* child process. */
            createProc(index+1);
        }
        else
        {
            /* error */
            exit(EXIT_FAILURE);
        }
    }

    if(getpid() == root)
    {
        sleep(1); 
        pid = fork();
        if(pid == 0)
          execl("/usr/bin/pstree", "pstree", getppid(), 0);    
    }
}
like image 930
jabk Avatar asked May 17 '14 09:05

jabk


People also ask

How wait is used in C?

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.

Why do we use wait () in C?

The wait() and waitpid() functions allow the calling process to obtain status information pertaining to one of its child processes. Various options permit status information to be obtained for child processes that have terminated or stopped.

How do you use wait?

Wait means 'stay in the same place or not do something until something else happens'. We can use it with or without for: Put a tea bag into the cup, then add water and wait (for) a minute or two before taking it out. I phoned the head office but I had to wait (for) five minutes before I spoke to anyone.

What library is wait () in C?

BSD Process Wait FunctionsThe GNU C Library defines macros such as WEXITSTATUS so that they will work on either kind of object, and the wait function is defined to accept either type of pointer as its status-ptr argument. These functions are declared in `sys/wait.


1 Answers

The wait system-call puts the process to sleep and waits for a child-process to end. It then fills in the argument with the exit code of the child-process (if the argument is not NULL).

So if in the parent process you have

int status;
if (wait(&status) >= 0)
{
    if (WEXITED(status))
    {
        /* Child process exited normally, through `return` or `exit` */
        printf("Child process exited with %d status\n", WEXITSTATUS(status));
    }
}

And in the child process you do e.g. exit(1), then the above code will print

Child process exited with 1 status

Also note that it's important to wait for all child processes. Child processes that you don't wait for will be in a so-called zombie state while the parent process is still running, and once the parent process exits the child processes will be orphaned and made children of process 1.

like image 63
Some programmer dude Avatar answered Oct 11 '22 11:10

Some programmer dude