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.
This is the struct you should read out of /proc/
pid
/status:Note it's defined in header file procfs.h. Declare a
pstatus_t
variable and readsizeof(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.