25
Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads • Process concept • Process scheduling • Interprocess communication • Deadlocks • Threads

Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept Process scheduling Interprocess communication Deadlocks Threads

  • View
    226

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-1

Chapter 2.5 : Threads

• Process concept • Process scheduling • Interprocess communication • Deadlocks• Threads

Page 2: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-2

These lecture notes have been adapted from• How to program with threads

An introduction to multithreaded programming

By Bil Lewis and Daniel J. Bergand

• Tanenbaum slides

Page 3: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-3

Processes & Threads• Processes and threads are related concepts• A process is a kernel-level entity

– Process structure can only be accessed through system calls

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

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

Page 4: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-4

ThreadsThe Thread Model (1)

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

Page 5: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-5

The Thread Model (2)

• Items shared by all threads in a process

• Items private to each thread

Page 6: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-6

The Thread Model (3)

Each thread has its own stack

Page 7: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-7

Process and Thread Data Structures

Kernel Space

User Space

PCB

Code Data Stack

TCB1 TCB2 TCB3

Page 8: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-8

Characteristics of Threads

• The TCB (thread control block) consist of

– program counter

– register set

– stack space

Thus the TCB is a reduced PCB

• A traditional process is equal to a task with one thread

• All threads in a process share the state of that process

Page 9: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-9

Characteristics of Threads (Cont.)

• They reside in the exact same memory space (user

memory), see the same code and data

• When one thread alters a process variable (say, the

working directory), all the others will see the

change when they next access it

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

threads can also read from it.

Page 10: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-10

Characteristics of Threads (Cont.)

• Because no system calls are involved, threads are

fast

• There are no kernel structures affected by the

existence of threads in a program, so no kernel

resources are consumed -- threads are cheap

• The kernel doesn't even know that threads exist

Page 11: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-11

Thread Scheduling (1)

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

Page 12: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-12

Thread Scheduling (2)

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

Page 13: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-13

Threads of a Task

Threads

Code segmentData segment

Program Counter

Threads

Task

Page 14: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-14

Implementing Threads in User Space

A user-level threads package

Page 15: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-15

Implementing Threads in the Kernel

A threads package managed by the kernel

Page 16: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-16

Hybrid Implementations

Multiplexing user-level threads onto kernel- level threads

Page 17: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-17

Single vs. Multiple Threads of Execution

Start

End

Edit Document

Print Document

Single Thread Multiple Threads

End

Edit Document

Start

Print Document

Page 18: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-18

Thread Usage (1)

A word processor with three threads

Page 19: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-19

Thread Usage (2)

A multithreaded Web server

Page 20: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-20

Thread Usage (3)

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

Page 21: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-21

Some Benefits of Writing Multithreaded Programs:

Performance gains from multiprocessing hardware (parallelism)

Increased application throughput Increased application responsiveness Enhanced process-to-process

communications

Page 22: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-22

Parallellism

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

Page 23: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-23

Throughput

• When a traditional, single-threaded program requests a service from the operating system, it must wait for that service to complete, often leaving the CPU idle

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

Page 24: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-24

Responsiveness

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

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

user

Page 25: Ceng 334 - Operating Systems 2.5-1 Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads

Ceng 334 - Operating Systems 2.5-25

Communications

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

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

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