Linux下如何测进程是否异常

本文发布时间: 2019-Mar-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系统下找到的信号头文件。/* Signals. */#define SIGHUP 1 /* Hangup (POSIX). */#define SIGINT 2 /* Interrupt (ANSI). */#define SIGQUIT 3 /* Quit (POSIX). */#define SIGILL 4 /* Illegal instruction (ANSI). */#define SIGTRAP 5 /* Trace trap (POSIX). */#define SIGABRT 6 /* Abort (ANSI). */#define SIGIOT 6 /* IOT trap (4.2 BSD). */#define SIGBUS 7 /* BUS error (4.2 BSD). */#define SIGFPE 8 /* Floating-point exception (ANSI). */#define SIGKILL 9 /* Kill, unblockable (POSIX). */#define SIGUSR1 10 /* User-defined signal 1 (POSIX). */#define SIGSEGV 11 /* Segmentation violation (ANSI). */#define SIGUSR2 12 /* User-defined signal 2 (POSIX). */#define SIGPIPE 13 /* Broken pipe (POSIX). */#define SIGALRM 14 /* Alarm clock (POSIX). */#define SIGTERM 15 /* Termination (ANSI). */#define SIGSTKFLT 16 /* Stack fault. */#define SIGCLD SIGCHLD /* Same as SIGCHLD (System V). */#define SIGCHLD 17 /* Child status has changed (POSIX). */#define SIGCONT 18 /* Continue (POSIX). */#define SIGSTOP 19 /* Stop, unblockable (POSIX). */#define SIGTSTP 20 /* Keyboard stop (POSIX). */#define SIGTTIN 21 /* Background read from tty (POSIX). */#define SIGTTOU 22 /* Background write to tty (POSIX). */#define SIGURG 23 /* Urgent condition on socket (4.2 BSD). */#define SIGXCPU 24 /* CPU limit exceeded (4.2 BSD). */#define SIGXFSZ 25 /* File size limit exceeded (4.2 BSD). */#define SIGVTALRM 26 /* Virtual alarm clock (4.2 BSD). */#define SIGPROF 27 /* Profiling alarm clock (4.2 BSD). */#define SIGWINCH 28 /* Window size change (4.3 BSD, Sun). */#define SIGPOLL SIGIO /* Pollable event occurred (System V). */#define SIGIO 29 /* I/O now possible (4.2 BSD). */#define SIGPWR 30 /* Power failure restart (System V). */#define SIGSYS 31 /* Bad system call. */#define SIGUNUSED 31#define _NSIG 65 /* Biggest signal number + 1(including real-time signals). */#define SIGRTMIN (__libc_current_sigrtmin ())#define SIGRTMAX (__libc_current_sigrtmax ())/* These are the hard limits of the kernel. These values should not be used directly at user level. */#define __SIGRTMIN 32#define __SIGRTMAX (_NSIG – 1)#endif /* <signal.h> included. */


(以上内容不代表本站观点。)
---------------------------------
本网站以及域名有仲裁协议。
本網站以及域名有仲裁協議。

2024-Mar-04 02:11pm
栏目列表