-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinfo.h
30 lines (30 loc) · 781 Bytes
/
pinfo.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
char* pinfo(int pid)
{
char* spath=malloc(100),*apath = malloc(sizeof(char)*50),*expath = malloc(sizeof(char)* 1000),*status=malloc(5),*mem=malloc(100);
char buf[100010];
sprintf(spath,"/proc/%d/stat",pid);
sprintf(apath,"/proc/%d/exe",pid);
int fd;
readlink(apath,expath,1000);
fd = open(spath,O_RDONLY);
if(fd < 0)
{
printf("Invalid PID\n");
return "";
}
read(fd,buf,100010);
char * token = strtok(buf," ");
int i=1;
while(token!=NULL)
{
i++;
token = strtok(NULL," ");
if(i==3)
status=token;
if(i==23)
mem=token;
}
char* res = malloc(sizeof(char) * 250);
sprintf(res,"pid - %d\nProcess Status - %s\nMemory - %s{virtual memory}\nExecutable Path - %s\n",pid,status,mem,expath);
return res;
}