0% found this document useful (0 votes)
73 views21 pages

Operating Systems - Ch4 - Mod - Reem

This document provides an overview of threads and threading concepts from the textbook "Operating System Concepts" by Silberschatz, Galvin, and Gagne. It defines a thread as the basic unit of CPU utilization and discusses how modern operating systems and applications are multithreaded to take advantage of multicore processors. The document outlines threading models like one-to-one, many-to-one, and many-to-many and popular thread libraries including Pthreads and Windows threads. Key threading issues like parallelism, concurrency, and Amdahl's law are also summarized.

Uploaded by

Reema Amgad
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)
73 views21 pages

Operating Systems - Ch4 - Mod - Reem

This document provides an overview of threads and threading concepts from the textbook "Operating System Concepts" by Silberschatz, Galvin, and Gagne. It defines a thread as the basic unit of CPU utilization and discusses how modern operating systems and applications are multithreaded to take advantage of multicore processors. The document outlines threading models like one-to-one, many-to-one, and many-to-many and popular thread libraries including Pthreads and Windows threads. Key threading issues like parallelism, concurrency, and Amdahl's law are also summarized.

Uploaded by

Reema Amgad
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/ 21

CSCI315

Operating Systems- Threads


Dr. Reem Ibrahim

Operating System Concepts, Avi Silberschatz, Peter Baer Galvin, Greg Gagne,
John Wiley & Sons, ISBN 978-1-118-06333-0 , 9th Ed., 2012.
Threads
 Overview
 Multicore Programming
 Multithreading Models
 Thread Libraries
 Threading Issues

2 Operating System Dr. Reem Ibrahim


Thread
 A Thread is a basic unit of CPU utilization; it comprises a thread ID,
a program counter, a register set, and a stack.
 It shares with other threads belonging to the same process its code
section, data section, and other operating-system resources, such as
open files and signals.
 A traditional process has
a single thread of control.
If a process has multiple
threads of control, it can
perform more than one
task at a time.

3 Operating System Dr. Reem Ibrahim


Thread
 Most software applications that run on modern computers are
multithreaded.
 An application typically is implemented as a separate process
with several threads of control.
 A web browser might have one thread display images or text
while another thread retrieves data from the network.
 A word processor may have a thread for displaying graphics,
another thread for responding to keystrokes from the user,
and a third thread for performing spelling and grammar
checking in the background.

4 Operating System Dr. Reem Ibrahim


Thread
 Most operating-system kernels are now multithreaded. Several threads
operate in the kernel, and each thread performs a specific task, such as
managing devices, managing memory, or interrupt handling.

5 Operating System Dr. Reem Ibrahim


Thread
 Benefits of Threads
 Responsiveness – may allow
continued execution if part of
process is blocked, especially
important for user interfaces
 Resource Sharing – threads share
resources of process, easier than
shared memory or message passing
 Economy – cheaper than process
creation, thread switching lower
overhead than context switching
 Scalability – process can take
advantage of multiprocessor
architectures

6 Operating System Dr. Reem Ibrahim


Multicore Programming
 Multithreaded programming provides a mechanism for more efficient
use of these multiple computing cores and improved concurrency.
 Concurrent execution on single-core system: On a system with a
single computing core, concurrency means that the execution of the threads
will be interleaved over time because the processing core is capable of
executing only one thread at a time.

 Parallelism on a multi-core system: On a system with multiple cores,


however, concurrency means that the threads can run in parallel, because the
system can assign a separate thread to each core

7 Operating System Dr. Reem Ibrahim


Multicore Programming
 Parallelism implies a system can perform more than one task
simultaneously
 Concurrency supports more than one task making progress
 Single processor / core, scheduler providing concurrency
 Types of parallelism
 Data parallelism – distributes subsets of the same data across multiple
cores, same operation on each
 Task parallelism – distributing threads across cores, each thread
performing unique operation
 Modern Intel CPUs frequently support two threads per core,
while the Oracle T4 CPU supports eight threads per core.

8 Operating System Dr. Reem Ibrahim


Amdahl’s Law
 Identifies performance gains from adding additional cores to an application
that has both serial and parallel components
S is serial portion , N processing cores

 That is, if application is 75% parallel / 25% serial, moving from 1 to 2 cores
results in speedup of 1.6 times
 As N approaches infinity, speedup approaches 1 / S

Serial portion of an application has disproportionate effect on


performance gained by adding additional cores

9 Operating System Dr. Reem Ibrahim


Multithreading Models
 Support for threads may be provided either at the user level,
