-
Notifications
You must be signed in to change notification settings - Fork 51
/
kernel_proc.c
366 lines (277 loc) · 6.99 KB
/
kernel_proc.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <assert.h>
#include "kernel_cc.h"
#include "kernel_proc.h"
#include "kernel_streams.h"
/*
The process table and related system calls:
- Exec
- Exit
- WaitPid
- GetPid
- GetPPid
*/
/* The process table */
PCB PT[MAX_PROC];
unsigned int process_count;
PCB* get_pcb(Pid_t pid)
{
return PT[pid].pstate==FREE ? NULL : &PT[pid];
}
Pid_t get_pid(PCB* pcb)
{
return pcb==NULL ? NOPROC : pcb-PT;
}
/* Initialize a PCB */
static inline void initialize_PCB(PCB* pcb)
{
pcb->pstate = FREE;
pcb->argl = 0;
pcb->args = NULL;
for(int i=0;i<MAX_FILEID;i++)
pcb->FIDT[i] = NULL;
rlnode_init(& pcb->children_list, NULL);
rlnode_init(& pcb->exited_list, NULL);
rlnode_init(& pcb->children_node, pcb);
rlnode_init(& pcb->exited_node, pcb);
pcb->child_exit = COND_INIT;
}
static PCB* pcb_freelist;
void initialize_processes()
{
/* initialize the PCBs */
for(Pid_t p=0; p<MAX_PROC; p++) {
initialize_PCB(&PT[p]);
}
/* use the parent field to build a free list */
PCB* pcbiter;
pcb_freelist = NULL;
for(pcbiter = PT+MAX_PROC; pcbiter!=PT; ) {
--pcbiter;
pcbiter->parent = pcb_freelist;
pcb_freelist = pcbiter;
}
process_count = 0;
/* Execute a null "idle" process */
if(Exec(NULL,0,NULL)!=0)
FATAL("The scheduler process does not have pid==0");
}
/*
Must be called with kernel_mutex held
*/
PCB* acquire_PCB()
{
PCB* pcb = NULL;
if(pcb_freelist != NULL) {
pcb = pcb_freelist;
pcb->pstate = ALIVE;
pcb_freelist = pcb_freelist->parent;
process_count++;
}
return pcb;
}
/*
Must be called with kernel_mutex held
*/
void release_PCB(PCB* pcb)
{
pcb->pstate = FREE;
pcb->parent = pcb_freelist;
pcb_freelist = pcb;
process_count--;
}
/*
*
* Process creation
*
*/
/*
This function is provided as an argument to spawn,
to execute the main thread of a process.
*/
void start_main_thread()
{
int exitval;
Task call = CURPROC->main_task;
int argl = CURPROC->argl;
void* args = CURPROC->args;
exitval = call(argl,args);
Exit(exitval);
}
/*
System call to create a new process.
*/
Pid_t sys_Exec(Task call, int argl, void* args)
{
PCB *curproc, *newproc;
/* The new process PCB */
newproc = acquire_PCB();
if(newproc == NULL) goto finish; /* We have run out of PIDs! */
if(get_pid(newproc)<=1) {
/* Processes with pid<=1 (the scheduler and the init process)
are parentless and are treated specially. */
newproc->parent = NULL;
}
else
{
/* Inherit parent */
curproc = CURPROC;
/* Add new process to the parent's child list */
newproc->parent = curproc;
rlist_push_front(& curproc->children_list, & newproc->children_node);
/* Inherit file streams from parent */
for(int i=0; i<MAX_FILEID; i++) {
newproc->FIDT[i] = curproc->FIDT[i];
if(newproc->FIDT[i])
FCB_incref(newproc->FIDT[i]);
}
}
/* Set the main thread's function */
newproc->main_task = call;
/* Copy the arguments to new storage, owned by the new process */
newproc->argl = argl;
if(args!=NULL) {
newproc->args = malloc(argl);
memcpy(newproc->args, args, argl);
}
else
newproc->args=NULL;
/*
Create and wake up the thread for the main function. This must be the last thing
we do, because once we wakeup the new thread it may run! so we need to have finished
the initialization of the PCB.
*/
if(call != NULL) {
newproc->main_thread = spawn_thread(newproc, start_main_thread);
wakeup(newproc->main_thread);
}
finish:
return get_pid(newproc);
}
/* System call */
Pid_t sys_GetPid()
{
return get_pid(CURPROC);
}
Pid_t sys_GetPPid()
{
return get_pid(CURPROC->parent);
}
static void cleanup_zombie(PCB* pcb, int* status)
{
if(status != NULL)
*status = pcb->exitval;
rlist_remove(& pcb->children_node);
rlist_remove(& pcb->exited_node);
release_PCB(pcb);
}
static Pid_t wait_for_specific_child(Pid_t cpid, int* status)
{
/* Legality checks */
if((cpid<0) || (cpid>=MAX_PROC)) {
cpid = NOPROC;
goto finish;
}
PCB* parent = CURPROC;
PCB* child = get_pcb(cpid);
if( child == NULL || child->parent != parent)
{
cpid = NOPROC;
goto finish;
}
/* Ok, child is a legal child of mine. Wait for it to exit. */
while(child->pstate == ALIVE)
kernel_wait(& parent->child_exit, SCHED_USER);
cleanup_zombie(child, status);
finish:
return cpid;
}
static Pid_t wait_for_any_child(int* status)
{
Pid_t cpid;
PCB* parent = CURPROC;
/* Make sure I have children! */
int no_children, has_exited;
while(1) {
no_children = is_rlist_empty(& parent->children_list);
if( no_children ) break;
has_exited = ! is_rlist_empty(& parent->exited_list);
if( has_exited ) break;
kernel_wait(& parent->child_exit, SCHED_USER);
}
if(no_children)
return NOPROC;
PCB* child = parent->exited_list.next->pcb;
assert(child->pstate == ZOMBIE);
cpid = get_pid(child);
cleanup_zombie(child, status);
return cpid;
}
Pid_t sys_WaitChild(Pid_t cpid, int* status)
{
/* Wait for specific child. */
if(cpid != NOPROC) {
return wait_for_specific_child(cpid, status);
}
/* Wait for any child */
else {
return wait_for_any_child(status);
}
}
void sys_Exit(int exitval)
{
PCB *curproc = CURPROC; /* cache for efficiency */
/* First, store the exit status */
curproc->exitval = exitval;
/*
Here, we must check that we are not the init task.
If we are, we must wait until all child processes exit.
*/
if(get_pid(curproc)==1) {
while(sys_WaitChild(NOPROC,NULL)!=NOPROC);
} else {
/* Reparent any children of the exiting process to the
initial task */
PCB* initpcb = get_pcb(1);
while(!is_rlist_empty(& curproc->children_list)) {
rlnode* child = rlist_pop_front(& curproc->children_list);
child->pcb->parent = initpcb;
rlist_push_front(& initpcb->children_list, child);
}
/* Add exited children to the initial task's exited list
and signal the initial task */
if(!is_rlist_empty(& curproc->exited_list)) {
rlist_append(& initpcb->exited_list, &curproc->exited_list);
kernel_broadcast(& initpcb->child_exit);
}
/* Put me into my parent's exited list */
rlist_push_front(& curproc->parent->exited_list, &curproc->exited_node);
kernel_broadcast(& curproc->parent->child_exit);
}
assert(is_rlist_empty(& curproc->children_list));
assert(is_rlist_empty(& curproc->exited_list));
/*
Do all the other cleanup we want here, close files etc.
*/
/* Release the args data */
if(curproc->args) {
free(curproc->args);
curproc->args = NULL;
}
/* Clean up FIDT */
for(int i=0;i<MAX_FILEID;i++) {
if(curproc->FIDT[i] != NULL) {
FCB_decref(curproc->FIDT[i]);
curproc->FIDT[i] = NULL;
}
}
/* Disconnect my main_thread */
curproc->main_thread = NULL;
/* Now, mark the process as exited. */
curproc->pstate = ZOMBIE;
/* Bye-bye cruel world */
kernel_sleep(EXITED, SCHED_USER);
}
Fid_t sys_OpenInfo()
{
return NOFILE;
}