Designing a monitor process for monitoring and restarting processes

前端 未结 4 1522
灰色年华
灰色年华 2020-12-19 22:33

I am designing a monitor process. The job of the monitor process is to monitor a few set of configured processes. When the monitor process detects that a process has gone do

4条回答
  •  旧时难觅i
    2020-12-19 23:37

    You should not be checking /proc to determine which process has exited - it's possible for another, unrelated, process to start in the meantime and be coincidentally assigned the same PID.

    Instead, within your SIGCHLD handler you should use the waitpid() system call, in a loop such as:

    int status;
    pid_t child;
    
    while ((child = waitpid(-1, &status, WNOHANG)) > 0)
    {
        /* Process with PID 'child' has exited, handle it */
    }
    

    (The loop is needed because multiple child processes may exit within a short period of time, but only one SIGCHLD may result).

提交回复
热议问题