15 threads

22
1 Understand Process Management • Threads

Upload: myrajendra

Post on 27-May-2015

260 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 15 threads

1

Understand Process Management

• Threads

Page 2: 15 threads

2

HOME PREVIOUS TOPIC NEXTPREVIOUS QUESTION PAPERS FOR OSCPP TUTORIALS

Page 3: 15 threads

Recap

In the last class, you have learnt

• Relationship between Processes

• Reasons for providing an environment that allows

Process cooperation

3

Page 4: 15 threads

4

Objectives

On completion of this class, you would be able to

know

• Definition of Thread

• Benefits of Multithread programming

• Multithreading Models

Page 5: 15 threads

5

Thread Structure

• A thread is a light weight process (LWP)

• Basic unit of CPU utilization

• Shares other threads belonging to the same process its

code section, data section and other resources

• Threads created for a particular process are known as

sibling threads

Page 6: 15 threads

6

Thread Structure

Thread comprises of

• Thread id

• Program counter

• Register counter

• Register set

• Stack

Page 7: 15 threads

Thread Structure

• Process having multiple threads of control, can perform more

than one task at a time

• Threads operate, in many respects, in the same manner as

processes

• Like processes threads can share CPU, but only one thread at a

time is active

• Thread can create child threads

• Threads are not independent of one another

• This structure does not provide protection between threads as

they can read or write over any other thread7

Page 8: 15 threads

8

Thread States

Thread states

• New

• Ready

• Blocked

• Running

• Terminated

Page 9: 15 threads

9

Threads

Single threaded process

• A traditional (or heavy weight) Process that has a single thread

of control

Multi threaded process

• A process which has several threads of control

• It can do more than one task at a time

Page 10: 15 threads

10

Single and Multi Thread Process

Fig.1

Thread

Single Thread Process Multi Thread Process

Page 11: 15 threads

11

Benefits of Multithreading Programming

Responsiveness • Allows a program to continue even if a part of it is

blocked or performing a lengthy operation

Resource sharing• Threads share the memory and the resources of the

process to which they belong• Allows an application to have several different

threads of activity within the same address space

Page 12: 15 threads

12

Benefits of Multithreading Programming

Economy• Threads share resources of the process to which

they belong• Hence it is more economical to create and

conduct switch threads

Page 13: 15 threads

13

Benefits of Multithreading Programming

Utilization of multiprocessor architectures

• Benefits of multithreading can be greatly increased

in a multiprocessor architecture where threads may

be running in parallel on different processors

• Multithreading increases concurrency on a multi-

CPU machine

Page 14: 15 threads

14

Multi Threading Model

• An operating system support multiple threads of execution in

one process

• Support for threads may be provided at either the user level or

by the kernel

• User threads are supported above the kernel and are managed

without kernel support

• Kernel threads are supported and managed directly by

operating system

• Windows XP. Solaris, True64 UNIX - support kernel threads

Page 15: 15 threads

15

One process one thread One process multiple thread

Multiple process, one thread per process Multiple process, multiple threads per process

Fig: multithreading model

Fig.1 (a) Fig.2 (b)

Fig.3 (c) Fig.4 (d)

Page 16: 15 threads

16

Multithreading Models

Many-to-one Model

• Maps many user-level threads to one kernel thread

• Green threads-a thread library available for Solaris users this

model

Fig.2

Page 17: 15 threads

17

Multithreading Models

One-to-One Model• Maps each user thread to a kernel thread

• Windows 95/98/NT/2000/XP implement the one-to-one model

Fig.3

Kernel thread

User thread

Page 18: 15 threads

18

Multithreading Models

Many-to-Many Model• Multiplexes many user level threads to a smaller or equal

number of kernel threads.

Fig.4

User thread

Kernel thread

Page 19: 15 threads

19

Summary

In this class, you have learnt

• A thread is a basic unit of CPU utilization

• A thread is a light weight process

• A thread shares with peer threads its code section

data section and other resources

• Extensive sharing makes CPU switching among

peer threads and thread creation inexpensive

Page 20: 15 threads

20

Frequently Asked Questions

1. Write short notes on threads

2. Explain the different multithreading models

Page 21: 15 threads

Other subject materials

• Web designing

• Micro processors

• C++ tutorials

• java

home

Page 22: 15 threads

22

Quiz

State whether the following statements are true or false

1. Thread is a light weight process

a) True

b) False

2. Threads can communicate with each other

a) True

b) False

3. A thread needs __________________

4. A single process _____________________

Less no. of resources

Supports multiple threads