MIT6.S081学习笔记--lec 1

发布时间 2023-07-18 09:34:39作者: Mikejiawei

引言

操作系统的目标

  • abstract H/W 抽象化硬件
  • multiplex 多路复用
  • isolation 隔离性
  • sharing 共享(进程通信,数据共享)
  • security / access control 安全性/权限控制
  • performance 性能/内核开销
  • range of applications 多应用场景

操作系统概览

操作系统应该提供的功能:1. 多进程支持 2. 进程间隔离 3. 受控制的进程间通信

  • xv6:一种在本课程中使用的类UNIX的教学操作系统,运行在RISC-V指令集处理器上,本课程中将使用QEMU模拟器代替

  • kernel(内核):为运行的程序提供服务的一种特殊程序。每个运行着的程序叫做进程,每个进程的内存中存储指令、数据和堆栈。一个计算机可以拥有多个进程,但是只能有一个内核

    每当进程需要调用内核时,它会触发一个system call(系统调用),system call进入内核执行相应的服务然后返回。

操作系统的组织结构如图1所示

OS Organization

内核提供的一系列系统调用就是用户程序可见的操作系统接口,xv6 内核提供了 Unix 传统系统调用的一部分,它们是:

image-20230717090404859

进程和内存

每个进程拥有自己的用户空间内存以及内核空间状态,当进程不再执行时xv6将存储和这些进程相关的CPU寄存器直到下一次运行这些进程。kernel将每一个进程用一个PID(process identifier)指代。在进程执行中,常常会使用forkexec系统调用来创建新的进程。如下面代码所示:

fork and wait

  • fork:形式:int fork()。其作用是让一个进程生成另外一个和这个进程的内存内容相同的子进程。在父进程中,fork的返回值是这个子进程的PID,在子进程中,返回值是0
  • exit:形式:int exit(int status)。让调用它的进程停止执行并且将内存等占用的资源全部释放。需要一个整数形式的状态参数,0代表以正常状态退出,1代表以非正常状态退出
  • wait:形式:int wait(int *status)。等待子进程退出,返回子进程PID,子进程的退出状态存储到int *status这个地址中。如果调用者没有子进程,wait将返回-1
  • pipe:形式:int pipe(int p[])。创建一个管道,将读/写文件描述符放在p[0]和p[1]中
  • sbrk:形式:char *sbrk(int n)。将进程的内存增加n字节。返回新内存的起始位置。
int pid = fork();
if (pid > 0) {
    printf("parent: child=%d\n", pid);
    pid = wait((int *) 0);
    printf("child %d is done\n", pid);
} else if (pid == 0) {
    printf("child: exiting\n");
    exit(0);
} else {
    printf("fork error\n");
}

前两行输出可能是

parent: child=1234
child: exiting

也可能是

child: exiting
parent: child=1234

这是因为在fork了之后,父进程和子进程将同时开始判断PID的值,在父进程中,PID为1234,而在子进程中,PID为0。看哪个进程先判断好PID的值,以上输出顺序才会被决定。

最后一行输出为

parent: child 1234 is done

子进程在判断完pid == 0之后将exit,父进程发现子进程exit之后,wait执行完毕,打印输出。

尽管fork了之后子进程和父进程有相同的内存内容,但是内存地址和寄存器是不一样的,也就是说在一个进程中改变变量并不会影响另一个进程。

fork and exec

exec:形式:int exec(char *file, char *argv[])。加载一个文件,获取执行它的参数,执行。如果执行错误返回-1,执行成功则不会返回,而是开始从文件入口位置开始执行命令。文件必须是ELF格式。

