-
Notifications
You must be signed in to change notification settings - Fork 0
/
child.c
111 lines (94 loc) · 3.11 KB
/
child.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
#include "child.h"
#include <csse2310a3.h>
#include <ctype.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
// longest possible status is null-terminated "signalled(XXX)" (15 characters)
#define MAX_STATUS_BUFFER_SIZE 15
/* Stores information about child processes created by the spawn command. */
extern ChildList* childList;
Child* get_child_by_jobid(int jobId) {
Child** children = childList->children;
for (int i = 0; children[i]; i++) {
if (children[i]->jobId == jobId) {
return children[i];
}
}
return NULL;
}
Child* get_child_by_pid(int processId) {
Child** children = childList->children;
for (int i = 0; children[i]; i++) {
if (children[i]->processId == processId) {
return children[i];
}
}
return NULL;
}
ChildList* init_child_list() {
ChildList* childList = malloc(sizeof(ChildList));
childList->numChildren = 0;
Child** children = malloc(sizeof(Child*));
childList->children = children;
return childList;
}
Child* init_child(pid_t processId, char* programName, int pToC, int cToP) {
// create child
Child* child = malloc(sizeof(Child));
child->processId = processId;
child->jobId = childList->numChildren;
child->programName = malloc(strlen(programName) + 1);
strcpy(child->programName, programName);
child->status = malloc(MAX_STATUS_BUFFER_SIZE);
strcpy(child->status, "running");
child->pToC = pToC;
child->cToP = cToP;
child->readStream = fdopen(cToP, "r");
// add the child to the list
childList->children = realloc(childList->children,
sizeof(Child) * (++(childList->numChildren)));
childList->children[childList->numChildren - 1] = child;
return child;
}
void report_single_child(Child* child) {
wait_on_child(child);
printf("[%d] %s:%s\n", child->jobId, child->programName, child->status);
fflush(stdout);
}
void wait_on_child(Child* child) {
int statusCode;
char* status = child->status;
// only wait on child and write to its status string if the last status
// was running - if it has been changed already, it cannot change again
if (!strcmp(status, "running")
&& waitpid(child->processId, &statusCode, WNOHANG)) {
if (WIFEXITED(statusCode)) { // statusCode => exited
sprintf(status, "exited(%d)", WEXITSTATUS(statusCode));
} else if (WIFSIGNALED(statusCode)) { // statusCode => signalled
sprintf(status, "signalled(%d)", WTERMSIG(statusCode));
} else {
strcpy(status, "running");
}
}
}
void reap_child(int signum, siginfo_t* info, void* ucontext) {
Child* child = get_child_by_pid(info->si_pid);
wait_on_child(child);
}
void free_child_list() {
Child** children = childList->children;
for (int i = 0; children[i]; i++) {
Child* child = children[i];
free(child->programName);
free(child->status);
fclose(child->readStream);
free(child);
}
free(children);
free(childList);
}