模拟的操作系统nachos解读

Notes of nachos.pdf

Five Assignments:
- thread management and synchronization
- the file system
- user-level multiprogramming support
- the virtual memory system
- network support

Thread Management

  1. Mesastyle locks and condition variables using semaphores(provide solutions to a number of concurrency problems)
  2. The thread system is based on FastThreads [Anderson et al 1989]
  3. For simplicity, thread scheduling is normally nonpreemptive but to emphasize the importance of critical sections we have a command line option that causes threads to be time-sliced at random but repeatable points in the program.
    Concurrent programs are correct only if they work when a context switch can happen at any time

课件

Chapter 01 An Overview of Nachos

nachos.conf sample

Machine.stubFileSystem = false
Machine.processor = false
Machine.console = false
Machine.disk = false
Machine.bank = false
Machine.networkLink = false
ElevatorBank.allowElevatorGUI = true
NachosSecurityManager.fullySecure = false
ThreadedKernel.scheduler = nachos.threads.RoundRobinScheduler #nachos.threads.PriorityScheduler
Kernel.kernel = nachos.threads.ThreadedKernel

Boot Process

  1. 创建nachos.machine
  2. 硬件初始化:the interrupt controller, timer, MIPS processor, console, and file system
  3. 启动AutoGrader

Interrupt Management

  1. maintain an event queue together with a simulated clock
  2. clock ticks examine the queue an event takes place
  3. 每次中断10 ticks;每次执行指令1 tick

Device Event Handler

  1. 每次clock改变,就通过调用Device Event Handler检查interrupt events
  2. device event handler是对硬件的模拟,interrupt handler是软件。前者调用后者
  3. the interrupt class在调用device event handler之前会关中断
    Alt text
    Alt text

  4. Lib常用程序

  5. Lib.assertTrue断言,假的时候退出
  6. currentTCB在线程切换的时候可能有滞后现象

c++版实验指导书

Chapter 1 Process and Thread

  1. Thread的状态:61 enum ThreadStatus { JUST_CREATED, RUNNING, READY, BLOCKED };
    其中,BLOCKED就是等待状态。
  2. 当一个Thread的状态从JUST CREATED变成READY时,Nachos为其分配stack
  3. Threads的constructor只是指明了thread name (为了debug)并设置状态为JUST CREATED。
  4. machineState[18]数组中存有寄存器变量:
    • stack:指明栈底
    • stackTop:指明栈顶(SP)
    • program counter (PC)

代码阅读

nachos.machine

Timer

  1. 硬件计时器,大约每500 ticks产生一个时钟中断(不是精确的500 ticks)
  2. 作用:
    • 实现time-slicing
    • 让一个thread sleep特定的时长
  3. Timer(Privilege privilege):构造器,执行了以下内容:
    • 得到了privilege
    • 生成了时钟中断和autoGraderInterrupt,并调用时钟中断
  4. timerInterrupt()
    • 安排下次时钟中断和autoGraderInterrupt
    • 更新timer最后一次中断的时间(即ticks数量)
    • 执行中断处理程序handler
  5. scheduleInterrupt():安排500ticks后调用timerInterrupt
  6. scheduleAutoGraderInterrupt:安排1 tick后调用autoGraderInterrupt
  7. setInterruptHandler(Runnable handler):设置中断处理程序handler

nachos.threads

Alarm

  1. Alarm在系统中只有一个,它有两个功能:
    • 使用硬件的timer来提供preemption
    • 让一个thread sleep特定的时长
  2. timerInterrupt():产生一个时钟中断,每500 clock会被Machine的timer调用一次。会导致正在运行的进程yield,产生上下文切换。
  3. waitUntil(long x):让当前thread sleep至少x ticks,等待x ticks后时钟中断时唤醒当前进程

Communicator

  1. 让thread间同步地交换32 bits的数据
  2. 可以有多个thread等待speak,多个thread等待listen,但同一时刻不能有speaker和listener同时处于等待状态,因为这种情况下两者可以配对。
  3. speak():等待一个listener,然后和它配对,并传送给它数据。
  4. listen():等待一个speaker,然后和它配对,并接受它传送的数据。

