Lecture 6: Threads: Operating Systems (A) (Honor Track)
Lecture 6: Threads: Operating Systems (A) (Honor Track)
Lecture 6: Threads
Tao Wang
School of Electronics Engineering and Computer Science
http://ceca.pku.edu.cn/wangtao
Fall 2013
Acknowledgements: Prof. Xiangqun Chen at PKU and Prof. Yuanyuan Zhou at UCSD
Review
Processes
The Process
The process is the OS abstraction for execution
It is the unit of execution
It is the dynamic execution context of a program
Process State
A process has an execution state that indicates
what it is currently doing
Running: Executing instructions on the CPU
It is the process that has control of the CPU
How many processes can be in the running state simultaneously?
State Queues
Ready Queue
Firefox PCB
Emacs PCB
X Server PCB
Idle PCB
ls PCB
Console Queue
Sleep Queue
Context Switch
When a process is running, its hardware state (PC, SP, regs,
etc.) is in the CPU
The hardware registers contain the current values
Buzz Words
Process
Execution state
Address space
Context switch
Practice
Create N processes in BOTH Linux and Windows,
printing Hello world! My process ID is xxx (xxx
should be the newly created process ID)
N=1
N = 10
N = 100
N = 1000
This Lecture
Threads
10
Buzz Words
Thread
Multithreading
Sharing
Scheduling
11
Issues in Processes
Recall that a process includes many things
An address space (defining all the code and data pages)
OS resources (e.g., open files) and accounting information
Execution state (PC, SP, regs, etc.)
A Parallel Program
To execute a parallel program, we need to
Create several processes that execute in parallel
Have the OS schedule these processes in parallel (logically or
physically)
13
Rethinking Processes
What is similar in these cooperating processes?
They all share the same code and data (address space)
They all share the same privileges
They all share the same resources (files, sockets, etc.)
Threads
Modern OSes (Mach, Chorus, NT, modern Unix) separate the
concepts of processes and threads
The thread defines a sequential execution stream within a process (PC,
SP, registers)
The process defines the address space and general process attributes
(everything but threads of execution)
Environment (resource)
execution
Figure 2-11. (a) Three processes each with one thread. (b) One
process with three threads.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
16
Analogy
Process:
Hire 4 software engineers to work on 4 projects (same or
different)
More expensive
Require communication
Threads
Have one super-engineer to work on
4 projects (multi-tasking)
Cheaper
More efficient (if the 4 projects
need to cooperate)
17
Private state
State (ready, running and waiting)
Registers
Program counter
Execution stack
Why?
Threads in a Process
Stack (T1)
Thread 2
Thread 1
Stack (T2)
Stack (T3)
Thread 3
Heap
Static Data
PC (T3)
PC (T2)
Code
PC (T1)
19
handle_request(int sock) {
Process request
close(sock);
}
21
22
23
24
25
Thread information:
Read /proc/[PID]/task/[thread ID]/stat
26
Kernel-Level Threads
We have taken the execution aspect of a process and separated
it out into threads
To make concurrency cheaper
User-Level Threads
To make threads cheap and fast, they need to be
implemented at user level
Kernel-level threads are managed by the OS
User-level threads are managed entirely by the run-time system
(user-level library)
30
User-level threads
Fast to create, manipulate, synchronize
Not integrated with OS (uninformed scheduling)
33
34
Implementing Threads
Implementing threads has a number of issues
Interface
Context switch
Preemptive vs. non-preemptive
Scheduling
Synchronization
35
thread_stop()
Stop the calling thread; also thread_block
thread_start(thread_t)
Start the given thread
thread_yield()
Voluntarily give up the processor
thread_exit()
Terminate the calling thread; also thread_destroy
36
Thread Scheduling
The thread scheduler determines when a thread runs
It uses queues to keep track of what threads are doing
Just like the OS and processes
But it is implemented at user-level in a library
37
Non-Preemptive Scheduling
Threads voluntarily give up the CPU with
thread_yield
Ping Thread
while (1) {
Pong Thread
while (1) {
printf(ping\n);
printf(pong\n);
thread_yield();
thread_yield();
}
thread_yield()
Wait a second. How does thread_yield() work?
The semantics of thread_yield are that it gives up the CPU
to another thread
In other words, it context switches to another thread
39
Implementing thread_yield()
thread_yield() {
thread_t old_thread = current_thread;
current_thread = get_next_thread();
append_to_queue(ready_queue, old_thread);
As old thread
context_switch(old_thread, current_thread);
return;
As new thread
41
Preemptive Scheduling
Non-preemptive threads have to voluntarily give up CPU
A long-running thread will take over the machine
Only voluntary calls to thread_yield(), thread_stop(), or thread_exit()
causes a context switch
42
Practice
Create N threads in BOTH Linux and Windows,
printing Hello world! My thread ID is xxx (xxx
should be the newly created threads ID)
N=1
N = 10
N = 100
N = 1000
Summary
The operating system as a large multithreaded program
Each process executes as a thread within the OS