-
Notifications
You must be signed in to change notification settings - Fork 0
/
activities.c
117 lines (107 loc) · 2.89 KB
/
activities.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "activities.h"
// Function to split the linked list into two halves
void split(struct ProcessInfo *source, struct ProcessInfo **front, struct ProcessInfo **back)
{
struct ProcessInfo *slow;
struct ProcessInfo *fast;
slow = source;
fast = source->next;
while (fast != NULL)
{
fast = fast->next;
if (fast != NULL)
{
slow = slow->next;
fast = fast->next;
}
}
*front = source;
*back = slow->next;
slow->next = NULL;
}
struct ProcessInfo *merge(struct ProcessInfo *a, struct ProcessInfo *b)
{
struct ProcessInfo *result = NULL;
if (a == NULL)
return b;
else if (b == NULL)
return a;
char str1[20], str2[20]; // Assuming a buffer large enough to hold the string
sprintf(str1, "%d", a->pid);
sprintf(str2, "%d", b->pid);
if (strcmp(str1, str2) <= 0)
{
result = a;
result->next = merge(a->next, b);
}
else
{
result = b;
result->next = merge(a, b->next);
}
return result;
}
void mergeSort(struct ProcessInfo **headRef)
{
struct ProcessInfo *head = *headRef;
struct ProcessInfo *a;
struct ProcessInfo *b;
if ((head == NULL) || (head->next == NULL))
return;
split(head, &a, &b);
mergeSort(&a);
mergeSort(&b);
*headRef = merge(a, b);
}
void activity_handler(char *tokens[], int num_tokens)
{
mergeSort(&processList);
struct ProcessInfo *current = processList;
while (current != NULL)
{
int pid = current->pid;
char location[1024];
sprintf(location, "/proc/%d/cmdline", pid);
sprintf(location, "/proc/%d/stat", pid);
FILE *f = fopen(location, "r");
char state = 0;
int pd;
char new_name[1024];
char statee[100];
if (f == NULL)
{
printf(" %d ", current->pid);
for (int i = 0; i < current->sz; i++)
{
printf("%s ", current->name[i]);
}
printf("finished\n");
processList = removeProcess(processList, current->pid);
}
else
{
fscanf(f, " %d ", &pd);
fscanf(f, " %s ", new_name);
fscanf(f, " %c ", &state);
if (state == 'R')
strcpy(statee, "Running");
else if (state == 'T')
strcpy(statee, "Stopped");
else if (state == 'Z')
strcpy(statee, "Zombie");
else if (state == 'S')
strcpy(statee, "Running");
else
strcpy(statee, "Unknown State");
fclose(f);
printf(" %d ", current->pid);
for (int i = 0; i < current->sz; i++)
{
printf("%s ", current->name[i]);
}
printf("%s\n", statee);
}
current = current->next;
}
return;
}