CS34

CS 134: Operating Systems Threads 2012-12-06

CS 134: Operating Systems Threads

1 / 23 Overview CS34 Overview Wiki Answers Questions Scheduler Questions Synchronization Questions

Overview Threads Concepts Uses Models Design 2012-12-06 Wiki Answers Thread Questions Scheduler Questions Synchronization Questions

Threads Concepts Uses Models Design

2 / 23 Wiki Answers Thread Questions

Thread Questions (1) CS34 Thread Questions (1) Wiki Answers What happens to a thread when it exits (i.e., calls thread_exit())? What about when it sleeps?

When a thread exits, it ensures the stack isn’t mangled, removes Thread Questions its space and destroys it, decrements the counter of whatever vnode it may be poitning at, puts itself into a zombie state, S_ZOMB, and preps itself to panic if it ever runs again Thread Questions (1) before it dies. When it sleeps, it makes sure it’s not in an handler, yields control to the next thread, enters the S_SLEEP state, and only starts taking control once more when wakeup() is called on its address. 2012-12-06 What happens to a thread when it exits (i.e., calls thread_exit())? What about when it sleeps?

When a thread exits, it ensures the stack isn’t mangled, removes its virtual memory space and destroys it, decrements the counter of whatever vnode it may be poitning at, puts itself into a zombie state, S_ZOMB, and preps itself to panic if it ever runs again before it dies. When it sleeps, it makes sure it’s not in an , yields control to the next thread, enters the S_SLEEP state, and only starts taking control once more when wakeup() is called on its address.

3 / 23 Wiki Answers Thread Questions

Thread Questions (2) CS34 Thread Questions (2) Wiki Answers What function(s) (s) a switch? Thread Questions There are two functions that handle a : mi_switch, which is the high level, machine-independent context switch function, and md_switch, which is the machine-independent code that actually does the context switch. mi_switch is in thread.c, and Thread Questions (2) md_switch is in pcb.c 2012-12-06

What function(s) handle(s) a context switch?

There are two functions that handle a context switch: mi_switch, which is the high level, machine-independent context switch function, and md_switch, which is the machine-independent code that actually does the context switch. mi_switch is in thread.c, and md_switch is in pcb.c

4 / 23 Wiki Answers Thread Questions

Thread Questions (3) CS34 Thread Questions (3) Wiki Answers How many thread states are there? What are they?

Thread Questions There are four thread states - S_RUN, S_READY, S_SLEEP, and S_ZOMB. These states are defined in kern/thread/thread.c. They express whether the thread is running, ready to run, sleeping, or a Thread Questions (3) zombie. 2012-12-06

How many thread states are there? What are they?

There are four thread states - S_RUN, S_READY, S_SLEEP, and S_ZOMB. These states are defined in kern/thread/thread.c. They express whether the thread is running, ready to run, sleeping, or a zombie.

5 / 23 Wiki Answers Thread Questions

CS34 Thread Questions (4) What does it mean to turn off? How is this Thread Questions (4) accomplished? Why is it important to turn off interrupts in the Wiki Answers thread subsystem code? If interrupts are turned off, then even if an interrupt is signaled the handler is not called until interrupts are turned back on. Interrupts Thread Questions are turned off using the function splhigh (set priority level high) and back on again using spl0 (set priority level zero). The priority What does it mean to turn interrupts off? How is this level can also be set to intermediate levels (or at least, it could if Thread Questions (4) OS/161 supported them) using the splx function. Turning off interrupts for thread operations is necessary to ensure that these operations complete successfully and aren’t broken mid-execution. For example, things could go pretty badly if the

accomplished? Why is it important to turn off interrupts in the 2012-12-06 scheduler interrupted us in the middle of a context switch and tried to start executing a thread that wasn’t finished setting up its stack. And it would be really awful if someone interrupted us in the thread subsystem code? middle of forking!

