MIT OpenCourseWare


» 進階搜尋
 課程首頁
 教學大綱
 教學時程
 相關閱讀資料
 課堂講稿
 實作課程
 作業
 測驗

Lecture 1: Operating System Overview


本頁翻譯進度

燈號說明

審定:林偉棻(簡介並寄信)
審定簡介:
美國密西根大學安娜堡校區電機資訊博士

翻譯:曾琬瑂(簡介並寄信)

Overview
  • Goal of course:
    • Understand operating systems in detail by designing and implementing miminal OS
    • Hands-on experience with building systems ("Applying 6.033")
    • "new" course (not taught at MIT since 1968); last year taught has an experiment.
  • What is an operating system?
    • a piece of software that turns the hardware into something useful
    • layered picture: hardware, OS, applications
    • Three main functions: fault isolate applications, abstract hardware, manage hardware
  • Examples:
    • OS-X, Windows, Linux, *BSD, ... (desktop, server)
    • PalmOS Windows/CE (PDA)
    • VxWorks, pSOS (real-time)
    • ...
  • OS Abstractions
    • processes: fork, wait, exec, exit, kill, getpid, brk, nice, sleep, trace
    • files: open, close, read, write, lseek, stat, sync
    • directories: mkdir, rmdir, link, unlink, mount, umount
    • users + security: chown, chmod, getuid, setuid
    • interprocess communication: signals, pipe
    • networking: socket, accept, snd, recv, connect
    • time: gettimeofday
  • OS organization
    • trust and nontrusted code (kernel)

Class structure

  • Lab: minimal OS for x86 in an exokernel style:
    • kernel interface: hardware + protection
    • libOS implements fork, exec, pipe, ...
    • development environment: gcc, bochs
  • Lecture structure
    • 45min lecture
    • 45min case study
  • First case study: UNIX 6th edition (for first half of the term):
    • Multi-user time-sharing operating system for PDP11
      • PDP-11 (1972):
      • 16-bit processor, 18-bit physical (40)
      • UNIBUS
      • memory-mapped I/O
      • performance: less than 1MIPS
      • register-to-register transfer: 0.9 usec
      • 56k-228k (40)
      • no paging, but some segmentation support
      • interrupts, traps
      • about $10K
      • rk disk with 2MByte of storage
      • with cabinet 11/40 is 400lbs
    • UNIX v6
      • 1976; first widely available UNIX outside Bell labs
      • R&T
      • simplicity (reaction to MULTICS)
      • complete (used for real work)
      • small (43 system calls)
      • modular (composition through pipes; one had to split programs!!)
      • advanced UI (shell)
      • compactly written (2 programmers, 9,000 lines of code)
      • newer UNIXs have inherited many of the conceptual ideas even though they added paging, networking, graphics, etc.
      • introduced C (derived from B)
      • distributed with source
      • V7 was sold by Microsoft for a couple years under the name Xenix
    • Lion's commentary
      • surpressed because of copyright issue
      • resurfaced in 1996

Case study: the shell (simplified)

  • Nice example that uses various OS abstractions. See Ritchie, D.M. and Thompson, K. "The UNIX Time-Sharing System." Communications of the ACM, 17. no. 7 (July 1974): 365-375, if you are unfamiliar with the shell's implementation.
  • Final lab is a simple shell.
  • Basic structure:
          
    while (1) {
            printf ("$");
            readcommand (command, args);
            if ((pid = fork ()) == 0) {  // child?
               exec (command, args, 0);
            } else if (pid > 0) {   // parent?
               wait (0);   // wait for child to terminate
            } else {
               printf ("Unable to fork\n");
                }
            }
  • Example:
            $ ls
  • why call "wait"? if child finishes, it becomes a zombie
  • I/O: file descriptors. Every process starts with:
    • file descriptor 0 for input (e.g., keyboard). read_command:
           read (1, buf, bufsize)
    • file descriptor 1 for output (e.g., terminal)
           write (1, "hello\n", strlen("hello\n")+1)
    • file descriptor 2 for error (e.g., terminal)
  • How does the shell implement:
     $ls > tmp1
    just before exec insert:
              close (1);
           fd = open ("tmp1", O_CREAT|O_WRONLY);   // fd will be 1!
  • How does the shell implement sharing an output file:
         $ls 2> tmp1
    replace last code with:
           close (1);
           close (2);
           fd1 = open ("tmp1", O_CREAT|O_WRONLY);   // fd will be 1!
           fd2 = dup (fd);
    both file descriptors share offset
  • how do programs communicate?
            $ sort file.txt | uniq | wc
    or
      $ sort file.txt > tmp1
        $ uniq tmp1 > tmp2
        $ wc tmp2
        $ rm tmp1 tmp2
    or
            $ kill signal
  • A pipe is an one-way communication channel. Here is an example where the parent is the writer and the child is the reader:
        int fd[2];
        
        if (pipe(fd) < 0) panic ("error");
        if ((pid = fork()) < 0) panic ("error");
        else if (pid > 0) {
          close(fd[0]);
          write(fd[1], "hello world\n", 12);
            } else {
          close(fd[1]);
          n = read (fd[0], buf, MAXBUF);
          write (1, buf, n);
            }
  • How does the shell implement pipelines (i.e., cmd 1 | cmd 2 | ...)? Here is a sketch of the modifications to our shell that sets up a pipe between cmd1 and cmd2:
           
            int fd[2];
            if (pipe(fd) < 0) panic ("error");
            if ((pid = fork ()) == 0) {
               close (1);
               tmp = dup (fd[1]);   // fd[1] is the write end
               close (fd[0]);       // close read end
               exec (command1, args1, 0);
            } else if (pid > 0) {
               close (0);
               tmp = dup (fd[0]);   // fd[0] is the read end
               close (fd[1]);       // close write end
               exec (command2, args2, 0);
            } else {
               printf ("Unable to fork\n");
                }
  • Why close read-end and write-end? multiple reasons: maintain that every process starts with 3 file descriptors and reading from an empty pipe blocks reader, while reading from a closed pipe returns end of file.
  • How do you background jobs?
            $ compute &
  • How does the shell implement "&", backgrounding? (Don't call wait immediately).
  • More details at: /v6/usr/source/s2/sh.c

 
MIT Home
Massachusetts Institute of Technology Terms of Use Privacy