Threads
Threads
This work is protected by United States copyright laws and is provided solely for
the use of instructors in teaching their courses and assessing student learning.
Dissemination or sale of any part of this work (including on the World Wide Web)
will destroy the integrity of the work and is not permitted. The work and materials
from it should never be made available to students except by instructors using
the accompanying text in their classes. All recipients of this work are expected to
abide by these restrictions and to honor the intended pedagogical purposes and
the needs of other instructors who rely on these materials.
Please, read and comprehend the above statement from the authors/publisher.
This material is provided to aid students in understanding the topics, you are also
bound to that statement
Threads
Threads
• Implicit assumption so far:
• 1 process ® 1 thread of execution
• Multithreaded execution:
• 1 process ® N threads of execution
Figure 2.9 A rough outline of the code for Figure 2.8. (a) Dispatcher thread. (b) Worker
thread.
Threads, Process, Finite State Machine
Model Characteristics
Threads Parallelism, blocking system calls
Single-threaded process No parallelism, blocking system calls
Finite-state machine Parallelism, nonblocking system calls, interrupts
Threads and Processes
• Threads reside in the same address space
of a single process
• All information exchange is via data shared
between the threads
• Threads synchronize via simple primitives
• Each thread has its own stack, hardware
registers, and state
• Thread table/switch: a lighter process
table/switch
• Each thread may call any O S-supported
system call on behalf of the process to
which it belongs
The Classical Thread Model (1)
Figure 2.10 (a) Three processes each with one thread. (b) One process with three threads.
The Classical Thread Model (2)
Figure 2.11 The first column lists some items shared by all threads in a
process. The second one lists some items private to each thread.
The Classical Thread Model (3)
Figure 2.15 (a) A user-level threads package. (b) A threads package managed by the kernel.
User Threads: Pros and Cons
Figure 2.17 Conflicts between threads over the use of a global variable.
Making Single-Threaded Code Multithreaded (2)
• What to do on fork?
• Clone all threads v s . calling thread
er us
Model Characteristics
Threads Parallelism, blocking system calls
Single-threaded process No parallelism, blocking system calls
Finite-state machine Parallelism, nonblocking system calls, interrupts