Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi [email protected] 1.
-
Upload
susanna-richardson -
Category
Documents
-
view
216 -
download
1
Transcript of Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi [email protected] 1.
![Page 1: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/1.jpg)
Operating SystemsOperating SystemsLecture 09: Lecture 09:
Threads (Chapter 4)Threads (Chapter 4)Anda Iamnitchi
1
![Page 2: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/2.jpg)
Processes vs. ThreadsProcesses vs. Threads
• A process viewed so far as units of:– Resource ownership– Scheduling/execution
• In multithreading OS: – Process stays the unit of resource ownership– Thread (or lightweight process) is the unit of scheduling
• Multithreading: the ability of an OS to support multiple, concurrent paths of execution within a single process
2
![Page 3: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/3.jpg)
3
Single vs. Multi-Threaded Approaches
MSDOS
Old UNIX
Java Runtime Env
Windows, Unix, etc
![Page 4: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/4.jpg)
Threads• In an OS that supports threads, scheduling and
dispatching is done on a thread basis
• Most of the state information dealing with execution is maintained in thread-level data structures– suspending a process involves suspending all threads
of the process
– termination of a process terminates all threads within the process
4
![Page 5: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/5.jpg)
Thread Execution States
The key states for a thread are:
Running Ready Blocked
Thread operations associated with a change in thread state are:
Spawn Block Unblock Finish
5
![Page 6: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/6.jpg)
2 RPCs with single thread
and multi-threading
![Page 7: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/7.jpg)
Multithreading on a Uniprocessor
7
![Page 8: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/8.jpg)
Types of Threads
8
![Page 9: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/9.jpg)
User-Level Threads (ULTs)
• All thread management is done by the application
• The kernel is not aware of the existence of threads
9
![Page 10: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/10.jpg)
Relationships Between ULTStates and Process States
10
![Page 11: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/11.jpg)
Advantages of ULTs
11
![Page 12: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/12.jpg)
Disadvantages of ULTs
• In a typical OS many system calls are blocking as a result, when a ULT executes a system
call, not only is that thread blocked, but all of the threads within the process are blocked
• In a pure ULT strategy, a multithreaded application cannot take advantage of multiprocessing
12
![Page 13: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/13.jpg)
Kernel-Level Threads (KLTs)
– Thread management is done by the kernel• no thread management
is done by the application
• Windows is an example of this approach
13
![Page 14: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/14.jpg)
Advantages of KLTs
• The kernel can simultaneously schedule multiple threads from the same process on multiple processors
• If one thread in a process is blocked, the kernel can schedule another thread of the same process
• Kernel routines can be multithreaded
14
![Page 15: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/15.jpg)
Disadvantage of KLTs
The transfer of control from one thread to another within the same process requires a mode switch to the kernel
15
![Page 16: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/16.jpg)
Combined Approaches
• Thread creation is done in the user space
• Bulk of scheduling and synchronization of threads is by the application
• Solaris is an example
16
![Page 17: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/17.jpg)
Performance Effect of Multiple Cores
Figure 4.7 (a) Figure 4.7 (b)
17
![Page 18: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/18.jpg)
Database Workloads on Multiple-Processor Hardware
Figure 4.8 Scaling of Database Workloads on Multiple Processor Hardware18
![Page 19: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/19.jpg)
Windows Processes
19
![Page 20: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/20.jpg)
Relationship Between Process and Resource
Figure 4.10 A Windows Process and Its Resources
20
![Page 21: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/21.jpg)
Process and Thread Objects
Windows makes use of two types of process-related objects:
21
![Page 22: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/22.jpg)
Windows Process and Thread Objects
22
![Page 23: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/23.jpg)
Windows Process Object Attributes
Table 4.3 Windows Process Object Attributes 23
![Page 24: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/24.jpg)
Windows Thread Object Attributes
Table 4.4 Windows Thread Object Attributes 24
![Page 25: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/25.jpg)
Thread States
Figure 4.12 Windows Thread States
25
![Page 26: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/26.jpg)
Symmetric Multiprocessing Support (SMP)
26
![Page 27: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/27.jpg)
Solaris Process
- makes use of four thread-related concepts:
27
![Page 28: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/28.jpg)
Processes and Threads in Solaris
Figure 4.13 Processes and Threads in Solaris [MCDO07]
28
![Page 29: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/29.jpg)
Traditional Unix vs Solaris
29
![Page 30: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/30.jpg)
A Lightweight Process (LWP) Data Structure Includes:
• An LWP identifier• The priority of this LWP • A signal mask • Saved values of user-level registers • The kernel stack for this LWP• Resource usage and profiling data• Pointer to the corresponding kernel thread• Pointer to the process structure
30
![Page 31: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/31.jpg)
Solaris Thread States
Figure 4.15 Solaris Thread States
31
![Page 32: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/32.jpg)
Interrupts as Threads
Most operating systems contain two fundamental forms of concurrent activity:
32
![Page 33: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/33.jpg)
Solaris Solution
Solaris employs a set of kernel threads to handle interrupts
• an interrupt thread has its own identifier, priority, context, and stack
• the kernel controls access to data structures and synchronizes among interrupt threads using mutual exclusion primitives
• interrupt threads are assigned higher priorities than all other types of kernel threads
33
![Page 34: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/34.jpg)
Linux Tasks
34
![Page 35: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/35.jpg)
Linux Process/Thread Model
Figure 4.16 Linux Process/Thread Model
35
![Page 36: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/36.jpg)
Linux Threads
36
![Page 37: Operating Systems Lecture 09: Threads (Chapter 4) Anda Iamnitchi anda@cse.usf.edu 1.](https://reader033.fdocuments.us/reader033/viewer/2022052701/56649e4e5503460f94b4490a/html5/thumbnails/37.jpg)
Summary• User-level threads
– created and managed by a threads library that runs in the user space of a process
– a mode switch is not required to switch from one thread to another– only a single user-level thread within a process can execute at a time– if one thread blocks, the entire process is blocked
• Kernel-level threads– threads within a process that are maintained by the kernel– a mode switch is required to switch from one thread to another– multiple threads within the same process can execute in parallel on a
multiprocessor– blocking of a thread does not block the entire process
• Process: related to resource ownership
• Thread: related to program execution 37