设计一个监视进程监控和重新启动的过程 [英] Designing a monitor process for monitoring and restarting processes

查看:182
本文介绍了设计一个监视进程监控和重新启动的过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计一个监视进程。监视进程的工作是监视几组配置的过程。当监控过程检测的过程已经下降,需要重新启动该进程。

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.

我开发code为我的Linux系统。这里是我开发了一个小型原型
- 美联储关于需要被监视的各种处理的细节(路径,参数)。 - 监视进程做了以下内容:
1.安装SIGCHLD信号处理程序
2.叉子和execv开始要监视的过程。存储子进程的pid。
3.当孩子下楼,父recevies一个SIGCHLD
4.信号处理程序现在将被调用。该处理器将用于以前存储的PID名单上的循环运行。每个PID,它将检查/ proc文件系统对应的PID的目录的存在。如果该目录不存在,则处理被重新启动。

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.

现在,我的问题是这样的
- 是上述方法(检查/ proc文件系统)检查的标准或推荐机制,如果一个进程正在运行或我应该做的事情像ps命令创建一个管道,并通过PS的输出循环?
- ?有没有达到我的要求,更好的办法

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?

问候。

推荐答案

您不应该检查的/ proc 来确定哪些进程已退出 - 这是可能的另一个不相关,过程在此期间就被巧合分配相同的PID。

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.

相反,你的 SIGCHLD 处理程序中,你应该使用 waitpid函数()系统调用,在一个循环中,如

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 */
}

(需要的循环,因为多个子进程可能很短的时间周期内退出,但只有一个SIGCHLD可能导致)。

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

这篇关于设计一个监视进程监控和重新启动的过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