The purpose of the wait() in parent c

前端 未结 3 1663
北恋
北恋 2021-01-27 14:40

I am new to processes in linux and c. I am using this straightforward example:

#include 
#include 
#include 

int         


        
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-27 15:28

    The system call wait(2) is typically used to find if the child process's state has changed (i.e. whether it's still running, exited, etc).

    Another purpose is to avoid "zombie" processes. If parent process doesn't wait on the child process and the child process exits before the parent process then it becomes a "zombie" process. So, a wait() call is used to "reap" the process and release the system resources associated with the process.

    Imagine if the parent process is a long running one and creates several child processes at regular intervals then all the zombie processes will have entries in the process table which is an unncessary use of system resources.

提交回复
热议问题