Title: Outline
1Outline
- Processes
- Threads
- Inter-process communication (IPC)
- Classical IPC problems
- Scheduling
2Scheduler
- Multiple processes are in state ready, which one
should go next? - Different systems have different answers.
- Cost of switching
- Switch mode, reload MMU, memory cache
3Process Behavior
A CPU-bound process spend most of its time
computing
Long CPU burst
Waiting for I/O
Short CPU burst
An I/O-bound process spend most of its time
waiting for I/O
Schedule two kinds of processes alternatively,
but impossible!
4Non-preemptive V.S. Preemptive
- Non-preemptive a process runs until blocks or
voluntarily release the CPU - Less cost of context switching, slow response
- Preemptive a process runs for a maximum of some
fixed time - Required a clock interrupt
- cost of context switching, fast response
5Schedulers Vs. Environment
Categories Non-preemptive preemptive
Batch ? ? (long interval)
Interactive None ?
Real-time ? Seldom
6When to Schedule
- A new process is created
- Run parent or child?
- A process exits
- Block on I/O, on semaphore,
- I/O interrupts
7Scheduling Algorithm Goals
- All systems
- Fairness each process a fair share of CPU
- Policy enforcement stated policy
- Balance keep all parts busy
- CPU-bound and I/O-bound processes
- Bach systems
- Through put maximize jobs/hour
- Always run short jobs
- Turnaround time minimize average waiting time
- CPU utilization keep CPU busy all the time
- CPU is the major cost.
- Maximize through put minimize turnaround time?
8Scheduling Algorithm Goals (2)
- Interactive systems
- Response time respond quickly
- Foreground process has high priorities
- Proportionality meet users expectations
- Reasonable
- Real-time systems
- Meeting deadlines avoid losing data
- Predictability process is scheduled almost
periodically.
9Scheduling in Batch Systems
- First-come first-served
- Non-preemptive, easy, fair
- New jobs are put on the end of the queue.
- How about if a process is blocked?
- Not good for a mix of CPU-bound processes and
I/O-bound processes. Why? Slow response! - Shortest job first (SJF) shortest turnaround
time - Non-preemptive, only optimal when jobs are
available simultaneously - Shortest remaining time next (SRTN)
- Preemptive version of SJF
10Problems with SJF/SRTN
- Starvation problem
- Difficult to achieve is it possible to know the
running time for processes in advance? - Using aging technique estimation of runtime for
next process from historical data
11Three-level Scheduling
Pick up the ready processes to run next
CPU
CPU scheduler
Arriving job
Main memory
Admission scheduler
Memory scheduler
Disk
Select which jobs to admit to the system
Determine which processes are in memory and which
on disk
12Scheduling in Interactive Systems
- Two-level (memory CPU) scheduling
- Round-robin
- Each process is assigned a time interval q,
called quantum - Oldest, simplest, fairest, most widely used
B running
F running
After q time
Next process to run
scheduling
Next process to run
A
F
D
G
B
D
G
A
13Issues About RR
- Need help from hardware
- Clock interrupt
- Length of quantum?
- Too short too many switches, low efficiency
- Too long high efficiency but long response time
- Can we put the same thread twice in the job queue?
14Priority Scheduling
- Each process is assigned a priority, pick up the
one with highest priority - Playing video film high priority
- Sending email low priority
- Prevent high-priority process from running
indefinitely - Assign priority dynamically
- Apply quantum
- Priority classes
- Algorithm within the class?
Highest Priority
Priority 4
Priority 3
Priority 2
Priority 1
Lowest Priority
15Multiple Queues
- Same with priority classes algorithm
- Put processes into prioritized queues
- Each process get some quanta
- Different
- Processes in higher priority queue have fewer
quanta each run, processes in lower priority
queue get more quanta each run - Processes move down one queue if not finishing
16Multiple Queues An Example
- Three queues
- Q3time quantum is 10ms, FCFS
- Q2time quantum is 20ms, SJF
- Q1time quantum is 40ms, round robin
- Scheduling
- A new process enters Q3. If it does not complete
in the first quantum, it is degraded to Q2. If it
does not complete in the first quantum, it is
degraded to Q1. - Inter-queue scheduling is strict priority.
H
Q3
Q2
Q1
L
17Some Other Methods
- Guaranteed scheduling make real promises, live
up to them - If n processes, each gets about 1/n
- Lottery scheduling
- Process with higher priority has more lottery
tickets - More tickets, more chances to get CPU
- Fair-share scheduling
- Take into account the owners of processes
18User-level Thread Scheduling
- Quantum is applied to process
- Threads in a process share the quantum of the
process - No clock interrupt within a process quantum
- Process schedules threads independently
- Possible A1, A2, A3, A1, A2, A3
- Impossible A1, B1, A2, B2, A3, B3
2. Runtime system picks a thread
Process B
Process A
1
2
3
1
2
3
1. Kernel picks a process
19Kernel-level Thread Scheduling
- Quantum is applied to thread
- Do not care which process it belongs to
- Clock interrupt
- Possible A1, A2, A3, A1, A2, A3
- Also possible A1, B1, A2, B2, A3, B3
Process B
Process A
1
2
3
Kernel picks a thread
20CMPT 300 Operating System I
21Outline
- Resources and deadlocks
- The ostrich algorithm
- Deadlock detection and recovery
- Deadlock avoidance
- Deadlock prevention
- Related issues
22Examples of Deadlock
- Deadlock on hardware resources
- A gets CD recorder and waits for scanner
- B gets scanner and waits for CD recorder
- CD recorder and scanner could be on different
machines. - Deadlock on software resources
- A gets record 100 and waits for record 300
- B gets record 300 and waits for record 600
- C gets record 600 and waits for record 100
- Involving more than 2 processes
- Deadlock processes are blocked and will remain
so forever. - Can a deadlock involve only one single-thread
process?
23Resources in Computer Systems
- Resources objects that processes want to access
exclusively - Hardware software
- Preemptable can be taken away without ill
effect, e.g. memory - Non-preemptable cannot be taken away without ill
effect, e.g. printer, CD-ROM - Use of resources request ? use ? release
- Blocked (put to sleep) if the resource is not
available.
24Resources Acquisition
Using semaphores (mutexes) to guarantee exclusive
access.
One process, one resource
One process, two resources
semaphore resource_11 semaphore
resource_21 void process() down(resource_1)
down(resource_2) use_both_resources() up(
resource2) up(resource1)
semaphore resource1 void process() down(re
source) use_resource() up(resource)
25Resource Acquisition 2-processes, 2-resources
semaphore rs_11semaphore rs_21 void
process_A(void) down(rs_1)
down(rs_2) use_resources()
up(rs_2) up(rs_1) void
process_B(void) down(rs_1)
down(rs_2) use_resources()
up(rs_2) up(rs_1)
semaphore rs_11semaphore rs_21 void
process_A(void) down(rs_1)
down(rs_2) use_resources()
up(rs_2) up(rs_2) void
process_B(void) down(rs_2)
down(rs_1) use_resources()
up(rs_1) up(rs_2)
1
3
Blocked!
2
4
Blocked!
Deadlock-free
Potential deadlock
26Conditions for Deadlocks
- Conditions for deadlocks
- Mutual exclusion
- Hold and wait
- No preemption
- Circular wait
- All conditions must be present
- Attack deadlocks by break some condition
27Deadlock Modeling RAG
A
A
R
scanner
Process node
CD ROM
Resource node
A
R
B
Deadlock!
Holding a resource
Requesting a resource
Assume there is one resource of each kind
28Deadlock ? Directed Cycle
Process B Request S Request T Release S Release T
Process C Request T Request R Release T Release R
Process A Request R Request S Release R Release S
Execution Order
A requests R B requests S C requests T A requests
S B requests T C requests R Deadlock!
A
R
29Deadlock Can Be Avoided
A requests R C requests T A requests S C requests
R A releases R A releases S NO cycle ? No
deadlock!
A
A
A
R
R
R
A
A
A
R
R
R
30Strategies for Attacking Deadlocks
- Ignore the problem altogether
- Is it a solution? Yes!
- Detection and recovery
- Let deadlocks occur, detect them and take action
- Dynamic avoidance by careful resource allocation
- Avoid deadlock by making the right resource
allocation decision all the time - Prevention by structurally negating one of the
conditions - Four necessary conditions
31Outline
- Resources and deadlock
- The ostrich algorithm
- Deadlock detection and recovery
- Deadlock avoidance
- Deadlock prevention
- Related issues
32The Ostrich Algorithm
- Ignore the deadlock problem
- Mathematically unacceptable
- Promising in engineering
- Reasonable in real world
- Most OSes (UNIX, Windows) ignore the problem
- Difficult to detect
- Expensive to attack
- Put restrictions on processes
- Trade-off between convenience and correctness
33Outline
- Resources and deadlock
- The ostrich algorithm
- Deadlock detection and recovery
- Deadlock avoidance
- Deadlock prevention
- Related issues
34Deadlock Detection
- The system may enter a deadlock state.
- The system needs
- An algorithm that periodically determines that a
deadlock has occurred. - A procedure to recovery from a deadlock
- Two situations
- One instance of each type of resource
- Multiple instances of each type of resource
35One Resource of Each Type
- A cycle in a resource allocation graph (RAG)
deadlock!
Any algorithms for detecting cycles in directed
graphs works, like depth-first search algorithm.
R
A
C
D
E
S
T
F
U
V
Process
G
W
Resource
36Multiple Resource of Each Type
- RAG does not work
- No cycle?no deadlock Yes.
- Cycle?deadlock? Not necessary!
D
E
T
U
V has multiple resources
V1, V2
G
37A Matrix-Based Algorithm
Assume r processes, s resource classes
Available resource vector A (A1, A2, A3, , As)
Existing resource vector E (E1, E2, E3, , Es)
Request matrix R
Current allocation matrix C
R11 R12 R13 R1s
R21 R22 R23 R2s
Rr1 Rr2 Rr3 Rrs
C11 C12 C13 C1s
C21 C22 C23 C2s
Cr1 Cr2 Cr3 Crs
Row i indicates the resources allocated to
process Pi
Rij is the number of resource j that Pi wants
Cij is the number of instances of resource j that
are held by process Pi.
38Algorithm Details
- Initially all processes are unmarked.
- Algorithm progressing?processes marked
- These processes are able to complete
- When algorithm terminates
- Unmarked process?deadlock
39Algorithm Details (Cont.)
- Step 1 Look for an unmarked process Pi, for
which the i-th row of R (request matrix) is less
than or equal to A (available resource vector). - X ? Y ? Xi ? Yi for 1 ? i ? m E.g.(1, 2, 1) ?
(2,2,2) - Ri ? A the resources being requested can be met
- Step 2 If such process is found, add the i-th
row of C (current allocation matrix) to A, mark
the process, go back to Step 1. - Mark process?this process is not deadlocked.
- Return the resources held by process Pi to A.
- If no such process exists, the algorithm
terminates. - All the unmarked processes are deadlocked.
40An Example
Assume r3 processes, s4 resource classes
Tape drives Plotters Scanners CD ROMs
Tape drives Plotters Scanners CD ROMs
E(4 2 3 1)
A(2 1 0 0)
P3 marked
Current allocation matrix
Request matrix
A(2 2 2 0)
2 0 0 1
1 0 1 0
2 1 0 0
0 0 1 0
2 0 0 1
0 1 2 0
P2 marked
P1P2P3
R
C
A(4 2 2 1)
P1 marked
Deadlock free!
A(4 2 3 1)
41Another Example
Tape drives Plotters Scanners CD ROMs
Tape drives Plotters Scanners CD ROMs
E(4 2 3 1)
A(2 1 0 0)
Current allocation matrix
Request matrix
0 0 1 0
2 0 0 1
0 1 2 0
2 0 0 1
1 0 1 0
2 1 0 1
C
R
No request can be satisfied
Deadlocked!
42Recovery From Deadlock
- Preemption
- Take a resource away from its current owner
- Without ill effect? Difficult or impossible.
- Rollback
- Set checkpoints and roll back to a checkpoint in
case of deadlock. - Ho frequently to set checkpoints? Which process
to rollback? which checkpoint? - Killing processes owning resource(s)
- If possible, kill those that can be rerun with no
ill effects
43Outline
- Resources and deadlock
- The ostrich algorithm
- Deadlock detection and recovery
- Deadlock avoidance
- Deadlock prevention
- Related issues
44Basic Idea Assumption
- Resources are requested one at a time.
- Whether granting a resource is safe or not
- Design an algorithm that makes right decisions
all the time - Assumption knowing the maximum requirements for
each resource by each process in advance. - In practical, difficult to know.
- Number of processes is not fixed.
45Safe And Unsafe States
- System state determined by E, A, C and R.
- A system is in a safe state if there exists a
sequence of processes P1, P2Pr, where - For each Pi, the resources that Pi will still
require can be satisfied by the currently
available resources (A vector) plus the resources
held by all Pj, jlti. - E.g., P1 can be satisfied by A, P2 can be
satisfied by A plus the resource held by P1. - Safe? no deadlock. Deadlock?unsafe
- Unsafe? possible deadlock
46Safe State An Example
Three processes A, B and C. One resource (10
instances).
Has Max
A 3 9
B 2 4
C 2 7
Has Max
A 3 9
B 4 4
C 2 7
Has Max
A 3 9
B 0 -
C 2 7
Free 3
Free 1
Free 5
Has Max
A 3 9
B 0 -
C 7 7
Has Max
A 3 9
B 0 -
C 0 -
Safe state
Process Sequence B?C?A
Free 0
Free 7
47Unsafe State An Example
Has Max
A 4 9
B 2 4
C 2 7
Has Max
A 3 9
B 2 4
C 2 7
ARequire 1
unsafe state
Free 2
Safe state
Free 3
Has Max
A 4 9
B 4 4
C 2 7
Has Max
A 4 9
B 0 -
C 2 7
Free 0
Free 4
deadlock!