Today
-
CPU multiplexing: a historical perspective.
-
Preemption and context switching.
$ cat announce.txt
-
ASST1 is due Monday 2/22 @ 5PM.
-
We have office hours all afternoon and Monday afternoon.
-
test161 submit
should be available soon. (We hit a minor snag regarding key management.) -
We will have video lectures Monday and Wednesday next week. These lectures will also be posted to the class YouTube playlist.
Review: Interrupt Handling
-
Enters privileged mode,
-
records state necessary to allow the operating system to process the interrupt
-
jumps to a fixed memory location and begins executing instructions.
Review: Making System Calls
To access the kernel system call interface an application:
-
arranges arguments to the system call in an agreed-on place where the kernel can find them, typically in registers or on its stack,
-
loads a number identifying the system call it wants the kernel to perform into a pre-determined register, and
-
executes the
syscall
instruction.
Review: Software Exceptions v. Software Interrupts
-
Think of the CPU as saying to the kernel: "The
/bin/true
process would like your assistance."
-
Think of the CPU as saying to the kernel: "I need some help with this
/bin/false
process. It just tried to divide by zero and I think it needs to be terminated."
Now: Why Are We Doing All This Stuff?
-
CPU multiplexing: a historical perspective.
-
Preemption and context switching.
CPU Limitations: Number
-
Potentially the most expensive and complex component of the system!
-
Multiple cores have emerged as a solution to thermal- and energy-management issues caused by transistor density.
-
But generally, even if you have more than one, there are fewer cores than tasks to be run.
CPU Limitations: Number
CPU Limitations
-
It is way faster!
-
Faster than memory—usually addressed on the processor through out-of-order execution.
-
Way faster than disk—addressed by the operating system.
-
Way faster than you!—partially addressed by the operating system. (Nothing the operating system can really do about your limitations.)
Human Perceptual Limitations
Can you express these delays to a 1 GHz processor?
-
15 ms "rule of thumb": 15,000,000 clock cycles!
-
40 ms based on 25 frames-per-second for "smooth" video: 40,000,000 clock cycles!
-
100 ms was the rule for old telephone systems, the delay point after which human conversation patterns start to break down: 100,000,000 clock cycles!
Ancient History
The Harvard Mark I computer
The Garden
-
Long long ago, in a land before time, computers did not multitask. They did one thing at a time
-
That one thing had complete access to the machine in was running on.
-
The operating system—if there was one—was really just a library of routines to make programming easier. All abstractions, no multiplexing.
The Fall of Computing
-
At some point more people wanted to use the computer. That was annoying, because the geeks didn’t have it all to themselves anymore, but they just told people to form a line.
The Further Fall of Computing
-
At some point people wanted to interact with the computer, even multiple people interacting with the computer at the same time! Now what were the geeks to do?
Give the Geeks Some Credit
-
They solved these problems by building operating systems. Scheduling evolved to meet the needs of computer users.
The Rise of Multiprocessing
The Rise of Multiprocessing
Problems with Batch Scheduling
-
It is faster than other system components!
-
Inefficiency! Usage of slower parts of the system will cause the CPU to stall waiting for them to finish.
Solution: Context Switching
Birth of the Operating System
-
Operating systems emerged partly to hide delays caused by slow devices to keep the processor active.
-
Hiding processor delays requires only cooperative scheduling: threads only stop running when they require a long-latency operation.
Supporting Multiple Interactive Users
-
One task per user for multiple users, or
-
multiple tasks for a single user, or
-
multiple tasks for multiple users.
Supporting Multiple Interactive Users
The Illusion of Concurrency
-
Remember human perceptual limits?
-
The processors rapidly switches between tasks creating the notion of concurrency!
-
We refer to these transitions as context switches.
Implementing Context Switching
-
Hardware interrupts.
-
Software interrupts.
-
Software exceptions.
But what if these things don’t happen?
Timer Interrupts
-
Timer interrupts generated by a timer device ensure that the operating system regains control of the system at regular intervals.
-
Timer interrupts are the basis of preemptive scheduling: the operating system doesn’t wait for the thread to stop running, instead it preempts it.
-
Rest of interrupt handling is unchanged.
The Illusion of Concurrency
-
Timer interrupts mean that a running thread may be stopped at any time.
-
When the thread restarts we want it to appear that nothing has happened, that execution was not interrupted.
-
Of course other parts of the system might have changed, but the CPU state should be identical.
-
-
How do we do this?
Saving Thread State
-
Registers
-
Stack
We rely on memory protection to keep the stack unchanged until we restart the thread.
OS/161 Example
-
Saving thread state is the first thing that happens when the interrupt service routine is triggered. (Why?)
-
Saved state is sometimes known as a trap frame.
Saving Context: OS/161 Example
kern/arch/mips/locore/exception-mips1.S
Trap Frame: OS/161 Example
kern/arch/mips/include/trapframe.h
Context Switching
-
The kernel doesn’t trust (or want to pollute) the user thread’s stack.
-
Yes you can find that code. All the code is belong to you.
-
Exception return reloads the saved register state and then calls the MIPS instructions
rfe
, or return from exception.
Questions?
Next Time
-
Thread states and state transitions.
-
Threading implementations: user v. kernel.