Threads - SIUE Computer Science

Download Report

Transcript Threads - SIUE Computer Science

CS 314 Operating Systems
Chapter 2 (PART 1)
Light-Weight Process (Threads)
Department of Computer Science
Southern Illinois University Edwardsville
Spring, 2016
Dr. Hiroshi Fujinoki
E-mail: [email protected]
Threads/001
CS 414 Operating Systems
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?
Threads/002
CS 414 Operating Systems
Structure of process
For some applications,
this structure gives problems
PCB
Assume an application program that
 Stack
 Code
 Heap
Threads/003
(1) Requires multiple processes to
execute an application program
Address Space
(2) Requires inter-process
communications
CS 414 Operating Systems
Example Network BBS Server
Implementation
BBS Server Host
Client A
Client B
BBS DB
Process A
BBS DB
Client C
Process C
Multiprogramming
Client D
Client A
Threads/004
Process B
Client B
Client C
CS 414 Operating Systems
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
Threads/005
CS 414 Operating Systems
Problem 1: Overhead for context switching
Client A
arrives
Process A
Running
Process B
Process C
Context-switching
overhead
Running
Ready
Ready
Client B
arrives
Running
Ready
Ready
Running
Ready
Client C
arrives
What if the number of concurrent clients increased?
Threads/006
CS 414 Operating Systems
Problem 1: Overhead for context switching (continued)
If we just increase the number of clients, what will happen?
Non-responding interval
Process A
Process B
Process C
Running
Ready
Ready
Running
Ready
Ready
Running
Ready
Ready
Running
Process D
Ready
Process E
Ready
Threads/007
Running
CS 414 Operating Systems
Problem 1: Overhead for context switching (continued)
What could we do to reduce non-responding interval?
Non-responding interval
Process A
Running
Ready
Running
Process B
Ready
Running
Process C
This causes another problem
Ready
Running
Process D
Effective CPU
goes down
Ready
Process E
Ready
Threads/008
Running
CS 414 Operating Systems
Problem 2: System call overhead in accessing shared information
BBS Server Host
BBS DB
(Shared data)
BBS DB
Process A
Process B
Process C
Multiprogramming
Process B
Process A
Process C
Client A
Threads/009
Client B
Client C
OS
CS 414 Operating Systems
Two inefficiency problems using processes in this BBS Server
 Context-switching overhead
- If number of clients is increased
Either response time or effective CPU utilization is sacrificed
 System call overhead in accessing shared information
- Shared data is implemented with another address space
Slower program execution
Light-weight process (thread) is a solution for the two problems
Threads/010
CS 414 Operating Systems Thread
Concepts of light-weight process
Multiple execution paths in a process
Global PCB
Process A
PCBA
PCBX
Address
Space
Global Stack
One Address Space
Process B
PCBB
Address
Space
Process C
PCBC
Address
Space
Threads/011
Global Heap
CS 414 Operating Systems
Concepts of light-weight process
Global PCB
Process A
Process
PCBA
PCBX
Address
Space
Global Stack
Private Stack
Code
Process B
PCBB
Private Heap
Address
Space
Private PCB
Process C
PCBC
Address
Space
Threads/012
Global Heap
CS 414 Operating Systems
Concepts of light-weight process
Process
Process A
PCBA
PCBX
Address
Space
Global Stack
Global PCB
Private Stack
Process B
Code
PCBB
Private Heap
Address
Space
Private PCB
Process C
PCBN = Global PCB + Private PCB
PCBC
Address
Space
Threads/013
Global Heap
CS 414 Operating Systems
Concepts of light-weight process
Remaining Question: How context-switching overhead can be reduced?
PCB w/out thread support
Global PCB
Private PCB
When a CPU needs to be switched
from a thread to another, only this
part should be swapped
Threads/014
CS 414 Operating Systems
Concepts of light-weight process
For CPU switching between
threads, global PCB remains
remains same
PCB w/out thread support
Global PCB
Private PCB
When a CPU needs to be switched
from a thread to another, only this
part should be swapped
Threads/015
CS 414 Operating Systems
Definition of Threads
• Thread = Multiple execution paths within a process
• Multi-threading
- Similar to “multi-programming”
- Making multiple threads in ready state
- Much less overhead for context-switching and communication
“light-weight process”
Threads/016
CS 414 Operating Systems
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
Threads/017
CS 414 Operating Systems
Major Disadvantages in threads
• No memory protection between threads
Programmers are responsible for avoiding “illegal access”
• 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
Threads/018