25
1 Chapter 2.5 : Threads Chapter 2.5 : Threads Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Deadlocks Deadlocks Threads Threads

1 Chapter 2.5 : Threads Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication

Embed Size (px)

Citation preview

Page 1: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

1

Chapter 2.5 : ThreadsChapter 2.5 : Threads

Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication DeadlocksDeadlocks ThreadsThreads

Page 2: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

2

ThreadsThreads

These lecture notes have been These lecture notes have been adapted fromadapted from

How to program with threadsHow to program with threadsAn introduction to multithreaded programming

By Bil Lewis and Daniel J. BergBy Bil Lewis and Daniel J. Bergandand

Tanenbaum slidesTanenbaum slides

Page 3: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

3

Processes & ThreadsProcesses & Threads Processes and threads are related Processes and threads are related

conceptsconcepts A process is a kernel-level entity A process is a kernel-level entity

Process structure can only be accessed Process structure can only be accessed through system callsthrough system calls

A thread (A thread (or a lightweight processor a lightweight process) is ) is a user-level entitya user-level entity The thread structure is in user space The thread structure is in user space It is accessed directly with the thread It is accessed directly with the thread

library calls, which are just normal user-library calls, which are just normal user-level functions (threads do not use system level functions (threads do not use system calls) calls)

Page 4: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

4

Single vs. Multiple Single vs. Multiple Threads of ExecutionThreads of Execution

Start

End

Edit Document

Print Document

Single Thread Multiple Threads

End

Edit Document

Start

Print Document

Page 5: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

5

Thread Usage (1)Thread Usage (1)

A word processor with three threadsA word processor with three threads

Page 6: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

6

Thread Usage (2)Thread Usage (2)

A multithreaded Web serverA multithreaded Web server

Page 7: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

7

Thread Usage (3)Thread Usage (3)

Rough outline of code for previous slideRough outline of code for previous slide(a) Dispatcher thread(a) Dispatcher thread(b) Worker thread(b) Worker thread

Page 8: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

8

The Classical The Classical Thread Thread Model (1)Model (1)

(a) Three processes each with one thread(a) Three processes each with one thread(b) One process with three threads(b) One process with three threads

Page 9: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

9

The Thread Model (2)The Thread Model (2)

((Per process itemsPer process items) Items shared by all threads ) Items shared by all threads in a processin a process

((Per thread itemsPer thread items) Items private to each thread) Items private to each thread

Page 10: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

10

The Thread Model (3)The Thread Model (3)

Each thread has its own stackEach thread has its own stack

Page 11: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

11

Process and Thread Data Process and Thread Data StructuresStructures

Kernel Space

User Space

PCB

Code Data Stack

TCB1 TCB2 TCB3

Page 12: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

12

Characteristics of Characteristics of ThreadsThreads

The TCB (thread control block) consist of The TCB (thread control block) consist of program counterprogram counter register setregister set stack spacestack space

Thus the TCB is a reduced PCBThus the TCB is a reduced PCB A traditional process is equal to a task A traditional process is equal to a task

with one threadwith one thread All threads in a process share the state of All threads in a process share the state of

that process that process

Page 13: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

13

Characteristics of Characteristics of Threads (Cont.)Threads (Cont.)

They reside in the exact same memory They reside in the exact same memory

space (user memory), see the same code space (user memory), see the same code

and data and data

When one thread alters a process variable When one thread alters a process variable

(say, the working directory), all the others (say, the working directory), all the others

will see the change when they next access it will see the change when they next access it

If one thread opens a file to read it, all the If one thread opens a file to read it, all the

other threads can also read from it.other threads can also read from it.

Page 14: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

14

Characteristics of Characteristics of Threads (Cont.)Threads (Cont.)

Because no system calls are involved, Because no system calls are involved,

threads are fastthreads are fast

There are no kernel structures affected by There are no kernel structures affected by

the existence of threads in a program, so the existence of threads in a program, so

no kernel resources are consumed -- no kernel resources are consumed --

threads are cheap threads are cheap

The kernel doesn't even know that threads The kernel doesn't even know that threads

existexist

Page 15: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

15

Thread Scheduling (1)Thread Scheduling (1)

Possible scheduling of user-level threadsPossible scheduling of user-level threads 50-msec process quantum50-msec process quantum threads run 5 msec/CPU burstthreads run 5 msec/CPU burst

Page 16: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

16

Thread Scheduling (2)Thread Scheduling (2)

Possible scheduling of kernel-level threadsPossible scheduling of kernel-level threads 50-msec process quantum50-msec process quantum threads run 5 msec/CPU burstthreads run 5 msec/CPU burst

Page 17: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

17

Threads of a TaskThreads of a Task

Threads

Code segmentData segment

Program Counter

Task

Page 18: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

18

Implementing Threads in Implementing Threads in User SpaceUser Space

A user-level threads packageA user-level threads package

Page 19: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

19

Implementing Threads in Implementing Threads in the Kernelthe Kernel

A threads package managed by the kernelA threads package managed by the kernel

Page 20: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

20

Hybrid ImplementationsHybrid Implementations

Multiplexing user-level threads onto kernel- level Multiplexing user-level threads onto kernel- level threadsthreads

Page 21: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

21

Some Benefits of Some Benefits of Writing Multithreaded Writing Multithreaded

Programs: Programs: Performance gains from multiprocessing

hardware (parallelism) Increased application throughput Increased application responsiveness Enhanced process-to-process

communications

Page 22: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

22

ParallellismParallellism

Different threads can run on Different threads can run on different processors different processors simultaneously with no special simultaneously with no special input from the user and no effort input from the user and no effort on the part of the programmeron the part of the programmer

Page 23: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

23

ThroughputThroughput When a traditional, single-threaded When a traditional, single-threaded

program requests a service from the program requests a service from the operating system, it must wait for operating system, it must wait for that service to complete, often that service to complete, often leaving the CPU idleleaving the CPU idle

Multithreading provides progress Multithreading provides progress even though one or more threads wait even though one or more threads wait for an event as long as other threads for an event as long as other threads are activeare active

Page 24: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

24

ResponsivenessResponsiveness Blocking one part of a process need not Blocking one part of a process need not

block the whole process. Single-threaded block the whole process. Single-threaded applications that do something lengthy when applications that do something lengthy when a button is pressed typically display a a button is pressed typically display a "please wait" cursor and freeze while the "please wait" cursor and freeze while the operation is in progress operation is in progress

If such applications were multithreaded, If such applications were multithreaded, long operations could be done by long operations could be done by independent threads, allowing the independent threads, allowing the application to remain active and making the application to remain active and making the

application more responsive to the userapplication more responsive to the user

Page 25: 1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication

25

CommunicationsCommunications An application that uses multiple processes An application that uses multiple processes

to accomplish its tasks can be replaced by to accomplish its tasks can be replaced by an application that uses multiple threads to an application that uses multiple threads to accomplish those same tasks accomplish those same tasks

Processes-to-process communication Processes-to-process communication through traditional IPC (interprocess through traditional IPC (interprocess communications) facilities (e.g., pipes or communications) facilities (e.g., pipes or sockets)sockets)

The threaded application can use the The threaded application can use the inherently shared memory of the process inherently shared memory of the process