for user threads, or by the kernel, for kernel threads.
 User threads are supported above the kernel and are managed
without kernel support.
 Three primary thread libraries (POSIX Pthreads, Windows
threads, Java threads)
 Kernel threads are supported and managed directly by the
operating system.
 Kernel threads -Examples – virtually all general purpose
operating systems, including ( Windows, Solaris, Linux, Tru64 UNIX,
Mac OS X)

10 Operating System Dr. Reem Ibrahim


Multithreading Models
 A relationship must exist between user threads and
kernel threads.
 Three common models of establishing such a relationship
 The one-to-one model,
 The many-to-one model
 The many-to-many model.

11 Operating System Dr. Reem Ibrahim


Multithreading Models
 One-to-One model
 Maps each user thread to a kernel thread. It also allows multiple
threads to run in parallel on multiprocessors.
 The only drawback to this model is that creating a user thread requires
creating the corresponding kernel thread. Because the overhead of
creating kernel threads can burden the performance of an application,
most implementations of this model restrict the number of threads
supported by the system.
 Linux, along with the family of Windows operating systems, implement
the one-to-one model.

12 Operating System Dr. Reem Ibrahim


Multithreading Models
 Many-to-One model
 Many user-level threads mapped to single kernel thread
 One thread blocking causes all to block
 Multiple threads may not run in parallel on multicore system
because only one may be in kernel at a time
 Few systems currently use this model
(Examples: Solaris Green Threads,
GNU Portable Threads)

13 Operating System Dr. Reem Ibrahim


Multithreading Models
 Many-to-Many model
 Allows many user level threads to be mapped to many kernel
threads (smaller or equal no of threads)
 Allows the operating system to create a sufficient number of
kernel threads
 Solaris prior to version 9
 Windows with the ThreadFiber package
 Two-level Model
 Similar to M:M, except that it allows
a user thread to be bound to kernel
thread
 Examples (IRIX,HP-UX,Tru64 UNIX)

14 Operating System Dr. Reem Ibrahim


Thread Libraries
 Thread library provides programmer with API for
creating and managing threads.
 Two primary ways of implementing
 Library entirely in user space
 Kernel-level library supported by the OS
 Three main thread libraries are in use today:
 POSIX Pthreads: A POSIX standard (IEEE 1003.1c) API for
thread creation and synchronization. Common in UNIX
operating systems (Solaris, Linux, Mac OS X)
 Windows
 Java.

15 Operating System Dr. Reem Ibrahim


Thread Libraries
 Pthreads
 The pthread_create() function is used to create a new thread.
 The pthread_join() function shall suspend execution of the
calling thread until the target thread terminates.

16 Operating System Dr. Reem Ibrahim


Thread Libraries
 Pthreads Example Pthreads Example (Cont.)

Pthreads Code for Joining 10 Threads

– 9th Edition Silberschatz, Galv


Pthreads Code for Joining 10 Threads
Operating System Concepts 4.20

17 Operating System Dr. Reem Ibrahim


Thread Libraries
 Windows Multithreaded C Program

18 Operating System Dr. Reem Ibrahim


Thread Libraries
 Windows Multithreaded C Program
 windows.h header file must be included when using the Windows API
 Data shared by the separate threads—Sum—are declared globally (the
DWORD data type is an unsigned 32-bit integer).
 The Summation() function that is to be performed in a separate thread.
 Threads are created in the Windows API using the CreateThread() function.
 Once the summation thread is created, the parent must wait for it to complete
before outputting the value of Sum, as the value is set by the summation thread.
 Windows API using the WaitForSingleObject() function, which causes the
creating thread to block until the summation thread has exited.

19 Operating System Dr. Reem Ibrahim


Thread Libraries
 Windows Multithreaded C Program
 Waiting for multiple threads to complete, the WaitForMultipleObjects()
function is used. This function is passed four parameters:
1. The number of objects to wait for
2. A pointer to the array of objects
3. A flag indicating whether all objects have been signaled
4. A timeout duration (or INFINITE)
 For example, if THandles is an array of thread HANDLE objects of size N, the
parent thread can wait for all its child threads to complete with this
statement:
WaitForMultipleObjects(N,THandles, TRUE, INFINITE);

20 Operating System Dr. Reem Ibrahim


Threading Issues
 Thread Cancellation
 Thread cancellation involves terminating a thread before it has completed
 Two general approaches:
 Asynchronous cancellation terminates the target thread immediately
 Deferred cancellation allows the target thread to periodically check if it should
be cancelled
 Pthread code to create and cancel a thread:

21 Operating System Dr. Reem Ibrahim

You might also like