Naruto Naruto - 1 year ago 65
C Question

What does wait() do on Unix?

I was reading about the

function in a Unix systems book. The book contains a program which has
in it. I don't understand what that means. In other program there was


...which also made me scratch my head.

Can anybody explain what the function above is doing?

mux mux
Answer Source

man wait(2)

All of these system calls are used to wait for state changes in a child of the calling process, and obtain information about the child whose state has changed. A state change is considered to be: the child terminated; the child was stopped by a signal; or the child was resumed by a signal

So wait() allows a process to wait until one of its child processes change its state, exists for example. If waitpid() is called with a process id it waits for that specific child process to change its state, if a pid is not specified, then it's equivalent to calling wait() and it waits for any child process to change its state.

The wait() function returns child pid on success, so when it's is called in a loop like this:


It means wait until all child processes exit (or change state) and no more child processes are unwaited-for (or until an error occurs)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download