xv6 shell使用以上四个system call来为用户执行程序。在shell进程的main中主循环先通过getcmd来从用户获取命令,然后调用fork来运行一个和当前shell进程完全相同的子进程。父进程调用wait等待子进程exec执行完(在runcmd中调用exec

/* sh.c */
int
main(void)
{
  static char buf[100];
  int fd;

  // Ensure that three file descriptors are open.
  while((fd = open("console", O_RDWR)) >= 0){
    if(fd >= 3){
      close(fd);
      break;
    }
  }

  // Read and run input commands.
  while(getcmd(buf, sizeof(buf)) >= 0){
    if(buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' '){
      // Chdir must be called by the parent, not the child.
      buf[strlen(buf)-1] = 0;  // chop \n
      if(chdir(buf+3) < 0)
        fprintf(2, "cannot cd %s\n", buf+3);
      continue;
    }
    if(fork1() == 0)
      runcmd(parsecmd(buf));
    // parent wait the child exit
    wait(0);
  }
  exit(0);
}

你可能会想,既然forkexec总是一起使用,为什么不合并成一个呢?实际上我们可以在fork 之后,对子进程进行一些设置,比如输入/输出重定向,然后再执行exec,注意exec并不会改变子进程的file table。

当我们不需要进行额外的设置时,fork 复制内存,exec替换内存,这意味着内存的浪费,有什么办法可以优化这种情况么?答案是肯定的,之后的4.6节我们会讲到 COW (copy-on-write)机制。

I/O 和文件描述符

  • file descriptor:文件描述符,用来表示一个被内核管理的、可以被进程读/写的对象的一个整数,表现形式类似于字节流,通过打开文件、目录、设备等方式获得。一个文件被打开得越早,文件描述符就越小。

    每个进程都拥有自己独立的文件描述符列表,其中0是标准输入,1是标准输出,2是标准错误。shell将保证总是有3个文件描述符是可用的

    while((fd = open("console", O_RDWR)) >= 0)
    {    
        if(fd >= 3)
        {        
            close(fd);        
            break;    
        } 
    }
    
  • readwrite:形式int write(int fd, char *buf, int n)int read(int fd, char *bf, int n)。从/向文件描述符fd读/写n字节bf的内容,返回值是成功读取/写入的字节数。每个文件描述符有一个offset,read会从这个offset开始读取内容,读完n个字节之后将这个offset后移n个字节,下一个read将从新的offset开始读取字节。write也有类似的offset

    /* essence of cat program */ 
    char buf[512]; 
    int n; 
    
    for (;;) 
    {    
        n = read(0, buf, sizeof buf);    
        if (n == 0)        
        break;    
        if (n < 0)
        {        
            fprintf(2, "read error\n");        
            exit(1);    
        }    
        if (write(1, buf, n) != n)
        {        
            fprintf(2, "write error\n");        
            exit(1);    
        } 
    }
    
  • close。形式是int close(int fd),将打开的文件fd释放,使该文件描述符可以被后面的openpipe等其他system call使用。

    使用close来修改file descriptor table能够实现I/O重定向

    /* implementation of I/O redirection, * more specifically, cat < input.txt */ 
    char *argv[2]; 
    argv[0] = "cat"; 
    argv[1] = 0; 
    if (fork() == 0) {   // in the child process    
        close(0);  // this step is to release the stdin file descriptor    
        open("input.txt", O_RDONLY);  // the newly allocated fd for input.txt is 0, since the previous fd 0 is released    
        exec("cat", argv);  // execute the cat program, by default takes in the fd 0 as input, which is input.txt 
    }
    

    父进程的fd table将不会被子进程fd table的变化影响,但是文件中的offset将被共享。

  • dup。形式是int dup(int fd),复制一个新的fd指向的I/O对象,返回这个新fd值,两个I/O对象(文件)的offset相同

    e.g.

    fd = dup(1);
    write(1, "hello ", 6); 
    write(fd, "world\n", 6); 
    // outputs hello world
    

    除了dupfork之外,其他方式不能使两个I/O对象的offset相同,比如同时open相同的文件

Pipes

pipe:管道,暴露给进程的一对文件描述符,一个文件描述符用来读,另一个文件描述符用来写,将数据从管道的一端写入,将使其能够被从管道的另一端读出。

我们之前有提到过pipe是一个system call,形式为int pipe(int p[])p[0]为读取的文件描述符,p[1]为写入的文件描述符。

xv6中有这样一个例子,通过写管道将参数传递给wc程序

/* run the program wc with stdin connected to the read end of pipe, parent process able to communicate with child process */
int p[2];
char *argv[2];

argv[0] = "wc";
argv[1] = 0;

pipe(p); // read fd put into p[0], write fd put into p[1]
if (fork() == 0) {
    close(0);
    dup(p[0]); // make the fd 0 refer to the read end of pipe
    close(p[0]); // original read end of pipe is closed
    close(p[1]); // fd p[1] is closed in child process, but not closed in the parent process. 注意这里关闭p[1]非常重要,因为如果不关闭p[1],管道的读取端会一直等待读取,wc就永远也无法等到EOF
    exec("/bin/wc", argv); // by default wc will take fd 0 as the input, which is the read end of pipe in this case
} else {
    close(p[0]); // close the read end of pipe in parent process will not affect child process
    write(p[1], "hello world\n", 12); 
    close(p[1]); // write end of pipe closed, the pipe shuts down
}

在xv6的shell实现中即sh.c也是类似的实现,关于pipe系统调用的源码阅读,我也总结了一份代码讲解。

case PIPE:
pcmd = (struct pipecmd*)cmd;
if(pipe(p) < 0)
    panic("pipe");
if(fork1() == 0){
    // in child process
    close(1); // close stdout
    dup(p[1]); // make the fd 1 as the write end of pipe
    close(p[0]);
    close(p[1]);
    runcmd(pcmd->left); // run command in the left side of pipe |, output redirected to the write end of pipe
}
if(fork1() == 0){
    // in child process
    close(0); // close stdin
    dup(p[0]); // make the fd 0 as the read end of pipe
    close(p[0]);
    close(p[1]);
    runcmd(pcmd->right); //  run command in the right side of pipe |, input redirected to the read end of pipe
}
close(p[0]);
close(p[1]);
wait(0); // wait for child process to finish
wait(0); // wait for child process to finish
break;

文件系统

xv6文件系统包含了文件(byte arrays)和目录(对其他文件和目录的引用)。目录生成了一个树,树从根目录/开始。对于不以/开头的路径,认为是是相对路径

  • mknod:创建设备文件,一个设备文件有一个major device #和一个minor device #用来唯一确定这个设备。当一个进程打开了这个设备文件时,内核会将readwrite的system call重新定向到设备上。
  • 一个文件的名称和文件本身是不一样的,文件本身,也叫inode,可以有多个名字,也叫link,每个link包括了一个文件名和一个对inode的引用。一个inode存储了文件的元数据,包括该文件的类型(file, directory or device)、大小、文件在硬盘中的存储位置以及指向这个inode的link的个数
  • fstat。一个system call,形式为int fstat(int fd, struct stat *st),将inode中的相关信息存储到st中。
  • link。一个system call,将创建一个指向同一个inode的文件名。unlink则是将一个文件名从文件系统中移除,只有当指向这个inode的文件名的数量为0时这个inode以及其存储的文件内容才会被从硬盘上移除

注意:Unix提供了许多在用户层面的程序来执行文件系统相关的操作,比如mkdirlnrm等,而不是将其放在shell或kernel内,这样可以使用户比较方便地在这些程序上进行扩展。但是cd是一个例外,它是在shell程序内构建的,因为它必须要改变这个calling shell本身指向的路径位置,如果是一个和shell平行的程序,那么它必须要调用一个子进程,在子进程里起一个新的shell,再进行cd,这是不符合常理的。