If interrupts are turned off, then even if an interrupt is signaled the handler is not called until interrupts are turned back on. Interrupts are turned off using the function splhigh (set priority level high) and back on again using spl0 (set priority level zero). The priority level can also be set to intermediate levels (or at least, it could if OS/161 supported them) using the splx function. Turning off interrupts for thread operations is necessary to ensure that these operations complete successfully and aren’t broken mid-execution. For example, things could go pretty badly if the scheduler interrupted us in the middle of a context switch and tried to start executing a thread that wasn’t finished setting up its stack. And it would be really awful if someone interrupted us in the

middle of forking! 6 / 23 Wiki Answers Thread Questions

Thread Questions (5) CS34 Thread Questions (5) Wiki Answers What happens when a thread wakes up another thread? How Thread Questions does a sleeping thread get to run again? It removes the sleeping thread from the queue, and calls make_runnable on the thread, which currently adds it to the end of the runqueue. The thread gets to run again when an mi_switch is Thread Questions (5) called, and that thread is returned by the scheduler. 2012-12-06

What happens when a thread wakes up another thread? How does a sleeping thread get to run again?

It removes the sleeping thread from the queue, and calls make_runnable on the thread, which currently adds it to the end of the runqueue. The thread gets to run again when an mi_switch is called, and that thread is returned by the scheduler.

7 / 23 Wiki Answers Scheduler Questions

Scheduler Questions (6) CS34 Scheduler Questions (6) Wiki Answers What function is responsible for choosing the next thread to run? Scheduler Questions How does that function pick the next thread? struct thread * scheduler(void); it uses a round-robin run queue that schedules each thread in the queue in equal time-slice Scheduler Questions (6) without priorities. 2012-12-06

What function is responsible for choosing the next thread to run? How does that function pick the next thread?

struct thread * scheduler(void); it uses a round-robin run queue that schedules each thread in the queue in equal time-slice without priorities.

8 / 23 Wiki Answers Scheduler Questions

Scheduler Questions (7) CS34 Scheduler Questions (7) Wiki Answers What role does the hardware timer play in ? What Scheduler Questions hardware independent function is called on a timer interrupt? The interrupt handler for the hardware timer calls hardclock, defined in src/kern/thread/hardclock.c. The method hardclock finishes by calling thread_yield every time it is run, forcing a Scheduler Questions (7) context switch. 2012-12-06

What role does the hardware timer play in scheduling? What hardware independent function is called on a timer interrupt?

The interrupt handler for the hardware timer calls hardclock, defined in src/kern/thread/hardclock.c. The method hardclock finishes by calling thread_yield every time it is run, forcing a context switch.

9 / 23 Wiki Answers Synchronization Questions

CS34 Synchronization Questions (8) Synchronization Questions (8) Describe how thread_sleep() and thread_wakeup() are used to implement semaphores. What is the purpose of the argument Wiki Answers passed to thread_sleep()?

thread_sleep is used in the P function of the semaphore. This Synchronization Questions function suspends the current thread until the semaphore count is greater than zero. thread_wakeup() is used in the V function of the semaphore. This function wakes up all the suspended threads waiting on the Synchronization Questions (8) current semaphore. Describe how thread_sleep() and thread_wakeup() are used to The addr argument that is passed in is the address of the object (in this case, semaphore) the sleeping thread is associated with. This is required so that when thread_wakeup is called on the 2012-12-06 same semaphore, it can selectively wake up only the threads implement semaphores. What is the purpose of the argument associated with that particular semaphore. passed to thread_sleep()?

thread_sleep is used in the P function of the semaphore. This function suspends the current thread until the semaphore count is greater than zero. thread_wakeup() is used in the V function of the semaphore. This function wakes up all the suspended threads waiting on the current semaphore. The addr argument that is passed in is the address of the object (in this case, semaphore) the sleeping thread is associated with. This is required so that when thread_wakeup is called on the same semaphore, it can selectively wake up only the threads associated with that particular semaphore.

10 / 23 Wiki Answers Synchronization Questions

CS34 Wiki Answers Why does the lock API in OS/161 provide lock_do_i_hold(), but Synchronization Questions not lock_get_holder()? ??? 2012-12-06

