使用procf/< pid>/状态了解进程状态 [英] Knowing the process status using procf/<pid>/status

查看:171
本文介绍了使用procf/< pid>/状态了解进程状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Solaris.

I am working on Solaris.

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

I know that if there is a process running, there is a file called /proc/<PID>/status, where <PID> is the process id, and it contains a field called state.

作为示例,我使用了我的shell程序:

As an example, I used my shell process:

> ps
   PID TTY         TIME CMD
 18671             0:01 tcsh

其进程ID为18671.

whose process id is 18671.

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

I had written a simple C program for extracting that information:

#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上做个尝试,即可检查过程的状态.

The online material for procfs says that we can simply do a cat on proc/<pid>/status and check the state of the process.

但是对于我来说,它是一个二进制文件.我从未在任何地方看到它是二进制的.

But in my case it's a binary file. I never saw it mentioned anywhere that it is binary.

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

Is there a way where I could use a simple C program to get the state of the current process?

也可以接受C ++解决方案.

A C++ solution would also be acceptable.

推荐答案

这是您应该从/proc/pid/status中读取的结构:

This is the struct you should read out of /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)个字节读入该变量.

Note it's defined in header file procfs.h. Declare a pstatus_t variable and read sizeof(pstatus_t) bytes into that variable.

提示:也不能通过ls使用,也可以使用/proc/self/psinfo读取自身过程的psinfo.

Tip: Also not available through ls, you can also use /proc/self/psinfo to read psinfo of self process.

这篇关于使用procf/&lt; pid&gt;/状态了解进程状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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