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
- Mesastyle locks and condition variables using semaphores(provide solutions to a number of concurrency problems)
- The thread system is based on FastThreads [Anderson et al 1989]
- 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
- 创建nachos.machine
- 硬件初始化:the interrupt controller, timer, MIPS processor, console, and file system
- 启动AutoGrader
Interrupt Management
- maintain an event queue together with a simulated clock
- clock ticks
→ examine the queue→ an event takes place - 每次中断10 ticks;每次执行指令1 tick
Device Event Handler
- 每次clock改变,就通过调用Device Event Handler检查interrupt events
- device event handler是对硬件的模拟,interrupt handler是软件。前者调用后者
the interrupt class在调用device event handler之前会关中断
Lib常用程序
- Lib.assertTrue断言,假的时候退出
- currentTCB在线程切换的时候可能有滞后现象
c++版实验指导书
Chapter 1 Process and Thread
- Thread的状态:
61 enum ThreadStatus { JUST_CREATED, RUNNING, READY, BLOCKED };
其中,BLOCKED
就是等待状态。 - 当一个Thread的状态从JUST CREATED变成READY时,Nachos为其分配stack
- Threads的constructor只是指明了thread name (为了debug)并设置状态为JUST CREATED。
- 在
machineState[18]
数组中存有寄存器变量:
- stack:指明栈底
- stackTop:指明栈顶(SP)
- program counter (PC)
代码阅读
nachos.machine
Timer
- 硬件计时器,大约每500 ticks产生一个时钟中断(不是精确的500 ticks)
- 作用:
- 实现time-slicing
- 让一个thread sleep特定的时长
Timer(Privilege privilege)
:构造器,执行了以下内容:
- 得到了privilege
- 生成了时钟中断和autoGraderInterrupt,并调用时钟中断
timerInterrupt()
:
- 安排下次时钟中断和autoGraderInterrupt
- 更新timer最后一次中断的时间(即ticks数量)
- 执行中断处理程序handler
scheduleInterrupt()
:安排500ticks后调用timerInterruptscheduleAutoGraderInterrupt
:安排1 tick后调用autoGraderInterruptsetInterruptHandler(Runnable handler)
:设置中断处理程序handler
nachos.threads
Alarm
- Alarm在系统中只有一个,它有两个功能:
- 使用硬件的timer来提供preemption
- 让一个thread sleep特定的时长
timerInterrupt()
:产生一个时钟中断,每500 clock会被Machine的timer调用一次。会导致正在运行的进程yield,产生上下文切换。waitUntil(long x)
:让当前thread sleep至少x ticks,等待x ticks后时钟中断时唤醒当前进程
Communicator
- 让thread间同步地交换32 bits的数据
- 可以有多个thread等待speak,多个thread等待listen,但同一时刻不能有speaker和listener同时处于等待状态,因为这种情况下两者可以配对。
speak()
:等待一个listener,然后和它配对,并传送给它数据。listen()
:等待一个speaker,然后和它配对,并接受它传送的数据。
InterruptPrivilege
schedule(long when, String type, Runnable handler)
:在when
时间之后产生一个type
类型的中断,即执行handler
这个中断处理程序。tick(boolean inKernelMode)
:根据当前是否是内核模式决定系统时间前进多少。
KThread
- runningThreads只是代表开始运行了并且尚未运行完的thread,不代表正在运行
- 一个KThread的状态有:new、ready、running、blocked、blocked
yield()
:当前thread变成ready状态,执行下一个thread(让出CPU)sleep()
:当前thread变成blocked状态,执行下一个thread
Lock
- Lock的状态:free或busy
acquire()
:等待直到Lock free,然后再将该Lock变成busy(也就是请求获得该Lock)release()
:将Lock变成free,若有thread在等待则唤醒它
ThreadQueue
- 多个Thread间争夺资源时放到这里(limited access时),常见情况如:
- 同一时刻只能有一个KThread在运行
- 同一时刻一个Lock只能被一个thread占有
waitForAccess(KThread thread)
:该thread目前没有该access,申请该accessKThread nextThread()
:将该access交给下一个thread,返回交给了谁void acquire(KThread thread)
:该thread获得了access,但不是从nextThread()
中返回的ThreadPrint()
:打印队列中所有thread
nachos.security
Privilege
- 下列操作只能用
doPrivileged()
经Nachos security manager管理:
- 创建thread
- 读写test directory中的文件
- 以特定返回值退出
- 下列操作可以直接用
Privilege
对象操作:
- 调度中断
- 系统时间前进
- 获取系统statistics
- 安装console
- flush处理器的pipeline
- 授权TCB的相关操作
doPrivileged(Runnable action)
:Perform the specified action with privilege.Object doPrivileged(PrivilegedAction action)
:返回action的值Object doPrivileged(PrivilegedExceptionAction action)
exit(int exitStatus)
:以特定状态退出系统addExitNotificationHandler(Runnable handler)
:系统退出时调用handlerinvokeExitNotificationHandlers()
:调用7中添加的handlers
Five tasks
Phase 1
Task 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
- 该Task在Condition2中施展~
sleep()
的含义:释放Lock,本thread进入sleep状态,直到被另一个thread唤醒,唤醒之后该thread再次占有该Lock。sleep()
的实现:
- 关中断,保存中断状态
- 释放Lock
- 本thread进入队列等待再次获得Lock
- 进入sleep状态
- 再次请求获得Lock
- 恢复中断状态
wake()
的含义:唤醒一个相关threadwake()
的实现:
- 关中断,保存中断状态
- 获取一个正在等待的thread,让它ready
- 恢复中断状态
由于Lock又被刚刚ready的thread获得了,所以Lock状态不变
wakeAll()
的含义:唤醒所有相关threadswakeAll()
的实现:不断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 方式
Task 3: Alarm
- 该Task在Alarm中施展~
- 要求完成waitUtil()功能,只准修改waitUtil()和timer interrupt handler的代码
- 在Alarm中定义WaitThread类,记录了一个KThread和它需要被唤醒的时间。
- 定义一个优先队列waitedThreadPriQueue,用来存储所有等待被唤醒的WaitThread。
waitUtil()
的实现:
- 关中断
- 将currentThread封装的waitThread加入优先队列
- 让currentThread sleep
- 恢复中断状态
- Timer中的
timerInterrupt
添加的代码:
- 关中断
- 让优先队列中的所有该唤醒的ready,并从队列中pop
- 恢复中断状态