- 论坛徽章:
- 0
|
对于Linux下的多进程的系统中,实时监测各个进程的状态很有必要,如果发现麽个进程异常了,可以清除这个进程后重新再调用起来,这样对整个系统的稳定运行很有必要。
那么怎样实时监测进程运行状态呢?下面贴出范例供大家参草。
#include <sys/io.h>
#include <sys/ipc.h>
#include <sys/timeb.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/sysinfo.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/msg.h>
void CheckPid(void)
{
pid_t termpid;
int flags;
termpid = waitpid(-1,&flags,WNOHANG);
if (termpid>0)
{
msglog.code = LOG_MSG_OTHER;
if(WIFEXITED(&flags))
{
printf("pid=%d的子进程正常结束,返回信息=%d,结束状态=%d\n",termpid,flags,WEXITSTATUS(&flags));
}
else if(WIFSIGNALED(&flags))
{
printf("pid=%d的子进程异常终止,返回信息=%d,终止进程的信号的编号=%d\n",termpid,flags,WTERMSIG(&flags));
}
else if(WIFSTOPPED(&flags))
{
printf("pid=%d的子进程暂停,返回信息=%d,暂停进程的信号的编号=%d\n",termpid,flags,WSTOPSIG(&flags));
}
else
{
printf_d(cur_dep,SYS_PID_DSC_OUTTYPE,"pid=%d的子进程退出",termpid);
}
}
}
其中“返回信息”flags的返回值有几种情况,如段错误、正常终止、 被信号终断等。详细的介绍见Linux系统下的信号头文件。
参考文件 http://www.sidongxiao.com/2012/10/19/linux-how-to-check-pipe-ok/
|
|