0% found this document useful (0 votes)
63 views44 pages

Lecture 6: Threads: Operating Systems (A) (Honor Track)

The document discusses threads as a way to improve process efficiency by separating process execution state from other process attributes, allowing multiple threads to run concurrently within a single process and share resources. It covers the differences between kernel threads managed by the operating system and user-level threads managed in a runtime library, and how both can be used together for fine-grained concurrency.

Uploaded by

sankarshan7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views44 pages

Lecture 6: Threads: Operating Systems (A) (Honor Track)

The document discusses threads as a way to improve process efficiency by separating process execution state from other process attributes, allowing multiple threads to run concurrently within a single process and share resources. It covers the differences between kernel threads managed by the operating system and user-level threads managed in a runtime library, and how both can be used together for fine-grained concurrency.

Uploaded by

sankarshan7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

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

A process is sometimes called a job or a task

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?

Ready: Waiting to be assigned to the CPU


Ready to execute, but another process is executing on the CPU

Waiting: Waiting for an event, e.g., I/O completion


It cannot make progress until event is signaled (disk completes)

As a process executes, it moves from state to


state
4

State Queues
Ready Queue

Firefox PCB

Disk I/O Queue

Emacs PCB

X Server PCB

Idle PCB

ls PCB

Console Queue

Sleep Queue

There may be many wait queues, one


for each type of wait (disk, console,
timer, network, etc.)
5

Process Data Structures


How does the OS represent a process in the kernel?
Process Control Block (PCB)
Contains all of the info about a process
Where the OS keeps all of a process hardware execution
state (PC, SP, regs, etc.) when the process is not running
This state is everything that is needed to restore the hardware to
the same configuration it was in when the process was switched
out of the hardware

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

When the OS stops running a process, it saves the current


values of the registers into the process PCB
When the OS is ready to start executing a new process, it
loads the hardware registers from the values stored in that
process PCB
The process of changing the CPU hardware state from one
process to another is called a context switch
This can happen 100 or 1000 times a second!
7

Buzz Words

Process

Execution state

Address space

Context switch

Process control block


(PCB)

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

Hand in a report showing what happen with different


Ns, with analysis
9

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

Creating a new process is costly because of all of the


data structures that must be allocated and initialized
Recall struct proc in Solaris

Communicating between processes is costly because


most communication goes through the OS
Overhead of system calls and copying data
12

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)

This situation is very inefficient


Space: PCB, page tables, etc.
Time: create data structures, fork and copy addr space, etc.

Solution: possible to have cooperating processes?

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

What dont they share?


Each has its own execution state: PC, SP, and registers

Key idea: Why dont we separate the concept of a process from


its execution state?
Process: address space, privileges, resources, etc.
Execution state: PC, SP, registers

Exec state also called thread of control, or thread


14

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)

A thread is bound to a single process


A process, however, can have multiple threads

Sharing: all the threads in a process share the address space,


privileges, resources
Threads become the unit of scheduling
Processes are now the containers in which threads execute
15

Threads: Lightweight Processes


A sequential execution stream within a process

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

The Thread Model


Shared information
Processor info: parent process, time, etc
Memory: segments, page table, and stats, etc
I/O and file: communication ports, directories and file descriptors, etc

Private state
State (ready, running and waiting)
Registers
Program counter
Execution stack
Why?

Each thread execute separately


18

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

Threads: Concurrent Servers


Using fork() to create new processes to handle requests in
parallel is overkill for such a simple task
Recall our forking Web server:
while (1) {
int sock = accept();
if ((child_pid = fork()) == 0) {
Handle client request
Close socket and exit
} else {
Close socket
}
}

Besides, can the parent process see the modification on global


variables in the child process?
20

Threads: Concurrent Servers


Instead, we can create a new thread for each request
web_server() {
while (1) {
int sock = accept();
thread_fork(handle_request, sock);
}
}

handle_request(int sock) {

Difference from fork()?

Process request
close(sock);
}
21

Thread Usage: Web Server

Figure 2-8. A multithreaded Web server.


Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

22

Thread Usage: word processor

Figure 2-7. A word processor with three threads.


A thread can wait for I/O, while the other threads can still running
What if it is single-threaded?
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

23

Windows Thread Lists from the


Performance Monitor

24

Windows Thread Lists from the


Performance Monitor (cont.)

25

Thread Information on Linux


Process information:
Read /proc/[PID]/stat file

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

As such, the OS now manages threads and processes


