xv6-65oo2/user/init.c

55 lines
1.1 KiB
C
Raw Normal View History

2007-08-28 19:04:36 +00:00
// init: The initial user-level program
#include "kernel/types.h"
#include "kernel/stat.h"
#include "kernel/spinlock.h"
#include "kernel/sleeplock.h"
#include "kernel/fs.h"
#include "kernel/file.h"
#include "user/user.h"
#include "kernel/fcntl.h"
char *argv[] = { "sh", 0 };
int
main(void)
{
2007-08-08 08:39:23 +00:00
int pid, wpid;
2006-09-06 17:27:19 +00:00
2006-08-29 19:06:37 +00:00
if(open("console", O_RDWR) < 0){
mknod("console", CONSOLE, 0);
2006-08-29 19:06:37 +00:00
open("console", O_RDWR);
}
2006-09-08 14:36:44 +00:00
dup(0); // stdout
dup(0); // stderr
2006-09-06 18:47:51 +00:00
for(;;){
2019-08-27 17:13:03 +00:00
printf("init: starting sh\n");
pid = fork();
2006-08-29 19:06:37 +00:00
if(pid < 0){
2019-08-27 17:13:03 +00:00
printf("init: fork failed\n");
2019-09-11 14:04:40 +00:00
exit(1);
2006-08-29 19:06:37 +00:00
}
if(pid == 0){
exec("sh", argv);
2019-08-27 17:13:03 +00:00
printf("init: exec sh failed\n");
2019-09-11 14:04:40 +00:00
exit(1);
2006-08-29 19:06:37 +00:00
}
2020-08-15 09:46:32 +00:00
for(;;){
// this call to wait() returns if the shell exits,
// or if a parentless process exits.
wpid = wait((int *) 0);
if(wpid == pid){
// the shell exited; restart it.
break;
} else if(wpid < 0){
printf("init: wait returned an error\n");
exit(1);
} else {
// it was a parentless process; do nothing.
}
}
}
}