Knowing the process status using procf/<pid>/status

五迷三道 提交于 2019-12-01 10:10:03

问题


I am working on Solaris.

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.

As an example, I used my shell process:

> ps
   PID TTY         TIME CMD
 18671             0:01 tcsh

whose process id is 18671.

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);

}

It doesn't produce any output:

> ./a.out
Open Successful
> 

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.

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

A C++ solution would also be acceptable.


回答1:


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;

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

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



来源:https://stackoverflow.com/questions/13469276/knowing-the-process-status-using-procf-pid-status

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!