What is the difference between wait(null)
and wait(&status)
in c system programming?
And what is the content of the pointer status ?
wait(NULL) or more accurately wait(0) means wait until a state change in the child process. To find out about Unix / Linux C api calls, type man <function Name> on the command line. You'll need to get used to reading those pages, so better start now.
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.
Difference between wait and waitpid():Wait() waits for any child process but waitpid() waits for a specific child equal to pid. By default waitpid() waits for the only terminated child where as wait() waits for both terminated or a signaled child.
while(wait(NULL) > 0); This ensures that you wait for ALL the child processes and only when all have returned, you move to the next instruction.
If you call wait(NULL)
(wait(2)), you only wait for any child to terminate. With wait(&status)
you wait for a child to terminate but you want to know some information about it's termination.
You can know if the child terminate normally with WIFEXITED(status)
for example.
status
contains information about processes that you can check with some already defined MACRO.
wait(NULL)
will only wait until the child process is completed. But, wait(&status)
will return the process id of the child process that is terminated.
pid = wait(&status); // the information is returned
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With