Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
598 views
in Technique[技术] by (71.8m points)

c - Designing a monitor process for monitoring and restarting processes

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 down, it needs to restart the process.

I am developing the code for my linux system. Here is how I developed a small prototype - Fed the details(path, arguments) about the various processes that need to be monitored. - The monitor process did the following: 1. Installed a signal handler for SIGCHLD 2. A fork and execv to start the process to be monitored. Store the pid of the child processes. 3. When a child went down, the parent recevies a SIGCHLD 4. The signal handler will now be called. The handler will run a for loop on the list of pids stored earlier. For each pid, it will check the /proc filesystem for existence of a directory corresponding to the pid. If the directory doesn't exist, the process is restarted.

Now, my question is this - Is the above method (to check the /proc filesystem) a standard or recommended mechanism of checking if a process is running or should I do something like creating a pipe for the ps command and looping through the output of ps ? - Is there a better way of achieving my requirement?

Regards.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...