Upload
fatimah-al-juhani
View
226
Download
0
Embed Size (px)
Citation preview
8/2/2019 Chapter 4 102
1/35
SPRING 2011 - 1021
8/2/2019 Chapter 4 102
2/35
Why Threads : Issues with Processes
What are threads?
Threading models
Threading issues
SPRING 2011 - 102
2
8/2/2019 Chapter 4 102
3/35
The fork() system call is expensive
Copying the entire parent process as child
process. It may not always be required to copy
entire parent process to child process as the child
is going to call exec()system call immediately in
most of the cases.
IPC is required to pass information betweena parent and its child processes.
SPRING 2011 - 102
3
8/2/2019 Chapter 4 102
4/35
A thread is a lightweight process which
executes within the address space of a process.
A thread is an execution context that is
independently scheduled, but shares single
addresses space with other threads of the same
process.
SPRING 2011 - 102
4
8/2/2019 Chapter 4 102
5/35
SPRING 2011 - 102
Thread
Process
Terminated
f2
f1
A traditional process can be viewed as:
An address space with a single thread!
5
8/2/2019 Chapter 4 102
6/35
Suppose we want that f1 and f2 should be executed
by separate threads, while main function is executed
concurrently by another thread.
Multi threading refers to the ability of an OS tosupport multiple threads of execution with in a
single process. A single program made up of a
number ofdifferent concurrent activities; sometimes
called multitasking.
Multithreading works similar to multiprogramming.
The CPU switches rapidly back and forth among
threads providing the illusion that threads are
running in parallel.SPRING 2011 - 102
6
8/2/2019 Chapter 4 102
7/35
main()
{
thread(t1,f1);
thread(t2,f2);
}
f1(){ }
f2()
{ }
main t2t1
Process Address Space
PC
PC
PC
SPRING 2011 - 102
7
8/2/2019 Chapter 4 102
8/35
SPRING 2011 - 102
8
8/2/2019 Chapter 4 102
9/35
SPRING 2011 - 102
A thread is a lightweight process which executes within the
address space of a process.
A thread can be scheduled to run on a CPU as an independent unit
and terminate.
Multiple threads can run simultaneously.
Threads may be managed by the operating system or by a user
application
Examples: Win32 threads, C-threads, Pthreads
9
8/2/2019 Chapter 4 102
10/35
SPRING 2011 - 102
10
8/2/2019 Chapter 4 102
11/35
MS-DOS supports a single user
process and a single thread.
Some UNIX, support multipleuser processes but only supportone thread per process
SPRING 2011 - 102
11
8/2/2019 Chapter 4 102
12/35
Java run-time environment is a
single process with multiple
threads
Multiple processes andthreads
are found in Windows, Solaris,
and many modern versions of
UNIX
SPRING 2011 - 102
12
8/2/2019 Chapter 4 102
13/35
SPRING 2011 - 102
13
8/2/2019 Chapter 4 102
14/35
SPRING 2011 - 102
14
8/2/2019 Chapter 4 102
15/35
Each thread has
An execution state (running, ready, etc.) & An execution stack
Saved thread context when not running
Some per-thread static storage for local variables
Access to the memory and resources of its process (all threads of a process share this)
They reside in the same address space and have access to the same data.
When one thread alters an item of data in memory, other threads see the results if andwhen they access that item.
If one thread opens a file with read privileges, other threads in the same process can
also read from that file.
SPRING 2011 - 102
15
8/2/2019 Chapter 4 102
16/35
AddressSpace
T1
Stack
State
Pro
gram
Staticdata
Resources
Stack
State
T2
SPRING 2011 - 102
16
8/2/2019 Chapter 4 102
17/35
SPRING 2011 - 102
17
8/2/2019 Chapter 4 102
18/35
SPRING 2011 - 102
A thread can be in states similar to a process (new, ready,
running, blocked, terminated)
A thread can create another thread
Multiple threads can operate within the same address
space
No automatic protection mechanism is in place for
threadsthey are meant to help each other
18
8/2/2019 Chapter 4 102
19/35
8/2/2019 Chapter 4 102
20/35
Processors were originally developed with only one
core. The core is the part of the processor that
actually performs the reading and executing of
instructions. A multi-core processor is a singlecomponent with two or more independent actual
processors (called "cores").
Multithread programming make more efficient use
of multiple cores.
It allows separate concurrent tasks to run parallel on
individual cores.
SPRING 2011 - 102
20
8/2/2019 Chapter 4 102
21/35
SPRING 2011 - 10221
8/2/2019 Chapter 4 102
22/35
Three most popular threading models
User-level threads
Kernel-level threads
Combination of user- and kernel-level threads
SPRING 2011 - 102
22
8/2/2019 Chapter 4 102
23/35
By default an application begins with a single
thread within a process managed by Kernel.
Later the application may spawn a new thread
within the same process by invoking spawn
utility in the thread library.
User-level threads perform threadingoperations in user space
Threads are created by runtimelibraries.
When ever ULT makes a system call, Kernel
takes it as the
system call has been made by the process, so
all the threads of
the process are blocked.
Thread switching is fast and CPU is
not interrupted during
thread switching (No mode switch).
Kernel is scheduling the
processes while user level library will
be scheduling the thread SPRING 2011 - 102
23
8/2/2019 Chapter 4 102
24/35
Thread management done by kernel
and Kernel is aware of threads
When ever a KLT makes a system call, only that
thread
is blocked and the Kernel can schedule another
thread
of same process. So all the threads of the
process are
NOT blocked
SPRING 2011 - 102
24
8/2/2019 Chapter 4 102
25/35
SPRING 2011 - 102
Operating system maps all threads in amultithreaded process to single executioncontext
Many user threads per kernel thread; i.e. Kernel
sees just one thread.
Advantages
Thread management is done in user space, so it
is efficient.Disadvantages
When a thread within a process makes a system
call the entire process blocks.
Because only one thread can access kernel at a
time, no parallelism on multiprocessors is possible.
25
8/2/2019 Chapter 4 102
26/35
8/2/2019 Chapter 4 102
27/35
SPRING 2011 - 102
User
level
Threads
Kernel
level
Threads
Multiple user threads are multiplexed
over a smaller or equal number of
kernel threads
Advantage
Application can create as many user
threads as needed.
Kernel threads run in parallel onmultiprocessors
When a thread blocks, another thread can
still run
27
8/2/2019 Chapter 4 102
28/35
SPRING 2011 - 10228
8/2/2019 Chapter 4 102
29/35
Semantics offork() and exec() system calls
Thread cancellation
Signal handling
Thread pools
Thread specific data
Scheduler activations
SPRING 2011 - 102
29
8/2/2019 Chapter 4 102
30/35
Does fork() duplicate only the
calling thread
or
all threads?
SPRING 2011 - 102
30
8/2/2019 Chapter 4 102
31/35
Terminating a thread before it has finished
Two general approaches:
Asynchronous cancellation
terminates the target thread
immediately.
Prematurely terminating a
thread can cause subtle errors in
processes because multiple
threads share the same address
space
Deferred cancellation allows the
target thread to periodically
check if it should be cancelled
Some thread implementations
allow a thread to determine
when it can be terminated to
prevent process from entering
inconsistent state
SPRING 2011 - 102
31
8/2/2019 Chapter 4 102
32/35
A signal handler is used to process signals
1. Signal is generated by particular event
2. Signal is delivered to a process
3. Signal is handled
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
SignalsareusedinUNIXsystemst
onotifyaprocess
thatapa
rticulareventhasoccurred
SPRING 2011 - 102
32
8/2/2019 Chapter 4 102
33/35
SPRING 2011 - 102
33
8/2/2019 Chapter 4 102
34/35
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
SPRING 2011 - 102
34
8/2/2019 Chapter 4 102
35/35
Allows each thread to have its own copy of data
Useful when you do not have control over thethread creation process (i.e., when using a thread
pool)
SPRING 2011 - 102
35