使用procf / <pid> / status </pid>了解进程状态

时间:2012-11-20 08:35:33

标签: c++ c solaris procfs

我正在使用Solaris。

我知道如果有进程正在运行,则会有一个名为/proc/<PID>/status的文件,其中<PID>是进程ID,它包含一个名为state的字段。

作为一个例子,我使用了我的shell进程:

> ps
   PID TTY         TIME CMD
 18671             0:01 tcsh

其进程ID为18671。

我写了一个简单的C程序来提取这些信息:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/procfs.h>
#include <sys/fcntl.h>

static void get_status (pid_t pid)
{
  char procpath[100];
  char buf[100];
  int pfd;
  char State[100];
  char Name[100];
  prstatus_t * pms;
  FILE *proc;


    sprintf(procpath, "/proc/%d/status", pid);

    proc = fopen(procpath,"r");
    if (proc) {
        printf("Open Successful\n");
        fgets(buf,256,proc); sscanf(buf,"Name:\t%s",Name);
        fgets(buf,256,proc); sscanf(buf,"State:\t%c",State);
       }
    printf("%s",Name);
    printf("%s",State);
}

int main(int argc, char **argv)
{
    get_status(18671);

}

它不会产生任何输出:

> ./a.out
Open Successful
> 

procfs的在线资料说我们可以在proc/<pid>/status上做一只猫并检查过程的状态。

但在我的情况下,它是一个二进制文件。我从来没有在任何地方提到它是二进制的。

有没有办法可以使用简单的C程序来获取当前进程的状态?

C ++解决方案也是可以接受的。

1 个答案:

答案 0 :(得分:3)

这是您应该从/ proc / pid / status:

中读出的结构
typedef struct pstatus {
        int     pr_flags;       /* flags (see below) */
        int     pr_nlwp;        /* number of active lwps in the process */
        pid_t   pr_pid;         /* process id */
        pid_t   pr_ppid;        /* parent process id */
        pid_t   pr_pgid;        /* process group id */
        pid_t   pr_sid;         /* session id */
        id_t    pr_aslwpid;     /* historical; now always zero */
        id_t    pr_agentid;     /* lwp id of the /proc agent lwp, if any */
        sigset_t pr_sigpend;    /* set of process pending signals */
        uintptr_t pr_brkbase;   /* address of the process heap */
        size_t  pr_brksize;     /* size of the process heap, in bytes */
        uintptr_t pr_stkbase;   /* address of the process stack */
        size_t  pr_stksize;     /* size of the process stack, in bytes */
        timestruc_t pr_utime;   /* process user cpu time */
        timestruc_t pr_stime;   /* process system cpu time */
        timestruc_t pr_cutime;  /* sum of children's user times */
        timestruc_t pr_cstime;  /* sum of children's system times */
        sigset_t pr_sigtrace;   /* set of traced signals */
        fltset_t pr_flttrace;   /* set of traced faults */
        sysset_t pr_sysentry;   /* set of system calls traced on entry */
        sysset_t pr_sysexit;    /* set of system calls traced on exit */
        char    pr_dmodel;      /* data model of the process (see below) */
        char    pr_pad[3];
        taskid_t pr_taskid;     /* task id */
        projid_t pr_projid;     /* project id */
        int     pr_nzomb;       /* number of zombie lwps in the process */
        zoneid_t pr_zoneid;     /* zone id */
        int     pr_filler[15];  /* reserved for future use */
        lwpstatus_t pr_lwp;     /* status of the representative lwp */
} pstatus_t;

请注意,它在头文件procfs.h中定义。声明pstatus_t变量并将sizeof(pstatus_t)个字节读入该变量。

提示:也不能通过ls使用,您也可以使用/proc/self/psinfo来读取自我进程的psinfo。

相关问题