Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois...

18
Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois University Edwardsville Summer, 2004 Dr. Hiroshi Fujinoki E-mail: [email protected] S 414 Operating Systems Threads/001

Transcript of Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois...

Page 1: Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois University Edwardsville Summer, 2004 Dr. Hiroshi Fujinoki.

Chapter 2 (PART 1) Light-Weight Process (Threads)

Department of Computer ScienceSouthern Illinois University Edwardsville

Summer, 2004

Dr. Hiroshi FujinokiE-mail: [email protected]

CS 414 Operating Systems

Threads/001

Page 2: Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois University Edwardsville Summer, 2004 Dr. Hiroshi Fujinoki.

CS 414 Operating Systems

Threads/002

Presentation Agenda

• Two inefficiency problems in “process”

The two problems motivate “light-weigh process”

(“Light-weight process” = “Thread”)

• Introduce “thread” as a solution for the two problems in “process”

How thread works?

Page 3: Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois University Edwardsville Summer, 2004 Dr. Hiroshi Fujinoki.

CS 414 Operating Systems

Threads/003

Structure of process

PCB

Address Space

Stack

Code

Heap

For some applications, this structure gives problems

Assume an application program that

(1) Requires multiple processes to execute an application program

(2) Requires inter-process communications

Page 4: Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois University Edwardsville Summer, 2004 Dr. Hiroshi Fujinoki.

BBS Server Host

Implementation

CS 414 Operating Systems

Threads/004

Example Network BBS Server

BBS DB

Client A Client B

Client C Client D

BBS DB

Client A Client B Client C

Process A Process B Process C

Multiprogramming

Page 5: Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois University Edwardsville Summer, 2004 Dr. Hiroshi Fujinoki.

CS 414 Operating Systems

Threads/005

Two inefficiency problems using processes in this BBS Server

Context-switching overhead

- When a context-switch happens, an entire PCB of the running process has to be saved and the one for the next has to be loaded.

System call overhead in accessing shared information

- In order for the BBS information to be shared, the shared information needs to be in a different address space

- To access data in a different address space, each process needs to call a system call

Page 6: Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois University Edwardsville Summer, 2004 Dr. Hiroshi Fujinoki.

CS 414 Operating Systems

Threads/006

Problem 1: Overhead for context switching

Process A

Process B

Process C

Client Barrives

Client Aarrives

Running

Ready

Ready

Ready

Ready

What if the number of concurrent clients increased?

Running

Client Carrives

Ready Running

Running

Context-switchingoverhead

Page 7: Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois University Edwardsville Summer, 2004 Dr. Hiroshi Fujinoki.

CS 414 Operating Systems

Threads/007

Problem 1: Overhead for context switching (continued)

If we just increase the number of clients, what will happen?

Process A

Process B

Process C

Process D

Process E

Running

Ready Running

Ready

Ready

ReadyReady Running

Ready Running

Ready Running

Ready

Non-responding interval

Page 8: Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois University Edwardsville Summer, 2004 Dr. Hiroshi Fujinoki.

CS 414 Operating Systems

Threads/008

Problem 1: Overhead for context switching (continued)

What could we do to reduce non-responding interval?

Process A

Process B

Process C

Process D

Process E

Running

Ready

Ready

Ready

Running

Ready

Running

Ready

Running

Running

This causes another problem

Effective CPUgoes down

Non-responding interval

Page 9: Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois University Edwardsville Summer, 2004 Dr. Hiroshi Fujinoki.

CS 414 Operating Systems

Threads/009

Problem 2: System call overhead in accessing shared information

BBS Server Host BBS DB

Client A Client B Client C

Process A Process B Process C

Multiprogramming Process A

Process C

Process B

BBS DB(Shared data)

OS

Page 10: Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois University Edwardsville Summer, 2004 Dr. Hiroshi Fujinoki.

CS 414 Operating Systems

Threads/010

Two inefficiency problems using processes in this BBS Server

- If number of clients is increased

Context-switching overhead

System call overhead in accessing shared information

Either response time or effective CPU utilization is sacrificed

- Shared data is implemented with another address space

Slower program execution

Light-weight process (thread) is a solution for the two problems

Page 11: Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois University Edwardsville Summer, 2004 Dr. Hiroshi Fujinoki.

On

e Ad

dress S

pace

CS 414 Operating Systems

Threads/011

Concepts of light-weight process

Multiple execution paths in a processThread

PCBA

AddressSpace

Process A

PCBB

AddressSpace

Process B

PCBC

AddressSpace

Process C

PCBX

Global Stack

Global Heap

Global PCB

Page 12: Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois University Edwardsville Summer, 2004 Dr. Hiroshi Fujinoki.

Process

CS 414 Operating Systems

Threads/012

Concepts of light-weight process

PCBA

AddressSpace

Process A

PCBB

AddressSpace

Process B

PCBC

AddressSpace

Process C

PCBX

Global Stack

Global Heap

Global PCB

Private Stack

Code

Private Heap

Private PCB

Page 13: Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois University Edwardsville Summer, 2004 Dr. Hiroshi Fujinoki.

CS 414 Operating Systems

Threads/013

Concepts of light-weight process

PCBA

AddressSpace

Process A

PCBB

AddressSpace

Process B

PCBC

AddressSpace

Process C

Process

PCBX

Global Stack

Global Heap

Private Stack

Code

Private Heap

Private PCB

Global PCB

PCBN = Global PCB + Private PCB

Page 14: Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois University Edwardsville Summer, 2004 Dr. Hiroshi Fujinoki.

CS 414 Operating Systems

Threads/014

Concepts of light-weight process

PCB w/out thread support

Global PCBPrivate PCB

When a CPU needs to be switched from a thread to another, only this

part should be swapped

Remaining Question: How context-switching overhead can be reduced?

Page 15: Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois University Edwardsville Summer, 2004 Dr. Hiroshi Fujinoki.

CS 414 Operating Systems

Threads/015

Concepts of light-weight process

PCB w/out thread support

Global PCBPrivate PCB

When a CPU needs to be switched from a thread to another, only this

part should be swapped

For CPU switching between threads, global PCB remains

remains same

Page 16: Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois University Edwardsville Summer, 2004 Dr. Hiroshi Fujinoki.

CS 414 Operating Systems

Threads/016

Definition of Threads

• Thread =

• Multi-threading

- Making multiple threads in ready state

- Similar to “multi-programming”

- Much less overhead for context-switching and communication

Multiple execution paths within a process

“light-weight process”

Page 17: Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois University Edwardsville Summer, 2004 Dr. Hiroshi Fujinoki.

CS 414 Operating Systems

Threads/017

Major Advantages in threads

• Thread reduces context-switching and inter-process communication overhead (thus “light-weight”).

• The general advantages in threads are:

- Share all resources assigned to a “process”

Files opened, I/O devices assigned, global memory allocated, etc.

- Ability to execute multiple different tasks in asynchronous way

With easy and fast inter-thread communication

Page 18: Chapter 2 (PART 1) Light-Weight Process (Threads) Department of Computer Science Southern Illinois University Edwardsville Summer, 2004 Dr. Hiroshi Fujinoki.

CS 414 Operating Systems

Threads/018

Major Disadvantages in threads

• No memory protection between threads

• Managing assigned I/O resources could be very complex

- Any thread in a process can use any resources assigned to a process

- Multiple threads can be executed in any order

Example

• Implementation of threads is not standardized

- Each major OS (Solaris, LINUX, BSD, Windows) has its own implementation of threads and all different

Programmers are responsible for avoiding “illegal access”