Chapter 4: Threads
Chapter 4: Threads
Overview
Multithreading Models
Threading Issues
Pthreads
Windows XP Threads
Linux Threads
Java Threads
Operating System Concepts – 7th edition, Jan 23, 2005 4.2 Silberschatz, Galvin and Gagne ©2005
Thread
Thread is a lightweight process, it is apart of process.
A thread is a flow of control within a process.
The next slide illustrates the difference between a single-threaded
and multi-threaded processes.
The process model introduced in chapter three assumed
that a process was an executing program with a single
thread of control.
We can not run more than one thread at the same time if we have
only one processor.
Operating System Concepts – 7th edition, Jan 23, 2005 4.3 Silberschatz, Galvin and Gagne ©2005
Single and Multithreaded Processes
Files and signals are OS
resources
Operating System Concepts – 7th edition, Jan 23, 2005 4.4 Silberschatz, Galvin and Gagne ©2005
What resources are used when a thread is
created? How do they differ from those used
when a process is created?
Because a thread is smaller than a process, thread
creation typically uses fewer resources than process creation.
Creating a process requires allocating a process control block
(PCB) (PCB is a large data structure compared with the small
data structure needed for the creation of a thread).
The PCB includes a memory map, list of open files, and
environment variables.
Allocating and managing the memory map is typically The
most time-consuming activity.
Creating either a user or kernel thread involves allocating a
small data structure to hold a register set, stack, and priority.
Operating System Concepts – 7th edition, Jan 23, 2005 4.5 Silberschatz, Galvin and Gagne ©2005
Benefits of threads
Ex: any application in general is implemented as a separate process with
several threads of control. A word processor may have a thread for displaying
a picture, another thread for responding to keystrokes from the user, and a
third thread to perform spelling and grammar corrections.
Benefits of threads:
Responsiveness: multithreading an interactive application such a web
browser could still allow user interaction in one thread while an image was
being loaded in another thread. So the program will continue running even
if part of it is blocked or performing a lengthy process; this increase
responsiveness (response) to the user.
Resource Sharing: threads shares the same code, files, signals, and
data (see the previous figure).
Economy: allocation of memory and resources for process creation is
costly, so creating a number of threads that share some resources, then
context-switch between them will be effective in reducing such cost.
In Solaris creating a process is thirty times slower than is creating a
thread, and context switching is about five times slower.
Utilization of multiprocessor (MP) Architectures: two or more threads
can be running in parallel on different processors.
Operating System Concepts – 7th edition, Jan 23, 2005 4.6 Silberschatz, Galvin and Gagne ©2005
What’s the difference between a process
starting another copy of itself and
starting another thread?
A thread has access to the other memory
in the same process.
Processes have their own distinct memory.
Operating System Concepts – 7th edition, Jan 23, 2005 4.7 Silberschatz, Galvin and Gagne ©2005
User Threads
We have two types of threads:
User
Kernel
In user threads; thread management done by user-level threads
library, and above the kernel (without its support).
Three primary thread libraries:
POSIX Pthreads
Win32 threads
Java threads
Operating System Concepts – 7th edition, Jan 23, 2005 4.8 Silberschatz, Galvin and Gagne ©2005
What are differences between user-level
threads and kernel-levelthreads?
User-level threads are unknown by the kernel, whereas the
kernel is aware of kernel threads
Kernel threads need not be associated with a process whereas
every user thread belongs to a process.
Kernel threads are generally more expensive to maintain than user
threads as they must be represented with a kernel data structure.
Operating System Concepts – 7th edition, Jan 23, 2005 4.9 Silberschatz, Galvin and Gagne ©2005
Kernel Threads
Supported by the Kernel
Context switching between kernel threads typically requires saving
the value of the CPU registers from the thread being switched out
and restoring the CPU registers of the new thread being scheduled.
Examples
Windows XP/2000
Solaris
Linux
Tru64 UNIX
Mac OS X
Operating System Concepts – 7th edition, Jan 23, 2005 4.10 Silberschatz, Galvin and Gagne ©2005
Multithreading Models
There must exist a relationship between user and kernel threads,
this relation can be can be established using the following ways:
Many-to-One
One-to-One
Many-to-Many
Operating System Concepts – 7th edition, Jan 23, 2005 4.11 Silberschatz, Galvin and Gagne ©2005
Many-to-One
Many user-level threads mapped to single kernel thread
Examples:
Solaris Green Threads
Drawbacks:
Only one thread can access the kernel at a time; so multi
threads are unable to run on multiprocessors.
If the kernel thread makes a blocking system call then all user
threads will be blocked until the kernel thread is finished.
Benefits:
The management for the threads is done by the thread library
in user space, so it is efficient.
Operating System Concepts – 7th edition, Jan 23, 2005 4.12 Silberschatz, Galvin and Gagne ©2005
Many-to-One Model
Operating System Concepts – 7th edition, Jan 23, 2005 4.13 Silberschatz, Galvin and Gagne ©2005
One-to-One
Each user-level thread maps to kernel thread
Examples
Windows NT/XP/2000
Linux
Solaris 9 and later
Drawbacks:
Creating a user thread requires the creation of a kernel thread
(costly creation) which decrease the performance of the
application.
Benefits:
It allows multiple threads to run on parallel on multiprocessors.
If the kernel thread makes a blocking system call; then only one
user thread will be blocked until the kernel thread is finished,
the other threads can continue executing without any problems.
Operating System Concepts – 7th edition, Jan 23, 2005 4.14 Silberschatz, Galvin and Gagne ©2005
One-to-one Model
Operating System Concepts – 7th edition, Jan 23, 2005 4.15 Silberschatz, Galvin and Gagne ©2005
Many-to-Many Model
Allows many user level threads to be mapped to many kernel
threads (user threads are always more or equal to kernel
threads).
Allows the operating system to create a sufficient number of
kernel threads
Solaris prior to version 9
Windows NT/2000 with the ThreadFiber package
Drawbacks:
No drawbacks
Benefits:
It have the benefits of one to many and one to one
schemas.
Operating System Concepts – 7th edition, Jan 23, 2005 4.16 Silberschatz, Galvin and Gagne ©2005
Many-to-Many Model
Operating System Concepts – 7th edition, Jan 23, 2005 4.17 Silberschatz, Galvin and Gagne ©2005
Two-level Model
Similar to M:M, except that it allows a certain user thread
to be bound exclusively to a kernel thread
Examples
IRIX
HP-UX
Tru64 UNIX
Solaris 8 and earlier
Operating System Concepts – 7th edition, Jan 23, 2005 4.18 Silberschatz, Galvin and Gagne ©2005
Two-level Model
Operating System Concepts – 7th edition, Jan 23, 2005 4.19 Silberschatz, Galvin and Gagne ©2005
Java Thread States
Operating System Concepts – 7th edition, Jan 23, 2005 4.20 Silberschatz, Galvin and Gagne ©2005
End of Chapter 4