t hreads gursharan singh tatla [email protected] 27-jan-11 1

17
THREADS Gursharan Singh Tatla [email protected] 2 7 - J a n - 1 1 1 w w w . e a z y n o t e s . c o m

Upload: leonard-watson

Post on 30-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: T HREADS Gursharan Singh Tatla professorgstatla@gmail.com 27-Jan-11 1

THREADS

Gursharan Singh [email protected]

27

-Jan-1

1

1

ww

w.e

azy

note

s.com

Page 2: T HREADS Gursharan Singh Tatla professorgstatla@gmail.com 27-Jan-11 1

THREAD

A thread is a single sequential flow of execution of the tasks of a process.

A thread is a lightweight process and the smallest unit of CPU utilization. Thus, a thread is like a miniprocess.

Each thread has a thread id, program counter, register set and a stack.

A thread undergoes different states such as new, ready, running, waiting and terminated similar to that of a process.

However, a thread is not a program as it cannot run on its own. It runs within a program.

27

-Jan-1

1

2

ww

w.e

azy

note

s.com

Page 3: T HREADS Gursharan Singh Tatla professorgstatla@gmail.com 27-Jan-11 1

MULTI-THREADING

A process can have single thread of control or multiple threads of control.

If a process has single thread of control, it can perform only one task at a time.

Many modern operating systems have extended the process concept to allow a process to have multiple threads.

Thus, allowing the process to perform multiple tasks at the same time.

This concept is known as Multi-Threading.

27

-Jan-1

1

3

ww

w.e

azy

note

s.com

Page 4: T HREADS Gursharan Singh Tatla professorgstatla@gmail.com 27-Jan-11 1

MULTI-THREADING

For e.g.: The tasks in a web browser are divided into

multiple threads.

Downloading the images, downloading the text and displaying images and text.

While one thread is busy in downloading the images, another thread displays it.

The various operating systems the implement multithreading are Windows XP, Vista, 7, Server 2000 onwards, Linux etc.

In multithreading, a thread can share its code, data and resources with other threads of same process.

27

-Jan-1

1

4

ww

w.e

azy

note

s.com

Page 5: T HREADS Gursharan Singh Tatla professorgstatla@gmail.com 27-Jan-11 1

SINGLE THREAD & MULTI-THREAD2

7-Ja

n-1

1

5

ww

w.e

azy

note

s.com

Page 6: T HREADS Gursharan Singh Tatla professorgstatla@gmail.com 27-Jan-11 1

THREADS & PROCESSES

An idea of how threads & processes can be related to each other is depicted in the fig.:

27

-Jan-1

1

6

ww

w.e

azy

note

s.com

Page 7: T HREADS Gursharan Singh Tatla professorgstatla@gmail.com 27-Jan-11 1

THREADS & PROCESSES

There are several similarities and differences between a thread and a process: Similarities:

Like process, each thread has its own program counter and stack.

Threads share CPU just as a process.

Threads also run sequentially, like a process.

Threads can create child threads.

Threads have the same states as process: new, ready, running, waiting and terminated.

27

-Jan-1

1

7

ww

w.e

azy

note

s.com

Page 8: T HREADS Gursharan Singh Tatla professorgstatla@gmail.com 27-Jan-11 1

THREADS & PROCESSES

Differences: Each process has its own distinct address space in the

main memory. On the other hand, all threads of a same process share same address space.

Threads require less system resources than a process.

Threads are not independent of each other, unlike processes.

Threads take less time for creation and termination than a process.

It takes less time to switch between two threads than to switch between two processes.

27

-Jan-1

1

8

ww

w.e

azy

note

s.com

Page 9: T HREADS Gursharan Singh Tatla professorgstatla@gmail.com 27-Jan-11 1

TYPES OF THREADS

Threads are of three types:

Kernel Level Threads

User Level Threads

Hybrid Threads

27

-Jan-1

1

9

ww

w.e

azy

note

s.com

Page 10: T HREADS Gursharan Singh Tatla professorgstatla@gmail.com 27-Jan-11 1

KERNEL LEVEL THREADS

Threads of processes defined by operating system itself are called Kernel Level Threads.

In these types of threads, kernel performs thread creation, scheduling and management.

Kernel threads are used for internal workings of operating system.

Kernel threads are slower to create and manage.

The various operating systems that support kernel level threads are: Windows 2000, XP, Solaris 2.

27

-Jan-1

1

10

ww

w.e

azy

note

s.com

Page 11: T HREADS Gursharan Singh Tatla professorgstatla@gmail.com 27-Jan-11 1

USER LEVEL THREADS

The threads of user application process are called User Level Threads.

They are implemented in the user space of main memory.

User level library (functions to manipulate user threads) is used for thread creation, scheduling and management without any support from the kernel.

User level threads are fast to create and manage.

27

-Jan-1

1

11

ww

w.e

azy

note

s.com

Page 12: T HREADS Gursharan Singh Tatla professorgstatla@gmail.com 27-Jan-11 1

HYBRID THREADS

In hybrid approach, both kernel level threads and user level threads are implemented.

For e.g.: Solaris 2.

27

-Jan-1

1

12

ww

w.e

azy

note

s.com

Page 13: T HREADS Gursharan Singh Tatla professorgstatla@gmail.com 27-Jan-11 1

MULTI-THREADING MODELS

Depending on the support for user and kernel threads, there are three multithreading models:

Many-to-One Model

One-to-One Model

Many-to-Many Model

27

-Jan-1

1

13

ww

w.e

azy

note

s.com

Page 14: T HREADS Gursharan Singh Tatla professorgstatla@gmail.com 27-Jan-11 1

MANY-TO-ONE MODEL

In this model, many user level threads are mapped to one kernel level thread.

Threads are managed in user space.

27

-Jan-1

1

14

ww

w.e

azy

note

s.com

Page 15: T HREADS Gursharan Singh Tatla professorgstatla@gmail.com 27-Jan-11 1

ONE-TO-ONE MODEL

In this model, each user level thread is mapped to one kernel level thread.

27

-Jan-1

1

15

ww

w.e

azy

note

s.com

Page 16: T HREADS Gursharan Singh Tatla professorgstatla@gmail.com 27-Jan-11 1

MANY-TO-MANY MODEL

In this model, many user level threads are mapped to many kernel level threads.

27

-Jan-1

1

16

ww

w.e

azy

note

s.com

Page 17: T HREADS Gursharan Singh Tatla professorgstatla@gmail.com 27-Jan-11 1

17