InterruptPrivilege

  1. schedule(long when, String type, Runnable handler):在when时间之后产生一个type类型的中断,即执行handler这个中断处理程序。
  2. tick(boolean inKernelMode):根据当前是否是内核模式决定系统时间前进多少。

KThread

  1. runningThreads只是代表开始运行了并且尚未运行完的thread,不代表正在运行
  2. 一个KThread的状态有:new、ready、running、blocked、blocked
  3. yield():当前thread变成ready状态,执行下一个thread(让出CPU)
  4. sleep():当前thread变成blocked状态,执行下一个thread

Lock

  1. Lock的状态:free或busy
  2. acquire():等待直到Lock free,然后再将该Lock变成busy(也就是请求获得该Lock)
  3. release():将Lock变成free,若有thread在等待则唤醒它

ThreadQueue

  1. 多个Thread间争夺资源时放到这里(limited access时),常见情况如:
    • 同一时刻只能有一个KThread在运行
    • 同一时刻一个Lock只能被一个thread占有
  2. waitForAccess(KThread thread):该thread目前没有该access,申请该access
  3. KThread nextThread():将该access交给下一个thread,返回交给了谁
  4. void acquire(KThread thread):该thread获得了access,但不是从nextThread()中返回的Thread
  5. Print():打印队列中所有thread

nachos.security

Privilege

  1. 下列操作只能用doPrivileged()经Nachos security manager管理:
    • 创建thread
    • 读写test directory中的文件
    • 以特定返回值退出
  2. 下列操作可以直接用Privilege对象操作:
    • 调度中断
    • 系统时间前进
    • 获取系统statistics
    • 安装console
    • flush处理器的pipeline
    • 授权TCB的相关操作
  3. doPrivileged(Runnable action):Perform the specified action with privilege.
  4. Object doPrivileged(PrivilegedAction action):返回action的值
  5. Object doPrivileged(PrivilegedExceptionAction action)
  6. exit(int exitStatus):以特定状态退出系统
  7. addExitNotificationHandler(Runnable handler):系统退出时调用handler
  8. invokeExitNotificationHandlers():调用7中添加的handlers

Five tasks

Phase 1

Task 1

  1. public final void join(): This java thread join method puts the current thread on wait until the thread on which it’s called is dead. If the thread is interrupted, it throws InterruptedException.

Task 2: Condition Variables

  1. 该Task在Condition2中施展~
  2. sleep()的含义:释放Lock,本thread进入sleep状态,直到被另一个thread唤醒,唤醒之后该thread再次占有该Lock。
  3. sleep()的实现:
    • 关中断,保存中断状态
    • 释放Lock
    • 本thread进入队列等待再次获得Lock
    • 进入sleep状态
    • 再次请求获得Lock
    • 恢复中断状态
  4. wake()的含义:唤醒一个相关thread
  5. wake()的实现:
    • 关中断,保存中断状态
    • 获取一个正在等待的thread,让它ready
    • 恢复中断状态
      由于Lock又被刚刚ready的thread获得了,所以Lock状态不变
  6. wakeAll()的含义:唤醒所有相关threads
  7. wakeAll()的实现:不断wake直到等待队列为空

改进点:In Nachos, condition variables are summed to obey Mesa-style semantics.The advance to Mesa-style semantics is that it is a lot easier to implement.
可以尝试改成Hoare 方式
Alt text

Task 3: Alarm

  1. 该Task在Alarm中施展~
  2. 要求完成waitUtil()功能,只准修改waitUtil()和timer interrupt handler的代码
  3. 在Alarm中定义WaitThread类,记录了一个KThread和它需要被唤醒的时间。
  4. 定义一个优先队列waitedThreadPriQueue,用来存储所有等待被唤醒的WaitThread。
  5. waitUtil()的实现:
    • 关中断
    • 将currentThread封装的waitThread加入优先队列
    • 让currentThread sleep
    • 恢复中断状态
  6. Timer中的timerInterrupt添加的代码:
    • 关中断
    • 让优先队列中的所有该唤醒的ready,并从队列中pop
    • 恢复中断状态

猜你喜欢

转载自blog.csdn.net/gdymind/article/details/78264856
今日推荐