Professional Documents
Culture Documents
New ch4 Updated
New ch4 Updated
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
Processes
Operating System Concepts – 7th edition, Jan 23, 2005 4.3 Silberschatz, Galvin and Gagne ©2005
Parallel Processes
Operating System Concepts – 7th edition, Jan 23, 2005 4.4 Silberschatz, Galvin and Gagne ©2005
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 don’t they share?
Each has its own execution state: PC, SP, and registers
Key idea: Why don’t 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
A thread is bound to a single process
Processes, however, can have multiple threads
Threads become the unit of scheduling
Processes are now the containers in which threads execute
Operating System Concepts – 7th edition, Jan 23, 2005 4.5 Silberschatz, Galvin and Gagne ©2005
Conclusion
Operating System Concepts – 7th edition, Jan 23, 2005 4.6 Silberschatz, Galvin and Gagne ©2005
Threads in a Process
Operating System Concepts – 7th edition, Jan 23, 2005 4.7 Silberschatz, Galvin and Gagne ©2005
Multithreading
Operating System Concepts – 7th edition, Jan 23, 2005 4.8 Silberschatz, Galvin and Gagne ©2005
Single and Multithreaded Processes
Operating System Concepts – 7th edition, Jan 23, 2005 4.9 Silberschatz, Galvin and Gagne ©2005
Multithreading Benefits
Operating System Concepts – 7th edition, Jan 23, 2005 4.10 Silberschatz, Galvin and Gagne ©2005
Thread Design Space
Operating System Concepts – 7th edition, Jan 23, 2005 4.11 Silberschatz, Galvin and Gagne ©2005
Process/Thread Separation
Operating System Concepts – 7th edition, Jan 23, 2005 4.12 Silberschatz, Galvin and Gagne ©2005
Threads: Concurrent Servers
Operating System Concepts – 7th edition, Jan 23, 2005 4.13 Silberschatz, Galvin and Gagne ©2005
Threads: Concurrent Servers
Operating System Concepts – 7th edition, Jan 23, 2005 4.14 Silberschatz, Galvin and Gagne ©2005
Kernel 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
Kernel threads – supported and managed directly by the operating system.
Almost all contemporary operating systems support kernel threads.
Windows XP/2000
Solaris
Linux
Tru64 UNIX
Mac OS X
Operating System Concepts – 7th edition, Jan 23, 2005 4.15 Silberschatz, Galvin and Gagne ©2005
Kernel Thread Limitations
Operating System Concepts – 7th edition, Jan 23, 2005 4.16 Silberschatz, Galvin and Gagne ©2005
User-Level Threads
Operating System Concepts – 7th edition, Jan 23, 2005 4.17 Silberschatz, Galvin and Gagne ©2005
User-Level Thread Limitations
Operating System Concepts – 7th edition, Jan 23, 2005 4.18 Silberschatz, Galvin and Gagne ©2005
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)
For test-taking
Best to use both kernel and user level threads
Multithreading models (next slides)
Operating System Concepts – 7th edition, Jan 23, 2005 4.19 Silberschatz, Galvin and Gagne ©2005
Thread Libraries
Operating System Concepts – 7th edition, Jan 23, 2005 4.20 Silberschatz, Galvin and Gagne ©2005
Pthreads
Operating System Concepts – 7th edition, Jan 23, 2005 4.21 Silberschatz, Galvin and Gagne ©2005
Multithreading Models
One-to-One
Many-to-Many
Operating System Concepts – 7th edition, Jan 23, 2005 4.22 Silberschatz, Galvin and Gagne ©2005
Many-to-One
Operating System Concepts – 7th edition, Jan 23, 2005 4.23 Silberschatz, Galvin and Gagne ©2005
Many-to-One Model
Operating System Concepts – 7th edition, Jan 23, 2005 4.24 Silberschatz, Galvin and Gagne ©2005
One-to-One
Operating System Concepts – 7th edition, Jan 23, 2005 4.25 Silberschatz, Galvin and Gagne ©2005
One-to-one Model
Operating System Concepts – 7th edition, Jan 23, 2005 4.26 Silberschatz, Galvin and Gagne ©2005
Many-to-Many Model
Operating System Concepts – 7th edition, Jan 23, 2005 4.27 Silberschatz, Galvin and Gagne ©2005
Many-to-Many Model
Operating System Concepts – 7th edition, Jan 23, 2005 4.28 Silberschatz, Galvin and Gagne ©2005
Multithreading Models
Operating System Concepts – 7th edition, Jan 23, 2005 4.29 Silberschatz, Galvin and Gagne ©2005
Two-level Model
Operating System Concepts – 7th edition, Jan 23, 2005 4.30 Silberschatz, Galvin and Gagne ©2005
Two-level Model
Operating System Concepts – 7th edition, Jan 23, 2005 4.31 Silberschatz, Galvin and Gagne ©2005
Implementing Threads
Operating System Concepts – 7th edition, Jan 23, 2005 4.32 Silberschatz, Galvin and Gagne ©2005
Threading Issues
Operating System Concepts – 7th edition, Jan 23, 2005 4.33 Silberschatz, Galvin and Gagne ©2005
Semantics of fork() and exec()
Does fork() create a new process which duplicates only the calling
thread, or all threads?
May have two versions of fork(). One that duplicates all
threads and another that duplicates only the thread that
invoked the fork() system call
Which one to use? Depends on the application if it call exec()
directly after fork or not.
If a thread invokes exec(), the new program replaces the entire
process, including all threads.
Operating System Concepts – 7th edition, Jan 23, 2005 4.34 Silberschatz, Galvin and Gagne ©2005
Thread Cancellation
Operating System Concepts – 7th edition, Jan 23, 2005 4.35 Silberschatz, Galvin and Gagne ©2005
Signal Handling
Operating System Concepts – 7th edition, Jan 23, 2005 4.36 Silberschatz, Galvin and Gagne ©2005
Thread Pools
Operating System Concepts – 7th edition, Jan 23, 2005 4.37 Silberschatz, Galvin and Gagne ©2005
Thread Specific Data
Operating System Concepts – 7th edition, Jan 23, 2005 4.38 Silberschatz, Galvin and Gagne ©2005
Scheduler Activations
Operating System Concepts – 7th edition, Jan 23, 2005 4.39 Silberschatz, Galvin and Gagne ©2005
Thread Scheduling
Operating System Concepts – 7th edition, Jan 23, 2005 4.40 Silberschatz, Galvin and Gagne ©2005
Thread Context Switch
Operating System Concepts – 7th edition, Jan 23, 2005 4.41 Silberschatz, Galvin and Gagne ©2005
Windows XP Threads
Operating System Concepts – 7th edition, Jan 23, 2005 4.42 Silberschatz, Galvin and Gagne ©2005
Linux Threads
Operating System Concepts – 7th edition, Jan 23, 2005 4.43 Silberschatz, Galvin and Gagne ©2005
Threads Summary
Operating System Concepts – 7th edition, Jan 23, 2005 4.44 Silberschatz, Galvin and Gagne ©2005
End of Chapter 4