All thread operations are implemented in the kernel
The OS schedules all of the threads in the system

OS-managed threads are called kernel-level threads or


lightweight processes
NT: threads
Solaris: lightweight processes (LWP)
POSIX Threads (pthreads): PTHREAD_SCOPE_SYSTEM
27

Kernel Thread Limitations


Kernel-level threads make concurrency much cheaper than
processes
Much less state to allocate and initialize

However, for fine-grained concurrency, kernel-level threads


still suffer from too much overhead
Thread operations still require system calls
Ideally, want thread operations to be as fast as a procedure call
Kernel-level threads have to be general to support the needs of all
programmers, languages, runtimes, etc.

For more fine-grained concurrency, need even cheaper


threads
28

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)

User-level threads are small and fast


A thread is simply represented by a PC, registers, stack, and small
thread control block (TCB)
Creating a new thread, switching between threads, and synchronizing
threads are done via procedure call
No kernel involvement
User-level thread operations 100x faster than kernel threads
pthreads: PTHREAD_SCOPE_PROCESS
29

Implementing Threads in User Space

Figure 2-16. (a) A user-level threads package. (b) A threads


package managed by the kernel.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

30

U/L Thread Limitations


But, user-level threads are not a perfect solution
As with everything else, they are a tradeoff

User-level threads are invisible to the OS


They are not well integrated with the OS

As a result, the OS can make poor decisions


Scheduling a process with idle threads
Blocking a process whose thread initiated an I/O, even though the
process has other threads that can execute

Solving this requires communication between the kernel and


the user-level thread manager
31

Kernel vs. User Threads


Kernel-level threads
Integrated with OS (informed scheduling)
Slow to create, manipulate, synchronize

User-level threads
Fast to create, manipulate, synchronize
Not integrated with OS (uninformed scheduling)

Understanding the differences between kernel and


user-level threads is important
For programming (correctness, performance)
32

Kernel and User Threads


Or use both kernel and user-level threads
Can associate a user-level thread with a kernel-level thread
Or, multiplex user-level threads on top of kernel-level threads

Java Virtual Machine (JVM) (also pthreads)


Java threads are user-level threads
On older Unix, only one kernel thread per process
Multiplex all Java threads on this one kernel thread
On NT, modern Unix
Can multiplex Java threads on multiple kernel threads
Can have more Java threads than kernel threads

33

User and Kernel Threads

Multiplexing user-level threads


on a single kernel thread for
each process

Multiplexing user-level threads


on multiple kernel threads for
each process

34

Implementing Threads
Implementing threads has a number of issues
Interface
Context switch
Preemptive vs. non-preemptive
Scheduling
Synchronization

Focus on user-level threads now


Kernel-level threads are similar to original process management and
implementation in the OS

35

Sample Thread Interface


thread_fork(procedure_t)
Create a new thread of control
Also thread_create(), thread_setstate()

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

Run queue: Threads currently running (usually one)


Ready queue: Threads ready to run
Are there wait queues?
How would you implement thread_sleep(time)?

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();
}

What is the output of running these two threads?


38

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

So what does it mean for thread_yield to return?


It means that another thread called thread_yield!

Execution trace of ping/pong


printf(ping\n);
thread_yield();
printf(pong\n);
thread_yield();

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

The magic step is invoking context_switch()


Why do we need to call append_to_queue()?
40

Thread Context Switch


The context switch routine does all of the magic
Saves context of the currently running thread (old_thread)
Push all machine state onto its stack (not its TCB)
Restores context of the next thread
Pop all machine state from the next threads stack
The next thread becomes the current thread
Return to the NEW thread

This is all done in assembly language


It works at the level of the procedure calling convention, so it cannot be
implemented using procedure calls

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

Preemptive scheduling causes an involuntary context switch


Need to regain control of processor asynchronously
How?
Use timer interrupt
Timer interrupt handler forces current thread to call thread_yield
How do you do this?

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

Hand in a report showing what happen with different


Ns, with analysis on what are the differences between
threads and processes
43

Summary
The operating system as a large multithreaded program
Each process executes as a thread within the OS

Multithreading is also very useful for applications


Efficient multithreading requires fast primitives
Processes are too heavyweight

Solution is to separate threads from processes


All the threads in a process share the address space, privileges,
resources
Kernel-level threads much better, but still significant overhead
User-level threads even better, but not well integrated with OS

Now, how do we get our threads to correctly cooperate with


each other?
Next lecture: Synchronization I
44

You might also like