HW: Boot shell
这部分讲了 Unix 中 shell 的设计思想,以及里面的一些 function 实现的原理。比较底层,值得深入学习,之后的多线程都涉及到了这个部分的知识。
这个 HW 主要针对 shell 中的执行命令 exec, 重定向 < >, 管道 | 实现的原理进行考察。
shell 的本质是一个 user program,并无特别之处。
补充一些背景知识:
关于 fork() 函数,之后的 lab 中会有详细介绍 fork 的底层实现。
这里我们需要知道 fork(),是一个 system call 创建了一个新的 process。
子进程与父进程有相同的 memory content,系统调用 fork() 时,可以看做同时间两个进程在跑。 pid = fork(),子进程返回值为0。父进程返回值为子进程的 pid。
int pid = fork();
if (pid > 0) {
printf("parent: child=%d\n", pid);
pid = wait();
printf("child %d is done\n", pid);
} else if (pid == 0) {
printf("child: exiting\n");
exit();
} else {
printf("fork error\n");
}
wait(), wait for a child to exit
exit(), cause the calling process to stop executing and to release resources such as memory and open files.
需要注意的是 fork() 产生的子进程有与父进程相同的 memory content,但是子进程运行的内存地址和所用到的 register 与父进程不一样,在子进程中改变一个变量不会引起父进程中的变量改变。
exec() 这个 system call 被调用时将替换调用 exec() 的进程内存空间。并且加载新的 file,被加载的这个 file 必须是满足一定的 format,即 ELF 格式。如果 exec () 调用成功,不会 return 到调用 exec() 的进程,而是去执行 ELF file 的 entry address。
exec() 的 parameter 有两个,the name of the file containing the executable and an array of string arguments.
char *argv[3];
argv[0] = "echo";
argv[1] = "hello";
argv[2] = 0;
exec("/bin/echo", argv);
printf("exec error\n");
接下来看一下 file descriptor。XV6 kernel 用 file descriptor 当做每个进程的 index table,每个进程有独立的空间提供给 file descriptor,从0开始。
file descriptor | usage |
---|---|
0 | standard input |
1 | standard output |
2 | standard error |
fork 会同时 copy 父进程的 file descriptor table。exec 替换原有进程的 memory 但保留它的 file table。这个特性能使 kernel 做到 I/O 重定向。