0% found this document useful (0 votes)
6 views26 pages

Ai Os CH4

The document discusses the concept of threads in operating systems, highlighting the differences between single-threaded and multi-threaded processes. It covers the benefits of multithreading, challenges in multi-core programming, various multithreading models, and the distinction between user and kernel threads. Additionally, it explains implicit and explicit threading, including thread pools and fork-join mechanisms, as well as thread cancellation methods.

Uploaded by

abdoalsenaweabdo
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)
6 views26 pages

Ai Os CH4

The document discusses the concept of threads in operating systems, highlighting the differences between single-threaded and multi-threaded processes. It covers the benefits of multithreading, challenges in multi-core programming, various multithreading models, and the distinction between user and kernel threads. Additionally, it explains implicit and explicit threading, including thread pools and fork-join mechanisms, as well as thread cancellation methods.

Uploaded by

abdoalsenaweabdo
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/ 26

Kafrelsheikh University

Faculty of Artificial Intelligence


(2025)

Operating Systems
Ch4: Threads

Dr. Marwa Elseddik


Lecturer, Department of Robotics and Intelligent Machines - College of Artificial
Intelligence, Kafrelsheikh University
2025
Topics

1. Benefits

2. Multi-core Programming Challenges

3. Types of Parallelism

4. Multithreading Models

5. Implicit Threading

6. Thread Cancellation

2
Threads

• A thread is a single sequence of execution.


• A thread is the basic unit of CPU utilization.
• A traditional process has a single thread of control.
• A single-threaded process can perform one task at a time.
• A multi-threaded process can perform multiple tasks at a time.
• A thread comprises a thread ID, a PC, a register set, and a stack.
• Process threads share code section, data section, and resources.

3
Single-Threaded versus Multi-Threaded

4
Benefits

5
Benefits of Multithreaded Programs

• Responsiveness. A blocked thread doesn’t block the process.

• Resource sharing. Threads share the resources of the process.

• Economy. Thread creation has less overhead than process creation.

• Scalability. Threads can run in parallel on a multiprocessor machine.

6
Multi-core Programming
Challenges

7
Multi-core Programming Challenges
• Identifying tasks. divide program into tasks that can run in parallel.

• Balance. Ensure that these tasks perform equal work of equal value.

• Data splitting. Divide data between tasks that run on separate cores.

• Data dependency. Examine data to find dependencies between tasks.

• Testing and debugging. Testing a program with many execution paths.


8
Types of Parallelism
Data Parallelism
Task Parallelism

9
Data Parallelism

• Distribution of data across multiple cores.

10
Task Parallelism

• Distribution of tasks across multiple cores.

11
Multithreading Models
Many-to-one model Many-to-many model
One-to-one model Two-level model

12
User Threads versus Kernel Threads
• Support for threads may be provided either
• At the user level, for user threads.
• By the operating system, for kernel threads.

• User threads are managed without OS support.

• Kernel threads are supported and managed directly by the OS.

• Most modern OSs support kernel threads (e.g., Windows, Linux).


13
Many-to-One Model

• Only one user thread can access the kernel at a time.


• Threads cannot run in parallel on multi-core systems.
• The entire process will block if a thread makes a blocking system call.
• Thread management is done in user space by a thread library not OS.

14
One-to-One Model

• Multiple threads can access the kernel at a given time.


• Multiple threads can run in parallel on multi-core systems.
• When a thread makes a blocking system call, another thread can run.
• OS creates a kernel thread per user thread (can burden performance).

15
Many-to-Many Model

• Multiple threads can access the kernel at a given time.


• Multiple threads can run in parallel on multi-core systems.
• When a thread makes a blocking system call, another thread can run.
• OS creates a set of kernel threads (less than or equal to user threads).

16
Advantages of Many-to-Many Model

• Does not suffer from the drawback of the many-to-one model.


• Multiple user threads can run in parallel on a multicore system.

• Does not suffer from the drawback of the one-to-one model.


• Increasing the number of user threads does not affect system performance.

17
Two-Level Model

• Most operating systems use the one-to-one model.


• In practice, it is difficult to implement the many-to-many model.
• No need to limit the number of kernel threads on modern multicore systems.
• Modern concurrency libraries use the many-to-many model.
• Developers identify tasks and the library maps them to kernel threads.

18
Implicit Threading

19
Explicit Threading

• The server creates a thread for each request.


• The thread is discarded once it has completed its work.
• Each new request requires creating a new thread (i.e., overhead).
• The number of threads that can be created has no upper bound.
• Too many threads that work concurrently can exhaust system resources.

20
Implicit Threading

• Helps developing modern applications with hundreds of threads.


• Programmers identify tasks (not threads) that can run in parallel.
• The programmer writes a task as a function.
• Run-time libraries (not programmers) create and manage threads.
• Each task is mapped to a separate thread using the many-to-many model.
• Two implicit threading approaches:
• Thread pool. works well for asynchronous (independent) tasks.
• Fork-join. works well for tasks that are synchronous (cooperating).

21
Thread Pool

• At startup, the server creates a set of threads in a pool.


• When the server receives a request, it submits it to the pool.
• If the pool has a thread, the request is served immediately.
• If the pool is empty, the task is queued until a thread becomes free.
• Once a thread completes serving the request, it returns to the pool.
• Thread pools offer two benefits:
• Serving a request with an existing thread is faster than creating a thread.
• Limiting the number of threads helps avoid exhausting system resources.
• A dynamic thread pool can adjust the number of threads in the pool.
22
Fork-Join
• In traditional fork-join, the main parent thread does the following:
1. Create (i.e., forks) one or more child threads.
2. Wait for the created threads (children) to terminate.
3. Join the threads, at which point it can retrieve and combine their results.
• Implicit fork-join is a synchronous version of the thread pool.
• Threads are not constructed but parallel tasks are designated.
• A library manages the creation of threads and assigns tasks to those threads.

23
Thread Cancellation

24
Thread Cancellation
• Refers to terminating a thread before it has completed.
• Cancellation of a target thread can be asynchronous or deferred.

• Asynchronous cancellation
• One thread immediately terminates the target thread.

• Deferred cancellation
• The target thread periodically checks whether it should terminate.
• This allows the target thread to terminate itself in an orderly fashion.

25
Thank You

26

You might also like