I was reading about the wait() function in a Unix systems book. The book contains a program which has wait(NULL) in it. I don’t understand what that means. In other program there was
while(wait(NULL)>0)
…which also made me scratch my head.
Can anybody explain what the function above is doing?
man wait(2)
So
wait()allows a process to wait until one of its child processes change its state, exists for example. Ifwaitpid()is called with a process id it waits for that specific child process to change its state, if apidis not specified, then it’s equivalent to callingwait()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)