Why does the lock API in OS/161 provide lock_do_i_hold(), but not lock_get_holder()?

???

11 / 23 Wiki Answers Synchronization Questions

CS34 The thread subsystem in OS/161 uses a queue structure to manage some of its state. This queue structure does not contain Wiki Answers any synchronization primitives. Why not? Under what circumstances should you use a synchronized queue structure?

Synchronization Questions The runqueue queue used by the scheduler in the thread subsystem is only accessed by a single scheduler thread, so does not need any synchronization primitives to prevent other (non-existent) threads from messing up the queue. You should use a synchronized queue structure for any queue that multiple threads could access simultaneously.

The thread subsystem in OS/161 uses a queue structure to 2012-12-06 manage some of its state. This queue structure does not contain any synchronization primitives. Why not? Under what circumstances should you use a synchronized queue structure?

The runqueue queue used by the scheduler in the thread subsystem is only accessed by a single scheduler thread, so does not need any synchronization primitives to prevent other (non-existent) threads from messing up the queue. You should use a synchronized queue structure for any queue that multiple threads could access simultaneously.

12 / 23 Threads Concepts

Generalizing Processes CS34 Generalizing Processes Threads

Simple view of is Concepts Address space + Thread of execution Generalizing Processes Does the mapping need to be one-to-one? 2012-12-06

Simple view of process is Address space + Thread of execution

Does the mapping need to be one-to-one?

13 / 23 Threads Concepts

Possible Mappings CS34 Possible Mappings Threads Concepts Possible Mappings 2012-12-06

one process one process one thread multiple threads

multiple processes multiple processes one thread per process multiple threads per process

14 / 23 Threads Concepts

Threads CS34 Threads Threads Concepts Motivation: I Traditional processes: Virtual uniprocessor machine

I Multithreaded processes: Virtual multiprocessor machine Threads 2012-12-06

Motivation:

I Traditional processes: Virtual uniprocessor machine

I Multithreaded processes: Virtual multiprocessor machine

15 / 23 Threads Uses

Uses of Threads CS34 Uses of Threads Threads Various reasons why people use threads Uses I Performing foreground and background work I Supporting asynchronous processing

I Speeding execution Uses of Threads I Organizing programs 2012-12-06

Various reasons why people use threads

I Performing foreground and background work

I Supporting asynchronous processing

I Speeding execution

I Organizing programs

16 / 23 Threads Uses

Uses of Threads—Example CS34 Uses of Threads—Example Threads Uses Uses of Threads—Example /* Dispatcher Thread */ /* Worker Thread */ \\ Web server process for ( ; ; ) { for ( ; ; ) { url = get_next_request(); url = wait_for_work(); handoff_work(url); page = look_in_cache(url); } if (page == NULL) 2012-12-06 page = generate_page(url); send_page(page); } Dispatcher thread

Worker thread

Web page cache

Kernel Kernel space

Network connection

/* Dispatcher Thread */ /* Worker Thread */ \\ for ( ; ; ) { for ( ; ; ) { url = get_next_request(); url = wait_for_work(); handoff_work(url); page = look_in_cache(url); } if (page == NULL) page = generate_page(url); send_page(page); }

17 / 23 Threads Uses

Class Exercise CS34 Class Exercise Threads

Can an application implement threads without built-in thread Uses support in the OS? Class Exercise If so, what does it need from the from the OS to support threads? 2012-12-06

Can an application implement threads without built-in thread support in the OS?

If so, what does it need from the from the OS to support threads?

18 / 23 Threads Models

Model for User Threads CS34 Model for User Threads Threads Key: Models Model for User Threads

2012-12-06 Class Exercise What are the pros and cons of this approach?

So, maybe we should put the threads in the kernel? Threads User Key: Library Space User-level thread Kernel Space Kernel-level thread P Process P Pure user-level

Class Exercise What are the pros and cons of this approach?

19 / 23 Threads Models

Model for User Threads CS34 Model for User Threads Threads Key: Models Model for User Threads

