Multithreaded Programming in oprating system

YOGENDRAMS 24 views 26 slides May 26, 2024
Slide 1
Slide 1 of 26
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26

About This Presentation

oprating systems


Slide Content

UNIT 1 – Chapter 4:
Multithreaded Programming

Chapter 4: Threads
●Overview
●Multithreading Models
●Thread Libraries
●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

●automatically allow more than one thing to happen at the same time.
●is a unit of execution with in a process.
●it has less overhead and quicker context switch time
●Threads can share data while processes can not (not always true but true
enough).
●Threads are lightweight processes.
●example
●in a word processor,
4 a background thread may check spelling and grammar
4while a foreground thread processes user input ( keystrokes ),
4 while yet a third thread loads images from the hard drive, and
4 a fourth does periodic automatic backups of the file being edited.


Thread

Thread
●Basic unit of CPU utilization
●Consists of
●thread id
●program counter
●a register set &
●a stack

Single and Multithreaded Processes

Benefits of Multithreaded Programming
●Responsiveness
●One thread may provide rapid response while other threads are blocked or
slowed down doing intensive calculations.
●Resource Sharing
●By default threads share common code, data, and other resources, which
allows multiple tasks to be performed simultaneously in a single address
space.
●Economy
●Creating and managing threads ( and context switches between them ) is
much faster than performing the same tasks for processes.
●Scalability, i.e. Utilization of multiprocessor architectures
● A single threaded process can only run on one CPU, no matter how many
may be available, whereas the execution of a multi-threaded application
may be split amongst available processors.

Kernel Threads
●Supported & managed by the Kernel
●Every thread can run or block independently
●One process may have several threads waiting on different things
●Downside of kernel threads: a bit expensive

●Examples
●Windows XP/2000
●Solaris
●Linux
●Tru64 UNIX
●Mac OS X

User Threads
●Supported above the kernel, via a set of library calls at the user level.
4Thread management done by user-level threads library
–User program provides scheduler and thread package
4May have several user threads per kernel thread
●Advantages
4Cheap, Fast
–Threads do not need to call OS and cause interrupts to kernel
●Disadv
4 If kernel is single threaded, system call from any thread can block
the entire task

●Three primary thread libraries:
● POSIX Pthreads
● Win32 threads
● Java threads

Multithreading Models
●Relationship between user threads & kernel threads
●Many-to-One

●One-to-One

●Many-to-Many

Many-to-One
●Many user-level threads mapped to
single kernel thread
●Disadv
●Entire process is blocked if a
process makes a blocking
system call
●Multiple threads cannot be run
in parallel on multiprocessors
●Examples:
●Solaris Green Threads
●GNU Portable Threads

One-to-One
●Each user-level thread maps to kernel thread
●Disadv
●Creating a user thread requires creating corresponding kernel thread
●Examples
●Windows NT/XP/2000
●Linux
●Solaris 9 and later

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
●Examples
●Solaris prior to version 9
●Windows NT/2000 with the
ThreadFiber package

Two-level Model
●Similar to M:M, except that it
also allows a user thread to be
bound to kernel thread
●Examples
●IRIX
●HP-UX
●Tru64 UNIX
●Solaris 8 and earlier

Thread Libraries
●Thread library provides programmer with API for creating and managing
threads
●Two primary ways of implementing
●Library entirely in user space
4Invoking a function - local function call
●Kernel-level library supported by the OS
4Invoking a function - system call
● Main thread libraries
●POSIX Pthreads
●WIN32
●Java

Pthreads
●May be provided either as user-level or kernel-level
●POSIX standard (IEEE 1003.1c) API for thread creation
and synchronization
●API specifies behavior of the thread library, implementation
is up to development of the library
●Common in UNIX operating systems (Solaris, Linux, Mac
OS X)

WIN32 Threads

●Kernel level library available on windows system

Java Threads
●Allows thread creation & management directly in java
programs
●Java threads are managed by the JVM
●Typically implemented using the threads model provided by
underlying OS
●On windows system using Win32 API
●UNIX / Linux using Pthreads
●Java threads may be created by:
●Extending Thread class
●Implementing the Runnable interface

Threading Issues
●Semantics of fork() and exec() system calls
●Thread cancellation of target thread
●Asynchronous or deferred
●Signal handling
●Thread pools
●Thread-specific data
●Scheduler activations

Semantics of fork() and exec()
●Fork() system call creates separate , duplicate process

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

●If a thread invokes the exec() system call, the program specified in
the parameter to exec() will replace the entire process – including all
threads

● exec system call used after a fork to replace the process’ memory
space with a new program

Thread Cancellation
●Terminating a thread before it has finished
●Eg
●If multiple threads are concurrently searching through a
database & one thread returns the result, the remaining threads
might be canceled
●Target thread – a thread that is to be canceled
●Two general approaches:
●Asynchronous cancellation terminates the target thread
immediately
●Deferred cancellation allows the target thread to periodically
check if it should be cancelled

Signal Handling
●Signals are used in UNIX systems to notify a process that a particular
event has occurred
1.Signal is generated by particular event
2.Signal is delivered to a process
3.Signal is handled
●A signal handler is used to process signals
4Default signal handler
4User defined signal handler
●Options:
●Deliver the signal to the thread to which the signal applies
●Deliver the signal to every thread in the process
●Deliver the signal to certain threads in the process
●Assign a specific thread to receive all signals for the process

Thread Pools
●Create a number of threads at process startup & place them 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
●Useful when you do not have control over the thread creation
process (i.e., when using a thread pool)

Scheduler Activations
●Both M:M and Two-level models require communication to
maintain the appropriate number of kernel threads allocated to the
application
●Typically use an intermediate data structure between user and
kernel threads – lightweight process (LWP)
●Appears to be a virtual processor on
which process can schedule user thread
to run
●Each LWP attached to kernel thread
●How many LWPs to create?

●Scheduler activations provide upcalls - a communication
mechanism from the kernel to the thread library
●This communication allows an application to maintain the correct
number kernel threads

Questions
1.What is a thread ? Explain user level & kernel level threads.
2.Explain multithreading models.
3.Write the differences between Single and Multithreaded Processes
Models.
4.Discuss the benefits of multithreaded programming.