-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessHandler.c
More file actions
125 lines (111 loc) · 3.6 KB
/
processHandler.c
File metadata and controls
125 lines (111 loc) · 3.6 KB
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
118
119
120
121
122
123
124
125
#include "processHandler.h"
processHandler_t *createNewProcessHandler_T() {
processHandler_t *processHandler = (processHandler_t *) malloc(sizeof(processHandler_t));
processHandler->foreground = NULL;
processHandler->totalBackgroundProcessesAdded = 0;
processHandler->background = createEmptyList();
return processHandler;
}
process_t *createNewProcess_T(char *line, int id, int count, pid_t *pid, pid_t *ioHandler, int *hasRedirection) {
process_t *p = (process_t *) malloc(sizeof(process_t));
p->line = line;
p->jobId = id;
p->count = count;
p->pid = pid;
p->groupPid = pid[0];
p->groupStatus = RUNNING;
p->pidStatus = malloc(sizeof(status_t) * count);
p->ioHandlers = ioHandler;
p->hasRedirection = hasRedirection;
for (int i = 0; i < count; ++i) {
p->pidStatus[i] = RUNNING;
}
return p;
}
void checkProcessStatus(process_t *p) {
if (p->groupStatus != ENDED) {
for (int i = 0; i < p->count; ++i) {
if (p->pidStatus[i] != ENDED && waitpid(p->pid[i], NULL, WNOHANG) > 0) {
p->pidStatus[i] = ENDED;
}
}
int check = 1;
for (int i = 0; i < p->count; ++i) {
check = check && p->pidStatus[i] == ENDED;
}
if (check) {
p->groupStatus = ENDED;
}
}
}
void cleanProcess_T(process_t *process) {
free(process->line);
free(process->pid);
free(process->pidStatus);
free(process->ioHandlers);
free(process->hasRedirection);
free(process);
}
void cleanProcessHandler_T(processHandler_t *processHandler) {
node_t *n = processHandler->background->first;
while (n != NULL) {
free(n->previous);
cleanProcess_T((process_t *) n->info);
n = n->next;
}
free(processHandler->background);
removeForeground(processHandler);
free(processHandler);
}
void setForeground(processHandler_t *processHandler, process_t *process) {
processHandler->foreground = process;
}
process_t *getForeground(processHandler_t const *processHandler) {
return processHandler->foreground;
}
void removeForeground(processHandler_t *processHandler) {
if (processHandler->foreground != NULL) {
cleanProcess_T(processHandler->foreground);
processHandler->foreground = NULL;
}
}
int addBackground(processHandler_t *processHandler, process_t *process) {
addInfo(processHandler->background, process);
if (process->jobId == -1) {
processHandler->totalBackgroundProcessesAdded++;
process->jobId = processHandler->totalBackgroundProcessesAdded;
}
return processHandler->totalBackgroundProcessesAdded;
}
process_t *removeBackground(processHandler_t *processHandler, int jobId) {
node_t *n = processHandler->background->first;
process_t *p = NULL;
if (n != NULL && ((process_t *) n->info)->jobId == jobId) {
p = (process_t *) n->info;
}
while (n != NULL && p == NULL) {
n = n->next;
if (((process_t *) n->info)->jobId == jobId) {
p = (process_t *) n->info;
}
}
if (p != NULL) {
node_t *previous = n->previous;
node_t *next = n->next;
if (previous != NULL) {
previous->next = next;
} else {
processHandler->background->first = next;
}
if (next != NULL) {
next->previous = previous;
} else {
processHandler->background->last = previous;
}
processHandler->background->count--;
}
return p;
}
list_t *getBackground(processHandler_t const *processHandler) {
return processHandler->background;
}