2. The Cake Baking Analogy
The computer scientist baking the cake
has the ingredients (data) and recipe
(algorithm).
Now, he needs to blend the egg
Then that blended egg will be mixed up
with the dough made by flour
The dough mixed with blended egg will
then be placed to syrup.
Rushdi Shams, Dept of CSE, KUET 2
3. The Cake Baking Analogy
Are they dependent or independent of each
other?
- Dependent
Are they verbs? I mean are they executing
something?
- Of course, making something is always a verb
Are they sharing some common place?
- mmm, that can be tricky! I can say they are
sharing common place if the mixing up of
blended egg with the dough takes place in the
same bowl!
Rushdi Shams, Dept of CSE, KUET 3
4. The Cake Baking Analogy
So, blending egg is a process, mixing it
up with dough is a process and mixing
the dough with yummy syrup is a
process.
But they are dependent on each other.
We will thus call them Threads
And threads share some common
resources that processes don’t!
Rushdi Shams, Dept of CSE, KUET 4
5. Threads
Because threads have some of the
properties of processes, they are
sometimes called lightweight
processes
A thread comprises of
1. Thread ID
2. Program Counter (PC)
3. Register Set
4. Stack
Rushdi Shams, Dept of CSE, KUET 5
6. Threads
Threads share its code section and
data section and other operating
systems resources with other threads
belonging to the same process
Rushdi Shams, Dept of CSE, KUET 6
11. Why Threads?
A web browser has a thread to display
contents and another thread that serves
you as you click Download button
MS Word has a thread that displays the
content area, another thread that reads
keystrokes from the keyboard and the
third checks the grammar and spelling in
background
Rushdi Shams, Dept of CSE, KUET 11
12. Why Threads?
And of course process does the same
thing thread does!
A client poses 5 different requests (but
similar) to the web browser.
The browser can create 5 different
processes to serve him
But, what’s the wrong with this
architecture?
Rushdi Shams, Dept of CSE, KUET 12
13. Benefits of Threads
Responsiveness
A multithreaded web browser allows user to
interact on several buttons with less overhead
Resource Sharing
Have you ever heard any time anyone saying I
have plenty of resources, use as much as you
like??
Rushdi Shams, Dept of CSE, KUET 13
14. Benefits of Threads
Economy
Allocating memory and resources for process
creation is costly. It is more economical to
create and switch between threads than
processes.
-Creating process is 30% slower than creating
threads
-Switching between processes is 5% slower
than switching between threads
Rushdi Shams, Dept of CSE, KUET 14
15. User Thread
User threads are supported above the
kernel and implemented by a thread
library at user level
This library provides support for thread
creation, scheduling, and management
Does not need support from the kernel
Kernel is unaware of user level threads
If kernel is single threaded, any user
level thread performing system block
call will cause the entire process
blocked
Rushdi Shams, Dept of CSE, KUET 15
16. Kernel Thread
Supported directly by the operating
system
Slower to create and manage than user
threads
Rushdi Shams, Dept of CSE, KUET 16
17. Multithread Model
Many systems support for both user and
kernel threads.
1. Many to one Model
Many user level threads to one kernel
thread
Thread management is done in user
space. So, efficient but the entire process
will block if any of the user threads call a
system block.
Multiple threads are unable to run in
parallel
Rushdi Shams, Dept of CSE, KUET 17
19. Multithread Model
2. One to one Model
More concurrency than many to many
model by allowing another thread to
run when a thread makes a system
call.
Creating user thread requires creating
corresponding kernel thread. And
creating kernel thread is expensive!
Rushdi Shams, Dept of CSE, KUET 19
21. Multithread Model
3. Many to Many Model
Multiplexes many user level threads to
a smaller or equal number of kernel
threads
Rushdi Shams, Dept of CSE, KUET 21
23. Thread Cancellation
Thread cancellation is a task of
terminating threads before their
completion
OS creates 5 threads for a database
operation. 1 executes the query, 1
fetches the record and 1 displays. The 2
more threads are cancelled by the OS
Rushdi Shams, Dept of CSE, KUET 23
24. Thread Cancellation
You are browsing through IE. Suppose,
3 threads are running. Suddenly you
press STOP button. All of them are
cancelled.
Rushdi Shams, Dept of CSE, KUET 24
25. Asynchronous Cancellation
The thread that is to be cancelled is
called Target Thread
In asynchronous cancellation, one
thread immediately cancel the target
thread
Rushdi Shams, Dept of CSE, KUET 25
26. Deferred Cancellation
The target thread checks periodically
check if it should terminate, allowing the
target thread an opportunity to terminate
itself in an orderly fashion
Rushdi Shams, Dept of CSE, KUET 26
27. Problem in Cancellation
Well, cancellation of thread means
simply withdrawing OS resources from
the thread. Sometimes, resources are
GIVEN to a thread and sometimes
resources are ALLOCATED to a thread.
When GIVEN, it is really a problem to
retrieve that.
If a thread is in a mid of update, and
cancelled, lost of data may occur.
Rushdi Shams, Dept of CSE, KUET 27
28. Problem in Cancellation
The problem is severe in case of
asynchronous cancellation. OS often
cannot retrieve all of its resources from
the cancelled thread.
Most OS, however, do the
asynchronous thread cancellation!
Rushdi Shams, Dept of CSE, KUET 28
29. Signal Handling
A signal is used to notify a process that
a particular event has occurred. All
signals follow the pattern-
1. A signal is generated by the
occurrence of a particular event
2. A generated signal is delivered to a
process
3. Once delivered, the signal MUST be
handled.
Rushdi Shams, Dept of CSE, KUET 29
30. Types of Signals
Synchronous Signal
A signal is generated and delivered to a
particular process if the occurrence is
caused by that process only
Asynchronous Signal
A signal is generated and delivered to all
of the process created by the process
responsible for the event.
Rushdi Shams, Dept of CSE, KUET 30
31. Signal Handler
Every signal has a default signal handler
run by kernel.
User defined signal handler can
overtrump it if required.
Some signals can be ignored
(maximizing the window), others can be
handled by terminating the program
(illegal memory access)
Rushdi Shams, Dept of CSE, KUET 31
32. Signal Delivery
Delivery of a signal is simple in single
threaded programs (hand to hand delivery)
In multithreaded environment, any one of the
following can be accomplished-
1. Deliver the signal to the thread to which the
signal applies
2. Deliver the signal to every thread of the
process
3. Deliver the signal to certain threads of the
process
4. Assign a specific thread to receive all signals
for processes.
Rushdi Shams, Dept of CSE, KUET 32
33. Thread Pool
We said earlier that in web browser we can
serve the client faster by creating 5 threads
for 1 process (browsing) than by creating 5
processes (saving, downloading, clicking,
maximizing, minimizing)
Okay, but still there is a problem- if you
create too many threads, that would again
embarrass our POOR OS!!
Too many threads can be just a little bit
reduction in servicing time than creating
independent processes
Rushdi Shams, Dept of CSE, KUET 33
34. Thread Pool
That’s why OS creates a pool of threads
at start up
While starting up, the OS puts, say, 50
threads in its pool (10 system threads,
10 browsing threads, 10 nitty bitty, 20
bla bla)
Then it will invite those threads from the
pool when required.
Rushdi Shams, Dept of CSE, KUET 34