免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2052 | 回复: 1
打印 上一主题 下一主题

[学习分享] Linux下怎样检测进程是否异常 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-10-27 23:22 |只看该作者 |倒序浏览

对于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/


论坛徽章:
0
2 [报告]
发表于 2012-10-28 22:24 |只看该作者
自己编译通过的,自己顶一下!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP