Technical Women

038
Figure 1. Frances Allen

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

When an interrupt happens the CPU:
  1. Enters privileged mode,

  2. records state necessary to allow the operating system to process the interrupt

  3. jumps to a fixed memory location and begins executing instructions.

Review: Making System Calls

To access the kernel system call interface an application:

  1. arranges arguments to the system call in an agreed-on place where the kernel can find them, typically in registers or on its stack,

  2. loads a number identifying the system call it wants the kernel to perform into a pre-determined register, and

  3. executes the syscall instruction.

Review: Software Exceptions v. Software Interrupts

Interrupts are voluntary.
  • Think of the CPU as saying to the kernel: "The /bin/true process would like your assistance."

Exceptions are non-voluntary.
  • 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

(Historically) There is only one! Why?
  • Potentially the most expensive and complex component of the system!

(Recently) You may have many. Why?
  • 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

multicore

CPU Limitations

How does the CPU compare to other parts of the system—memory, disks, etc.?
  • 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

marki
Figure 2. 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.

peopleinline
Figure 3. People in 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?

terminalroom
Figure 4. Terminal Room

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

computerevolution single
Figure 5. Schedule the Humans

The Rise of Multiprocessing

computerevolution batch
Figure 6. Batch Scheduling

Problems with Batch Scheduling

Recall one of the "problems" with the CPU
  • It is faster than other system components!

What problem does this create for simple batch scheduling?
  • Inefficiency! Usage of slower parts of the system will cause the CPU to stall waiting for them to finish.

Solution: Context Switching

computerevolution cooperative
Figure 7. 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

Supporting multiple users requires the notion that multiple tasks are running simultaneously or concurrently, either:
  1. One task per user for multiple users, or

  2. multiple tasks for a single user, or

  3. multiple tasks for multiple users.

Supporting Multiple Interactive Users

computerevolution multiple
Figure 8. Multiple Jobs

The Illusion of Concurrency

How is this accomplished?
  • Remember human perceptual limits?

concurrency

concurrency 1

concurrency 2

  • The processors rapidly switches between tasks creating the notion of concurrency!

  • We refer to these transitions as context switches.

Implementing Context Switching

First problem: how does the operating system get control?
  1. Hardware interrupts.

  2. Software interrupts.

  3. 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

What does thread state consist of?
  • 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

contextswitch assembly

Trap Frame: OS/161 Example

kern/arch/mips/include/trapframe.h

trapframe

Context Switching

Threads switch to a separate kernel stack when executing in the kernel. Why?
  • The kernel doesn’t trust (or want to pollute) the user thread’s stack.

Do you think that you can find the corresponding OS/161 code that returns from an exception? What does it do?
  • 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.