-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.c
55 lines (44 loc) · 1.02 KB
/
init.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
// init: The initial user-level program
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fcntl.h"
#define PAGESIZE 4096
char *argv[] = { "sh", 0 };
int
main(void)
{
int thread_id, jpid;
// allocating 2 * pageSize for fptr in heap
void *fptr = malloc(2 * (PAGESIZE));
void *stack;
if(fptr == 0)
return -1;
int mod = (uint)fptr % PAGESIZE;
// the following if-else is for assigning page-aligned space to stack
if(mod == 0)
stack = fptr;
else
stack = fptr + (PAGESIZE - mod);
if(open("console", O_RDWR) < 0){
mknod("console", 1, 1);
open("console", O_RDWR);
}
dup(0); // stdout
dup(0); // stderr
for(;;){
printf(1, "init: starting sh\n");
thread_id = clone((void*)stack);
if(thread_id < 0){
printf(1, "init: fork failed\n");
exit();
}
if(thread_id == 0){
exec("sh", argv);
printf(1, "init: exec sh failed\n");
exit();
}
while((jpid = join()) >= 0 && jpid != thread_id)
printf(1, "zombie!\n");
}
}