0% found this document useful (0 votes)
61 views

Chapter#03 - Threads & Multithreading Concept

The document discusses threads and multithreading concepts. It defines a thread as the basic unit of CPU utilization with its own program counter, register set, and stack. A process can contain multiple threads that share code, data, and other resources. User-level threads are managed by a thread library without kernel support, while kernel threads are directly supported by the operating system. Common threading models include many-to-one, one-to-one, and many-to-many mappings of user to kernel threads. The document also covers threading issues such as signal handling, thread cancellation, thread pools, and thread-specific data.

Uploaded by

Manzar Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Chapter#03 - Threads & Multithreading Concept

The document discusses threads and multithreading concepts. It defines a thread as the basic unit of CPU utilization with its own program counter, register set, and stack. A process can contain multiple threads that share code, data, and other resources. User-level threads are managed by a thread library without kernel support, while kernel threads are directly supported by the operating system. Common threading models include many-to-one, one-to-one, and many-to-many mappings of user to kernel threads. The document also covers threading issues such as signal handling, thread cancellation, thread pools, and thread-specific data.

Uploaded by

Manzar Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Chapter 3:

Threads & Multithreading Concept

COURSE INSTRUCTOR:
ENGR. FARHEEN QAZI, ENGR. SYED HARIS MEHBOOB, MS. FALAK SALEEM

PROGRAM : SOFTWARE ENGINEERING


COURSE : OPERATING SYSTEMS (SE-204T)
BATCH : 2021F SPRING : 2023

DEPARTMENT OF SOFTWARE ENGINEERING


SIR SYED UNIVERSITY OF ENGINEERING & TECHNOLOGY
Today’s Agenda

 Overview
 User Level Thread
 Kernel Level Thread
 Multithreading Models
 Threading Issues
Objectives

 To introduce the notion of a thread — a fundamental unit of


CPU utilization that forms the basis of multithreaded computer
systems
 To discuss the APIs for the Pthreads, Win32, and Java thread
libraries
 To examine issues related to multithreaded programming
Thread Overview

 Threads are mechanisms that permit an application to perform


multiple tasks concurrently.
 Thread is a basic unit of CPU utilization
 Thread ID
 Program counter
 Register set
 Stack
 A single program can contain multiple threads
 Threads share with other threads belonging to the same process
o Code, data, open files…….
Single and Multithreaded Processes

heavyweight process lightweight process


Traditional (heavyweight) process has a single thread of control
Threads

Threads share…. Threads specific


Attributes….
 Global memory  Thread ID
 Process ID and parent process  Thread specific data
ID
 CPU affinity
 Controlling terminal  Stack (local variables and
 Process credentials (user ) function call linkage
information)
 Open file information
 ……
 Timers
 ………
Thread Example # 01
Contd…

 Thread Usage

A multithreaded Web server


Thread Example # 02

Word processor may have a thread for;


1. Displaying graphics.
2. Reading keystrokes from the user.
3. Performing spelling and grammar checking in background.
Benefits

 Responsiveness
Interactive application can delegate background functions to a
thread and keep running

 Resource Sharing
Several different threads can access the same address space

 Economy
Allocating memory and new processes is costly. Threads are much
‘cheaper’ to initiate.

 Scalability
Use threads to take advantage of multiprocessor architecture
Multithreading Models

 Support provided at either

 User level -> user threads


Supported above the kernel and managed without kernel
support

 Kernel level -> kernel threads


Supported and managed directly by the operating system

What is the relationship between user and kernel threads?


User Threads

 Supported above the kernel and are implemented by thread library


at the user level.

 The library supports thread creating, scheduling, and management


with no support from the kernel.

 Kernel is unaware of user-level threads, all thread creation and


scheduling are done in user space. Hence, they are generally fast.

 Three primary thread libraries:


 POSIX Pthreads
 Win32 threads
 Java threads
User Threads Advantages
User-level threads does not require modification to OS.

 Simple Representation:
Each thread is represented simply by a PC, registers, stack and
a small control block, all stored in the user process address
space.

 Simple Management:
Means that creating a thread, switching between threads and
synchronization between threads can all be done without
intervention of the kernel.

 Fast and Efficient:


Thread switching is not much more expensive than a
procedure call.
User Threads Disadvantages

 There is a lack of coordination between threads and operating


system kernel. Therefore, process as whole gets one time slice
irrespective of whether process has one thread or 1000 threads
within. It is up to each thread to relinquish control to other threads.

 Any user-level thread performing a blocking system call will cause the
entire process to block, even if other threads are available to run.
Kernel Threads

 Supported directly by the OS.


 Kernel performs thread creation, scheduling and management in kernel
