-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs.c
56 lines (44 loc) · 1.13 KB
/
jobs.c
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "jobs.h"
#include "headers.h"
#include "list.h"
#include "main.h"
void jobs(int args)
{
if (args > 1)
{
printf("Usage: jobs\n");
}
else
{
int index = 1, fd;
Node * curr = procList;
char * buf = (char*) malloc(1024);
while(curr)
{
sprintf(buf, "/proc/%d/stat", curr->pid);
fd = open(buf, O_RDONLY);
if(fd < 0)
{
printf("Couldn't find relevant files for pid %d\n", curr->pid);
perror("jobs");
continue;
}
read(fd, buf, 1024);
char * status;
strtok(buf, " ");
strtok(0, " ");
char * procStat = strtok(0, " ");
if(procStat[0] == 'T')
status = "Stopped";
else if(procStat[0] == 'Z')
status = "Zombie";
else
status = "Running";
printf("[%d] %s %s [%d]\n", index, status, curr->name, curr->pid);
index++;
curr = curr->next;
close(fd);
}
free(buf);
}
}