You are on page 1of 18

Operating Systems

Lecture 13
Threads
Read Ch 5.1 - 5.3

Silberschatz, Galvin and Gagne 2002


Operating System Concepts 5.1 Modified for CSCI 399, Royden, 2005
Threads

 A thread (a lightweight process) is a basic unit of CPU


utilization.
 A thread has a single sequential flow of control.
 A thread is comprised of: A thread ID, a program counter, a
register set and a stack.
 A process is the execution environment in which threads run.
 (Recall previous definition of process: program in execution).
 The process has the code section, data section, OS resources
(e.g. open files and signals).
 Traditional processes have a single thread of control
 Multi-threaded processes have multiple threads of control
 The threads share the address space and resources of the process
that owns them.

Silberschatz, Galvin and Gagne 2002


Operating System Concepts 5.2 Modified for CSCI 399, Royden, 2005
Single and Multithreaded Processes

Silberschatz, Galvin and Gagne 2002


Operating System Concepts 5.3 Modified for CSCI 399, Royden, 2005
Processes vs. Threads

Which of the following belong to the process and which to


the thread?

Program code: Process


local or temporary data: Thread
global data: Process
allocated resources: Process
execution stack: Thread
memory management info: Process
Program counter: Thread
Parent identification: Process
Thread state: Thread
Registers: Thread

Silberschatz, Galvin and Gagne 2002


Operating System Concepts 5.4 Modified for CSCI 399, Royden, 2005
Control Blocks

The thread control block (TCB) contains:


Thread state, Program Counter, Registers

PCB' = everything else (e.g. process id, open files, etc.)

The process control block (PCB) = PCB' U TCP

Silberschatz, Galvin and Gagne 2002


Operating System Concepts 5.5 Modified for CSCI 399, Royden, 2005
Why use threads?

 Because threads have minimal internal state, it


takes less time to create a thread than a
process (10x speedup in UNIX).
 It takes less time to terminate a thread.
 It takes less time to switch to a different thread.
 A multi-threaded process is much cheaper than
multiple (redundant) processes.

Silberschatz, Galvin and Gagne 2002


Operating System Concepts 5.6 Modified for CSCI 399, Royden, 2005
Examples of Using Threads

 Threads are useful for any application with multiple tasks


that can be run with separate threads of control.
 A Word processor may have separate threads for:
 User input
 Spell check
 grammar check
 displaying graphics
 document layout
 A web server may spawn a thread for each client
 Can serve clients concurrently with multiple threads.
 It takes less overhead to use multiple threads than to
use multiple processes.
Silberschatz, Galvin and Gagne 2002
Operating System Concepts 5.7 Modified for CSCI 399, Royden, 2005
Benefits
 Responsiveness:
 Threads allow a program to continue running even if part is
blocked.
 For example, a web browser can allow user input while loading
an image.
 Resource Sharing:
 Threads share memory and resources of the process to which
they belong.
 Economy:
 Allocating memory and resources to a process is costly.
 Threads are faster to create and faster to switch between.
 Utilization of Multiprocessor Architectures:
 Threads can run in parallel on different processors.
 A single threaded process can run only on one processor no
matter how many are available.
Silberschatz, Galvin and Gagne 2002
Operating System Concepts 5.8 Modified for CSCI 399, Royden, 2005
User Threads

 Thread management may be done at either the user


level or the kernel (OS) level.

User Threads:
 Thread management done by user-level threads library

 The kernel is unaware that the process is multithreaded.

 Thread creation and scheduling is done in user space


without kernel intervention.

Silberschatz, Galvin and Gagne 2002


Operating System Concepts 5.9 Modified for CSCI 399, Royden, 2005
Many-to-One Model

Silberschatz, Galvin and Gagne 2002


Operating System Concepts 5.10 Modified for CSCI 399, Royden, 2005
States of User Threads and
Processes
 What happens when a process or thread is blocked?
 Suppose process B has two threads, of which thread 2 is running.
The following conditions could occur:
 Thread 2 makes an I/O or other system call:
 The kernel puts process B into a blocked state.
 The thread data structure will still show thread 2 in the running state.
 Thread 2 is not actually running. (The entire process is blocked).
 Process B exhausts its time slice:
 The kernel puts process B into the ready state.
 Thread 2 still in running state (but not actually running!)
 Thread 2 needs action performed by Thread 1:
 Thread 2 goes into blocked state.
 Thread 1 starts running.
 Process B remains in running state.

Silberschatz, Galvin and Gagne 2002


Operating System Concepts 5.11 Modified for CSCI 399, Royden, 2005
Pros and Cons of User Level Threads:

 Pros:
 Fast switch between threads (kernel is not needed).
 Can have multi-threaded process on a system that does
not understand threads.
 Cheap to create and destroy threads.
 User has complete control over threads (e.g. can assign
priorities, etc.)
 Cons:
 System call from one thread blocks all threads in the
same process.
 Process cannot use multiple processors.
 Extra work for user.

Silberschatz, Galvin and Gagne 2002


Operating System Concepts 5.12 Modified for CSCI 399, Royden, 2005
User Level Thread Libraries

 User Level Thread Libraries include:


 POSIX Pthreads
 Mach C-Threads
 Solaris-2 UI threads.

Silberschatz, Galvin and Gagne 2002


Operating System Concepts 5.13 Modified for CSCI 399, Royden, 2005
Kernel Level Threads

Kernel Level Threads:


 All thread management is done by the Operating
System.
 Each user-level thread maps to kernel thread.
 Kernel does creation, scheduling and management of
threads.
 Examples
- Windows 95/98/NT/2000
- OS/2
- Solaris-2
- Some flavors of UNIX

Silberschatz, Galvin and Gagne 2002


Operating System Concepts 5.14 Modified for CSCI 399, Royden, 2005
One-to-one Model

Silberschatz, Galvin and Gagne 2002


Operating System Concepts 5.15 Modified for CSCI 399, Royden, 2005
Pros and Cons of Kernel Level Threads

 Pros:
 System call from a thread does not block other threads
in the same process.
 One process can use multiple processors.
 Create/destroy/switch of threads is less expensive than
for processes.
 Cons:
 Create/destroy/switch of threads is more expensive
than for user level threads.
 CPU scheduling algorithms are unfair: Each thread is
given the same time slice. Tasks with more threads are
given more CPU time than those with fewer threads.

Silberschatz, Galvin and Gagne 2002


Operating System Concepts 5.16 Modified for CSCI 399, Royden, 2005
Combining User and Kernel Level
Threads
 User and Kernel level threads execute simultaneously.
 Allows many user level threads to be mapped to many
kernel threads.
 If one user level thread is blocked, another kernel thread
may be chosen to execute.
 Assigning a User Level thread to a Kernel Level thread is
implicit and hidden from the programmer.
 Examples of systems with combination of User and
Kernel Level threads:
 Solaris 2
 Windows NT/2000 with the ThreadFiber package
 Some other UNIX flavors.

Silberschatz, Galvin and Gagne 2002


Operating System Concepts 5.17 Modified for CSCI 399, Royden, 2005
Many-to-Many Model

Silberschatz, Galvin and Gagne 2002


Operating System Concepts 5.18 Modified for CSCI 399, Royden, 2005

You might also like