+ No kernel overhead for thread library calls 2012-12-06 + Own scheduler = Application-specific scheduling policy? − I/O issues − Can’t (easily) take advantage of multiprocessing

So, maybe we should put the threads in the kernel? Threads User Key: Library Space User-level thread Kernel Space Kernel-level thread P Process P Pure user-level

+ No kernel overhead for thread library calls + Own scheduler = Application-specific scheduling policy? − I/O issues − Can’t (easily) take advantage of multiprocessing 19 / 23 Threads Models

Model for Kernel-Level Threads CS34 Model for Kernel-Level Threads Threads Key: Models Model for Kernel-Level Threads

2012-12-06 Class Exercise What are the pros and cons of this approach?

User Space Key: Kernel User-level thread Space Kernel-level thread P Process P Pure kernel-level

Class Exercise What are the pros and cons of this approach?

20 / 23 Threads Models

Model for Kernel-Level Threads CS34 Model for Kernel-Level Threads Threads Key: Models Model for Kernel-Level Threads

Now we have kernel overheads:

2012-12-06 I Kernel data structures I Mode switch to kernel

User Space Key: Kernel User-level thread Space Kernel-level thread P Process P Pure kernel-level

Now we have kernel overheads: I Kernel data structures I Mode switch to kernel

20 / 23 Threads Models

Hybrid Thread Schemes CS34 Hybrid Thread Schemes Threads Models Key: Hybrid Thread Schemes 2012-12-06 Class Exercise What are the pros and cons of this approach?

Threads User Library Space Kernel Key: Space User-level thread Kernel-level thread P P P Process

Combined

Class Exercise What are the pros and cons of this approach?

21 / 23 Multithreaded Process Model Thread Thread Thread Thread Thread Thread Control Control Control Block Block Block

Process User User User Control Stack Stack Stack Block

User Kernel Kernel Kernel Address Stack Stack Stack Space Class Question But what’s per-process and what’s per-thread?

Class Question But what’s per-process and what’s per-thread?

Threads Models

Traditional vs. Multithreaded Processes CS34 Traditional vs. Multithreaded Processes Threads Models Traditional vs. Multithreaded Processes Single-Threaded 2012-12-06 Process Model

Process User Control Stack Block

User Kernel Address Stack Space

22 / 23 Threads Models

Traditional vs. Multithreaded Processes CS34 Traditional vs. Multithreaded Processes Threads Models Traditional vs. Multithreaded Processes Single-Threaded Multithreaded 2012-12-06 Class Question Process Model Process Model But what’s per-process and what’s per-thread? Thread Thread Thread Thread Thread Thread Process User Control Control Control Stack Block Block Block Control Block Process User User User Stack Stack Stack User Kernel Control Block Address Stack Space User Kernel Kernel Kernel Address Stack Stack Stack Space Class Question But what’s per-process and what’s per-thread?

22 / 23 Threads Design

CS34 Per-Process vs. Per-Thread—You Decide. . . Per-Process vs. Per-Thread—You Decide. . . I Execution state I I/O State Threads I Registers I File descriptors I I Working directory I Program status word I Root directory I Stack pointer I Event Notifications I Scheduling information Design I Signals waiting I Process state I Signal mask I Priority I Time of next alarm I Class, etc. I Other I Memory Per-Process vs. Per-Thread—You Decide. . . I Process ID I Text area I Parent process I Data area I Process group I Stack area I Execution state I I/O State I Controlling terminal I Security/Authentication Info 2012-12-06 I Start time I User ID I CPU time I Group ID I Children’s CPU time I Registers I File descriptors I Program counter I Working directory I Program status word I Root directory I Stack pointer I Event Notifications

I Scheduling information I Signals waiting I Process state I Signal mask I Priority I Time of next alarm I Class, etc. I Other I Memory I Process ID I Text area I Parent process I Data area I Process group I Stack area I Controlling terminal I Security/Authentication Info I Start time I User ID I CPU time I Group ID I Children’s CPU time

23 / 23