-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs.c
87 lines (78 loc) · 1.87 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "headers.h"
extern int no_i;
extern BG rProc[100];
void jobs(char *str)
{
int r = 0, s = 0;
char *temp = strtok(str, " ");
temp = strtok(NULL, " ");
if (temp == NULL)
r = s = 1;
while (temp != NULL)
{
if (temp[0] == '-')
{
if (temp[1] == 'r')
r++;
if (temp[1] == 's')
s++;
}
temp = strtok(NULL, " ");
}
BG names[no_i], var;
for (int i = 0; i < no_i; i++)
names[i] = rProc[i];
int swap_count = 0, n = no_i;
while (n > 1)
{
for (int i = 0; i < n - 1; i++)
{
int k = strcmp(names[i].pname, names[i + 1].pname);
if (k > 0)
{
var = names[i];
names[i] = names[i + 1];
names[i + 1] = var;
swap_count++;
}
}
if (swap_count == 0)
break;
else
{
swap_count = 0;
n--;
}
}
for (int i = 0; i < no_i; i++)
{
if (names[i].done == -1)
continue;
char ch[10], a[N], b[N], name[N];
sprintf(name, "/proc/%d/stat", names[i].id);
FILE *fptr = fopen(name, "r");
if (fptr == NULL)
continue;
fscanf(fptr, "%s %s %s", a, b, ch);
if (s)
{
if (strcmp(ch, "T\0") == 0)
{
printf("[%d] ", names[i].num);
printf("Stopped ");
printf("%s [%d]\n", names[i].pname, names[i].id);
continue;
}
}
if (r)
{
if (strcmp(ch, "T\0") != 0)
{
printf("[%d] ", names[i].num);
printf("Running ");
printf("%s [%d]\n", names[i].pname, names[i].id);
}
}
fclose(fptr);
}
}