space.
 Kernel threads are generally slower.
 If a thread performs a blocking system call, the kernel can schedule another
thread.

 Examples
 Windows XP/2000
 Solaris
 Linux
 Tru64 UNIX
 Mac OS X
Kernel Threads Advantages

 Because kernel has full knowledge of all threads, Scheduler may


decide to give more time to a process having large number of
threads than process having small number of threads.

 Kernel-level threads are especially good for applications that


frequently block.

 In multiprocessor system, kernel can schedule threads on different


processors.
Kernel Threads Disadvantages

 The kernel-level threads are slow and inefficient. For instance, threads
operations are hundreds of times slower than that of user-level
threads.

 Since kernel must manage and schedule threads as well as


processes. It require a full thread control block (TCB) for each thread
to maintain information about threads. As a result there is significant
overhead and increased in kernel complexity.
Multithreading Models

User Thread – to - Kernel Thread

 Many-to-One

 One-to-One

 Many-to-Many
Many-to-One

Many user-level threads


mapped to single kernel
thread
One-to-One

Each user-level thread maps to kernel thread


 Examples
 Windows NT/XP/2000
 Linux
Many-to-Many Model

Allows many user level


threads to be mapped
to many kernel threads

Allows the operating


system to create a
sufficient number of
kernel threads
 Example
 Windows NT/2000 with the
ThreadFiber package
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 in use today:


 POSIC Pthreads
 Win32
 Java
Threading Issues

 Semantics of fork() and exec() system calls


 Thread cancellation.
 Signal handling
 Thread pools
 Thread safety
 Thread-specific data
 Scheduler activations
Semantics of fork() ,exec(), exit()

Does fork() duplicate only the calling thread or all threads?

 Threads and exec()


With exec(), the calling program is replaced in memory. All
threads, except the once calling exec(), vanish
immediately. No thread-specific data destructors or cleanup
handlers are executed.

 Threads and exit()


If any thread calls exit() or the main thread does a return,
ALL threads immediately vanish. No thread-specific data
destructors or cleanup handlers are executed.
Semantics of fork() ,exec(), exit()

 Threads and fork()


When a multithread process calls fork(), only the calling thread is replicated. All
other threads vanish in the child. No thread-specific data destructors or cleanup
handlers are executed.

Problems:
 The global data may be inconsistent:
 Was another thread in the process of updating it?
 Data or critical code may be locked by another thread. That lock is copied
into child process, too.
 Memory leaks
 Thread (other) specific data not available
Recommendation: In multithreaded application, only use fork() after exec()
Thread cancellation

 A task of terminating a (target) thread before it has completed.


 Example: If multiple threads are concurrently searching through a
database and one thread returns
 the expected result, the remaining threads might be cancelled.
 The thread to be cancelled is called the target thread.

 Example: When a user press the stop button on a web browser, the
thread loading a web page should be cancelled.

 Asynchronous Cancellation
 One thread immediately terminates the target thread.

 Deferred Cancellation
 The target thread can periodically check if it should terminate.

 Pthreads API provides deferred cancellation


Signal Handling

 A signal is used in UNIX systems to notify a process that a particular


event has occurred.

 Synchronous Signal Delivery.


Synchronous signals are delivered to the same process that
performed the operation causing the signal.
 Example: An illegal memory access or division by zero

 Asynchronous Signal Delivery


When a signal is generated by an event external to a running process,
the signal is asynchronously delivered typically to another process.
 Example: Terminating a process with <control> <C> or having a timer
expire.
Contd….

 Three Steps of Signal Processing.


1. A signal is generated by the occurrence of an event.
2. A generated signal is delivered to a process.
3. Once delivered, the signal must be handled.

 Two Ways of Signal Handling.


1. A Default Signal Handler (run by kernel).
2. A User-defined Signal Handler

 Overwrites the default handler.


Thread Pools

 Create a number of threads in a pool where they await work


 Advantages:
 Usually slightly faster to service a request with an existing
thread than create a new thread
 Allows the number of threads in the application(s) to be
bound to the size of the pool
Thread Specific Data

 Allows each thread to have its own copy of data


 Provides per-thread storage for a function

 Useful when you do not have control over the thread creation
process (i.e., when using a thread pool)
Threads vs. Processes

 Advantages of multithreading
 Sharing between threads is easy
 Faster creation
 Disadvantages of multithreading
 Ensure threads-safety
 Bug in one thread can bleed to other threads, since they share the
same address space
 Threads must compete for memory
 Considerations
 Dealing with signals in threads is tricky
 All threads must run the same program
 Sharing of files, users, etc
End of Chapter 3
Thank you

You might also like