Knowing the status of a process using procf / <pid> / status
I am working on Solaris.
I know that if there is a process, there is a file called /proc/<PID>/status , where <PID> is the process identifier, 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 identifier is 18671.
I wrote a simple C program to extract this 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 does not produce any output:
> ./a.out Open Successful > The online material for procfs says that we can just make a cat on proc/<pid>/status and check the status of the process.
But in my case it is a binary file . I have never seen him mention somewhere that he is binary.
Is there a way that I could use a simple C program to get the status of the current process?
A C ++ solution would also be acceptable.
This is the structure you should read from / 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 user times */ timestruc_t pr_cstime; /* sum of children 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 that it is defined in the procfs.h header file. Declare the variable pstatus_t and read the sizeof(pstatus_t) bytes into this variable.
Tip. Also not available via ls , you can also use /proc/self/psinfo to read the psinfo of your own process.