0% found this document useful (0 votes)
20 views22 pages

Threads

Thread

Uploaded by

yeet man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views22 pages

Threads

Thread

Uploaded by

yeet man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Copyright

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

• Why allow multiple threads per process?


• Lightweight processes
• Allow space- and time- efficient parallelism
• Organized in thread groups
• Allow simple communication and synchronization
Thread Usage (1)

Figure 2.7 A word processor with three threads.


Thread Usage (2)

Figure 2.8 A multithreaded Web server.


Thread Usage (3)

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)

Per process items Per thread items


Address space Program counter
Global variables Registers
Open files Stack
Child processes State
Pending alarms
Signals and signal handlers
Accounting information

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.12 Each thread has its own stack.


POSIX Threads (1 of 2)

Thread call Description


Pthread_create Create a new thread
pthread_exit Terminate the calling thread
pthread_join Wait for a specific thread to exit
pthread_yield Release the CPU to let another thread run
pthread_attr_init Create and initialize a thread's attribute structure
pthread_attr_destroy Remove a thread's attribute structure

Figure 2.13 Some of the Pthreads function calls.


Pthreads
Implementing Threads in User Space

Figure 2.15 (a) A user-level threads package. (b) A threads package managed by the kernel.
User Threads: Pros and Cons

+ Thread switching time (no mode switch)


+ Scalability, customizability (no in-kernel management)
- Transparency (typically requires app cooperation)
- Parallelism (blocking syscalls are problematic)
Hybrid Implementations

Figure 2.16 Multiplexing user-level threads onto kernel-level threads.


Making Single-Threaded Code Multithreaded (1)

Figure 2.17 Conflicts between threads over the use of a global variable.
Making Single-Threaded Code Multithreaded (2)

Figure 2.18 Threads can have private global variables.


Threads: Issues

• Does the OS keep track of threads?


• Kernel threads v s . user threads
er us

• What to do on fork?
• Clone all threads v s . calling thread
er us

• What if a thread is currently blocking on a systems call?


• What to do with signals?
• Send signal to all threads v s . single thread
er us

• Per-process or per-thread signal handlers


• Where to store per-thread variables?
• Does sharing come at a cost?
• Are threads required inside an operating system?
Event-Driven Servers
• Implement server as finite-state machine that responds to events
using asynchronous system calls
• E.g., the availability of data on a socket
• Implementation can be very efficient
• Every event leads to a burst of activity without blocking
• Most OS offer event notification interfaces for asynchronous I/O
• Linus: epoll
• FreeBSD: kqueue
Event-Driven Server
Single-Threaded V S Multi-threaded V S Event-Driven Servers
ersu ersu

Model Characteristics
Threads Parallelism, blocking system calls
Single-threaded process No parallelism, blocking system calls
Finite-state machine Parallelism, nonblocking system calls, interrupts

Figure 2.20 Three ways to construct a server.

You might also like