+ All Categories
Home > Education > Lecture 3 and 4 threads

Lecture 3 and 4 threads

Date post: 12-Sep-2014
Category:
View: 237 times
Download: 3 times
Share this document with a friend
Description:
 
Popular Tags:
34
Rushdi Shams, Dept of CSE, KUET 1
Transcript
Page 1: Lecture 3 and 4  threads

Rushdi Shams, Dept of CSE, KUET 1

Page 2: Lecture 3 and 4  threads

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

Page 3: Lecture 3 and 4  threads

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

Page 4: Lecture 3 and 4  threads

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

Page 5: Lecture 3 and 4  threads

Threads Because threads have some of the

properties of processes, they are sometimes called lightweight processes

A thread comprises of 1. Thread ID2. Program Counter (PC)3. Register Set4. Stack

Rushdi Shams, Dept of CSE, KUET 5

Page 6: Lecture 3 and 4  threads

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

Page 7: Lecture 3 and 4  threads

Thread Property

Rushdi Shams, Dept of CSE, KUET 7

Page 8: Lecture 3 and 4  threads

Rushdi Shams, Dept of CSE, KUET 8

Page 9: Lecture 3 and 4  threads

Threads vs ProcessesSimilarities

Rushdi Shams, Dept of CSE, KUET 9

Page 10: Lecture 3 and 4  threads

Threads vs ProcessesDissimilarities

Rushdi Shams, Dept of CSE, KUET 10

Page 11: Lecture 3 and 4  threads

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

Page 12: Lecture 3 and 4  threads

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

Page 13: Lecture 3 and 4  threads

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

Page 14: Lecture 3 and 4  threads

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

Page 15: Lecture 3 and 4  threads

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

Page 16: Lecture 3 and 4  threads

Kernel Thread

Supported directly by the operating system

Slower to create and manage than user threads

Rushdi Shams, Dept of CSE, KUET 16

Page 17: Lecture 3 and 4  threads

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

Page 18: Lecture 3 and 4  threads

Rushdi Shams, Dept of CSE, KUET 18

Page 19: Lecture 3 and 4  threads

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

Page 20: Lecture 3 and 4  threads

Rushdi Shams, Dept of CSE, KUET 20

Page 21: Lecture 3 and 4  threads

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

Page 22: Lecture 3 and 4  threads

Rushdi Shams, Dept of CSE, KUET 22

Page 23: Lecture 3 and 4  threads

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

Page 24: Lecture 3 and 4  threads

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

Page 25: Lecture 3 and 4  threads

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

Page 26: Lecture 3 and 4  threads

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

Page 27: Lecture 3 and 4  threads

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

Page 28: Lecture 3 and 4  threads

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

Page 29: Lecture 3 and 4  threads

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

Page 30: Lecture 3 and 4  threads

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

Page 31: Lecture 3 and 4  threads

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

Page 32: Lecture 3 and 4  threads

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 applies2. Deliver the signal to every thread of the

process3. Deliver the signal to certain threads of the

process4. Assign a specific thread to receive all signals

for processes.

Rushdi Shams, Dept of CSE, KUET 32

Page 33: Lecture 3 and 4  threads

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

Page 34: Lecture 3 and 4  threads

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


Recommended