-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessLinkedList.c
96 lines (72 loc) · 1.88 KB
/
processLinkedList.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
#include"processLinkedList.h"
//Universally accessable which is bad, but we don't know better solutions (yet)
node_t * createNode(int pidData, char* str){
node_t *node = (node_t*)malloc(sizeof(node_t));
node->pidData = pidData;
node->next = NULL;
node->previous = NULL;
strcpy(node->commandData, str);
return node;
}
int setLastNode(node_t *node) {
last = node;
return 0;
}
int addNode(node_t *node) {
last->next = node;
node->previous = last;
node->next = NULL;
last = node;
return 0;
}
int insertNode(node_t *node, node_t *prevNode) {
node->next = prevNode->next;
node->previous = prevNode;
prevNode->next = node;
node->next->previous = node;
if (node->next == NULL) {
last = node;
}
return 0;
}
int deleteNode(node_t *node) {
if (node->previous != NULL) {
node->previous->next = node->next;
}
if (node->next != NULL) {
node->next->previous = node->previous;
}
else {
last = node->previous;
}
free(node);
return 0;
}
void zombie_cleanup(node_t *start) {
for(node_t* n = start->next; n != NULL; n = n->next){
int wstatus;
pid_t deadstatus = waitpid(n->pidData, &wstatus, WNOHANG | WUNTRACED | WCONTINUED);
if (deadstatus != 0) {
if (deadstatus > 0) {
kill(deadstatus, SIGKILL);
}
printf("Exit status [%s] = %i\n", n->commandData, WEXITSTATUS(wstatus) );
n = n->previous;
deleteNode(n->next);
}
}
return;
}
void jobs(node_t* startNode) {
printf("=============Processes: =============\n\n");
node_t *n;
n = startNode;
printf("Parent PID: %i\n", n->pidData);
while (n->next != NULL) {
n = n->next;
printf("\tProcess PID: %i, Command: %s\n", n->pidData, n->commandData);
}
printf("=====================================\n");
fflush(stdout);
return;
}