Technical Women

003
Figure 1. Kristina Johnson

Today

  • Threads.

  • Threading implementations: user v. kernel.

  • Thread states.

Review: Thread Transitions

A transition between two threads is called a context switch.

Review: CPU Limitations

What are problems with the CPU that the operating system tries to correct?
  • There is only one, and

  • it is way faster than other parts of the system.

Review: Batch Scheduling

What is batch scheduling?
  • Running jobs sequentially until completion.

What is the problem with batch scheduling?
  • Slow devices can idle the CPU.

Review: The Illusion of Concurrency

How does the operating system create the illusion of concurrency?
  • By rapidly switching the CPU between multiple running tasks.

Review: Seize the Day

How does the operating system ensure that it retains control over what threads are using the CPU?
  • By using a periodic timer to generate hardware interrupts.

Review: 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.

Review: Saving Thread State

What does thread state consist of?
  • Registers

  • Stack

Review: Context Switch Overhead

  • Context switches are not free, require executing many instructions and saving fair amount of state.

  • Context switching creates the cost to enter the kernel.

How does this affect the rate at which the hardware timer fires to allow the kernel to (potentially) switch between threads?
  • Can’t go too fast otherwise overhead starts to dominate!

Questions: CPU Limitations, Concurrency, Context Switching?

Threads

So what is a thread?
  • Registers

  • Stack

How are each of the following shared between threads or processes?
  • Registers

  • Stack

  • Memory

  • File descriptor table.

Threads

process 0
process 1
process 2
process 3
process 4

Why Use Threads?

  1. Threads can be a good way of thinking about applications that do multiple things "simultaneously."

  2. Threads may naturally encapsulate some data about a certain thing that the application is doing.

  3. Threads may help applications hide or parallelize delays caused by slow devices.

Threads v. Processes Part II

Good example from Wikipedia: multiple threads within a single process are like multiple cooks trying to prepare the same meal together.

kitchen
  • Each one is doing one thing.

  • They are probably doing different things.

  • They all share the same recipe but may be looking at different parts of it.

  • They have private state but can communicate easily.

  • They must coordinate!

Meme

The OS corrupted

The cake

Aside: Threads v. Events

  • While threads are a reasonable way of thinking about concurrent programming, they are not the only way—​or even always the best way—​to make use of system resources.

  • Another approach is known as event-driven programming.

  • Anyone who has done JavaScript development or used frameworks like node.js has grown familiar with this programming model.

Events v. threads (over)simplified:
  • Threads can block, so we make use of the CPU by switching between threads!

  • Event handlers cannot block, so we can make use of the CPU by simply running events until completion.

Naturally Multithreaded Applications

Web server:
  • Use a separate thread to handle each incoming request.

Web browser:
  • Separate threads for each open tab.

  • When loading a page, separate threads to request and receive each unique part of the page.

Scientific applications:
  • Divide-and-conquer "embarrassingly parallelizable" data sets.

Why Not Processes?

  • IPC is more difficult because the kernel tries to protect processes from each other.

    • Inside a single process, anything goes!

  • State (what?) associated with processes doesn’t scale well.

Implementing Threads

Threads can be implemented in user space by unprivileged libraries.
  • This is the M:1 threading model, M user threads that look like 1 thread to the operating system kernel.

Threads can be implemented by the kernel directly.
  • This is the 1:1 threading model.

threadmodel base

threadmodel M1

threadmodel 11

Implementing Threads in User Space

How is this possible?
  • Doesn’t involve multiplexing between processes so no kernel privilege required!

How do I:
  • Save and restore context? This is just saving and restoring registers. The C library has an implementation called setjmp()/longjmp().

  • Preempt other threads? Use periodic signals delivered by the operating system to activate a user space thread scheduler.

Aside: setjmp()/longjmp() Wizardry

What will the following code do?

int main(int argc, void * argv) {
  int i, restored = 0;
  jump_buf saved;
  for (i = 0; i < 10; i++) {
    printf("Value of i is now %d\n", i);
    if (i == 5) {
      printf("OK, saving state...\n");
      if (setjmp(saved) == 0) {
        printf("Saved CPU state.\n");
        break;
      } else {
        printf("Restored CPU state.\n");
        restored = 1;
      }
    }
  }
  if (!restored) {
    longjmp(saved, 1);
  }
}
Value of i is now 0
Value of i is now 1
Value of i is now 2
Value of i is now 3
Value of i is now 4
Value of i is now 5
OK, saving state...
Saved CPU state.
Restored CPU state.
Value of i is now 6
Value of i is now 7
Value of i is now 8
Value of i is now 9
  • Use these tricks to impress your (new) friends!

  • (Or get rid of old ones…​)

!

image:http://i2.kym-cdn.com/entries/icons/original/000/011/057/unimpressed.PNG

Nailed the longjmp

Forgot the setjmp

Comparing Threading Implementations

M:1 (user space) threading

Pros:
  • Threading operations are much faster because they do not have to cross the user/kernel boundary.

  • Thread state can be smaller.

Cons:
  • Can’t use multiple cores!

  • Operating system may not schedule the application correctly because it doesn’t know about the fact that it contains more than one thread.

  • A single thread may block the entire process in the kernel when there are other threads that could run.

Comparing Threading Implementations

1:1 (kernel) threading

Pros:
  • Scheduling might improve because kernel can schedule all threads in the process.

Cons:
  • Context switch overhead for all threading operations.

Next: Thread Scheduling

  • We have discussed the mechanisms (context switching) used the multiplex the CPU…​

  • and the abstraction (threads).

  • We will start talking about scheduling next week: the policies the ensure that the multiplexing makes the best use of the available system resources.