主講人:虞台文

63
Operating Systems Principles Process Management and Coordination Lecture 5: Process and Thread Scheduling 主主主 主主主

description

Operating Systems Principles Process Management and Coordination Lecture 5: Process and Thread Scheduling. 主講人:虞台文. Content. Organization of Schedulers Embedded and Autonomous Schedulers Priority Scheduling Scheduling Methods A Framework for Scheduling Common Scheduling Algorithms - PowerPoint PPT Presentation

Transcript of 主講人:虞台文

Page 1: 主講人:虞台文

Operating Systems PrinciplesProcess Management and Coordination

Lecture 5:Process and Thread Scheduling

主講人:虞台文

Page 2: 主講人:虞台文

Content

Organization of Schedulers – Embedded and Autonomous Schedulers – Priority Scheduling

Scheduling Methods – A Framework for Scheduling – Common Scheduling Algorithms – Comparison of Methods

Priority Inversion

Page 3: 主講人:虞台文

Operating Systems PrinciplesProcess Management and Coordination

Lecture 5:Process and Thread Scheduling

Organization of Schedulers

Page 4: 主講人:虞台文

Process Scheduling/Dispatching

Process scheduling– Long term scheduling– Move process to Ready List (“RL”) after

creation (When and in which order?)

Process Dispatching– Short term scheduling– Select process from Ready List to run

We use “scheduling” to refer to both.

Page 5: 主講人:虞台文

Organization of Schedulers

Embedded– Called as function at

end of kernel call– Runs as part of calling

process

Autonomous– Separate process– May have dedicated

CPU on a multiprocessor– On single-processor,

run at every quantum:scheduler and other processes alternate

pi: processS : scheduler

Page 6: 主講人:虞台文

Organization of Schedulers

Embedded– Called as function at

end of kernel call– Runs as part of calling

process

Autonomous– Separate process– May have dedicated

CPU on a multiprocessor– On single-processor,

run at every quantum:scheduler and other processes alternate

OSOS

p1p1

SOSOS

p2p2

SOSOS

p3p3

SOSOS

p4p4

S

OSOS

p1p1

OSOS

p2p2

OSOS

p3p3

OSOS

p4p4

OSOS

SSUnixUnix

Windows 2000Windows 2000

Page 7: 主講人:虞台文

Priority Scheduling

Priority function returns numerical value P for process p:

P = Priority(p)

Static priority: unchanged for lifetime of p Dynamic priority: changes at runtime

Who runs next?Based on priority

Page 8: 主講人:虞台文

Priority-Leveled Processes

Priority divides processes into levels Implemented as multilevel ready list, say, RL

– p at RL[i] run before q at RL[j] if i > j– p, q at same level are ordered by other criteria, e.g., the o

rder of arrival.

RL[n]1

2

n 1

nf rontrear

1

2

n 1

nf rontrearf rontrearf rontrear

1

2

n 1

nf rontrear

1

2

n 1

nf rontrearf rontrearf rontrear

Lowest

Highest

Lowest

Highest

Page 9: 主講人:虞台文

The Processes in Ready List

Status.Type {running, ready_a, ready_s}

1

2

n 1

nf rontrear

1

2

n 1

nf rontrearf rontrearf rontrear

1

2

n 1

nf rontrear

1

2

n 1

nf rontrearf rontrearf rontrear

Page 10: 主講人:虞台文

The Scheduler

Page 11: 主講人:虞台文

The Scheduler

The scheduler may be invoked

periodically

when some events affect the priorities of existing processes, e.g.,

– a process is blocked/suspended

– a process is destroyed

– a new process created

– a process is activated

The scheduler may be invoked

periodically

when some events affect the priorities of existing processes, e.g.,

– a process is blocked/suspended

– a process is destroyed

– a new process created

– a process is activated

Page 12: 主講人:虞台文

The Scheduler

The scheduler may be invoked

periodically

when some events affect the priorities of existing processes, e.g.,

– a process is blocked/suspended

– a process is destroyed

– a new process created

– a process is activated

The scheduler may be invoked

periodically

when some events affect the priorities of existing processes, e.g.,

– a process is blocked/suspended

– a process is destroyed

– a new process created

– a process is activated

Some processes in RL can get

CPU.

Some processes in RL can get

CPU.

Running processes may be

preempted.

Running processes may be

preempted.

The scheduler/dispatcher should always keep the np (#processors) highest-priority active processes running.

Page 13: 主講人:虞台文

Invoking the Scheduler

Request(res) {if (Free(res))Allocate(res, self)

else {Block(self, res);Scheduler();

}}

Release(res) {Deallocate(res, self);if (Process_Blocked_in(res,pr)) {Allocate(res, pr);Unblock(pr, res);Scheduler();

}}

The caller to the scheduler may be a process to be• blocked/suspended or• awakened/activated

Page 14: 主講人:虞台文

Invoking the Scheduler

Create(s0, m0, pi, pid) { p = Get_New_PCB(); pid = Get_New_PID(); p->ID = pid; p->CPU_State = s0; p->Memory = m0; p->Priority = pi; p->Status.Type = ’ready_s’; p->Status.List = RL; p->Creation_Tree.Parent = self; p->Creation_Tree.Child = NULL; insert(self-> Creation_Tree.Child, p); insert(RL, p);Activate(); Scheduler();

}

Create(s0, m0, pi, pid) { p = Get_New_PCB(); pid = Get_New_PID(); p->ID = pid; p->CPU_State = s0; p->Memory = m0; p->Priority = pi; p->Status.Type = ’ready_s’; p->Status.List = RL; p->Creation_Tree.Parent = self; p->Creation_Tree.Child = NULL; insert(self-> Creation_Tree.Child, p); insert(RL, p);Activate(); Scheduler();

}

Suspend(pid) { p = Get_PCB(pid); s = p->Status.Type; if ((s==’blocked_a’)||(s==’blocked_s’))p->Status.Type = ’blocked_s’;

elsep->Status.Type = ’ready_s’;

if (s==’running’) {cpu = p->Processor_ID; p->CPU_State = Interrupt(cpu);Scheduler();

} }

Suspend(pid) { p = Get_PCB(pid); s = p->Status.Type; if ((s==’blocked_a’)||(s==’blocked_s’))p->Status.Type = ’blocked_s’;

elsep->Status.Type = ’ready_s’;

if (s==’running’) {cpu = p->Processor_ID; p->CPU_State = Interrupt(cpu);Scheduler();

} }

Activate(pid) {

p = Get_PCB(pid);

if (p->Status.Type == ’ready_s’) {

p->Status.Type = ’ready_a’;

Scheduler();

}

else

p->Status.Type = ’blocked_a’;

}

Activate(pid) {

p = Get_PCB(pid);

if (p->Status.Type == ’ready_s’) {

p->Status.Type = ’ready_a’;

Scheduler();

}

else

p->Status.Type = ’blocked_a’;

}

Destroy(pid) {

p = Get_PCB(pid);

Kill_Tree(p);

Scheduler();

}

Destroy(pid) {

p = Get_PCB(pid);

Kill_Tree(p);

Scheduler();

}

The caller to the scheduler may be a process to be• blocked/suspended or• awakened/activated

Page 15: 主講人:虞台文

An Embedded Scheduler

Scheduler() {

do {

Find highest priority ready_a process p;

Find a free cpu;

if (cpu != NIL) Allocate_CPU(p,cpu);

} while (cpu != NIL);

do {

Find highest priority ready_a process p;

Find lowest priority running process q;

if (Priority(p) > Priority(q)) Preempt(p,q);

} while (Priority(p) > Priority(q));

if (self->Status.Type!=’running’) Preempt(p,self);

}

The caller to the scheduler may be a process to be• blocked/suspended or• awakened/activated

If free CPUs are available, allocate these free CPUs to high-priority processes in ready_a state

If free CPUs are available, allocate these free CPUs to high-priority processes in ready_a state

To preempt low-priority running processes by high-priority ready_a ones.

To preempt low-priority running processes by high-priority ready_a ones.

If the caller is going to be idled, preempt itself.If the caller is going to be idled, preempt itself.

Page 16: 主講人:虞台文

An Embedded Scheduler

Scheduler() {

do {

Find highest priority ready_a process p;

Find a free cpu;

if (cpu != NIL) Allocate_CPU(p,cpu);

} while (cpu != NIL);

do {

Find highest priority ready_a process p;

Find lowest priority running process q;

if (Priority(p) > Priority(q)) Preempt(p,q);

} while (Priority(p) > Priority(q));

if (self->Status.Type!=’running’) Preempt(p,self);

}

The caller to the scheduler may be a process to be• blocked/suspended or• awakened/activated

To preempt low-priority running processes by high-priority ready_a ones.

To preempt low-priority running processes by high-priority ready_a ones.

If the caller is going to be idled, preempt itself.If the caller is going to be idled, preempt itself.

Page 17: 主講人:虞台文

An Embedded Scheduler

Scheduler() {

do {

Find highest priority ready_a process p;

Find a free cpu;

if (cpu != NIL) Allocate_CPU(p,cpu);

} while (cpu != NIL);

do {

Find highest priority ready_a process p;

Find lowest priority running process q;

if (Priority(p) > Priority(q)) Preempt(p,q);

} while (Priority(p) > Priority(q));

if (self->Status.Type!=’running’) Preempt(p,self);

}

The caller to the scheduler may be a process to be• blocked/suspended or• awakened/activated

If the caller is going to be idled, preempt itself.If the caller is going to be idled, preempt itself.

Page 18: 主講人:虞台文

An Embedded Scheduler

Scheduler() {

do {

Find highest priority ready_a process p;

Find a free cpu;

if (cpu != NIL) Allocate_CPU(p,cpu);

} while (cpu != NIL);

do {

Find highest priority ready_a process p;

Find lowest priority running process q;

if (Priority(p) > Priority(q)) Preempt(p,q);

} while (Priority(p) > Priority(q));

if (self->Status.Type!=’running’) Preempt(p,self);

}

The caller to the scheduler may be a process to be• blocked/suspended or• awakened/activated

Page 19: 主講人:虞台文

An Embedded Scheduler

Scheduler() {

do {

Find highest priority ready_a process p;

Find a free cpu;

if (cpu != NIL) Allocate_CPU(p,cpu);

} while (cpu != NIL);

do {

Find highest priority ready_a process p;

Find lowest priority running process q;

if (Priority(p) > Priority(q)) Preempt(p,q);

} while (Priority(p) > Priority(q));

if (self->Status.Type!=’running’) Preempt(p,self);

}

How to simplify the scheduler for a uniprocessor system?

Page 20: 主講人:虞台文

Operating Systems PrinciplesProcess Management and Coordination

Lecture 5:Process and Thread Scheduling

Scheduling Methods

Page 21: 主講人:虞台文

Scheduling Policy

Scheduling based on certain criteria.

For examples:– batch jobs vs. interactive users– System processes vs. user processes– I/O bound processes vs. CPU bound

processes

Page 22: 主講人:虞台文

The General Framework

When to schedule?– Preemptive– Nonpreemptive

Who to schedule?– Priority evaluation– Arbitration to break ties

Decision Mode

• Decision mode• Priority function• Arbitration Rule

A scheduling policy is determined by:

Page 23: 主講人:虞台文

The Decision Modes

Preemptive: scheduler called– periodically (quantum-oriented) or – when system state changes

More costly

Nonpreemptive: scheduler called– when process terminates or blocks

Not adequate in real-time or time-shared systems

• Decision mode• Priority function• Arbitration Rule

A scheduling policy is determined by:

Page 24: 主講人:虞台文

The Priority Function

• Decision mode• Priority function• Arbitration Rule

A scheduling policy is determined by:

Possible parameters:– Attained service time (a)– Real time in system (r)– Total service time (t)– Period (d)– Deadline (explicit or implied by period)– External priority (e)– Memory requirements (mostly for batch)– System load (not process-specific)

Page 25: 主講人:虞台文

The Arbitration Rules

• Decision mode• Priority function• Arbitration Rule

A scheduling policy is determined by:

Break ties among processes of the same priority– Random– Chronological (First In First Out = FIFO)– Cyclic (Round Robin = RR)

Page 26: 主講人:虞台文

Common Scheduling Algorithms

First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)

Page 27: 主講人:虞台文

Example Processesfor Scheduling

First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)

0 1 2 3 4 5 6 7 8 9

p1

p2

Page 28: 主講人:虞台文

FIFO

First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)

0 1 2 3 4 5 6 7 8 9

p1

p2

CPU does other things

StartScheduling

p1

p2

Page 29: 主講人:虞台文

FIFO

First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)

0 1 2 3 4 5 6 7 8 9

p1

p2

CPU does other things

StartScheduling

p1

p2

Page 30: 主講人:虞台文

SJF

First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)

0 1 2 3 4 5 6 7 8 9

p1

p2

CPU does other things

StartScheduling

p1

p2

Page 31: 主講人:虞台文

SJF

First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)

0 1 2 3 4 5 6 7 8 9

p1

p2

CPU does other things

StartScheduling

p1

p2

Page 32: 主講人:虞台文

SRT

First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)

0 1 2 3 4 5 6 7 8 9

p1

p2

CPU does other things

StartScheduling

p1

p2

p1

Page 33: 主講人:虞台文

SRT

First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)

0 1 2 3 4 5 6 7 8 9

p1

p2

CPU does other things

StartScheduling

p1

p2

p1

Page 34: 主講人:虞台文

RR

First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)

0 1 2 3 4 5 6 7 8 9

p1

p2

CPU does other things

StartScheduling

p1 p1

Assume a time-quantum of 0.1 time units.

p2

p1

Page 35: 主講人:虞台文

RR

First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)

0 1 2 3 4 5 6 7 8 9

p1

p2

CPU does other things

StartScheduling

p1 p1

Assume a time-quantum of 0.1 time units.

p2

p1

Page 36: 主講人:虞台文

ML

First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)

Page 37: 主講人:虞台文

1

2

n 1

n frontrear

ML

First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)

Lowest

Highest

Each process has a fixed priority.

Page 38: 主講人:虞台文

1

2

n 1

n frontrear

ML (Nonpreemptive)

First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)

Lowest

Highest

Each process has a fixed priority.

Page 39: 主講人:虞台文

1

2

n 1

n frontrear

ML (Preemptive)

First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)

Lowest

Highest

Each process has a fixed priority.

Page 40: 主講人:虞台文

ML (Nonpreemptive)

First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)

0 1 2 3 4 5 6 7 8 9

p1

p2

ep1 = 15

ep2 = 14

CPU does other things

StartScheduling

p1

p2

Page 41: 主講人:虞台文

MLF• Like ML, but priority chang

es dynamically• Every process enters at hig

hest level n• Each level P prescribes ma

ximum time tP

• tP increases as P decreases

• Typically: tn = T (constant)

tP = 2 tP+1

First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)

Page 42: 主講人:虞台文

MLF• The priority of a process is a

function of a.

• Find P for given a:

priority attained timen a<Tn – 1 a<T+2Tn – 2 a<T+2T+4T. . . . . .n – i a<(2i+1–1)T

First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)

a: attained service time

P = n – i = n – lg2(a/T+1)

Page 43: 主講人:虞台文

MLF• The priority of a process is a

function of a.

• Find P for given a:

priority attained timen a<Tn – 1 a<T+2Tn – 2 a<T+2T+4T. . . . . .n – i a<(2i+1–1)T

First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)

a: attained service time

P = n – i = n – lg2(a/T+1)

Page 44: 主講人:虞台文

RM & EDF

First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)

RM– Intended for periodic (real-time) processes– Preemptive – Highest priority: shortest period: P = –d

EDF– Intended for periodic (real-time) processes– Preemptive – Highest priority: shortest time to next deadline

r d number of completed periods

r % d time in current period

d – r % d time remaining in current period

P = –(d – r % d)

Page 45: 主講人:虞台文

Common Scheduling Algorithms

Page 46: 主講人:虞台文

Comparison of Methods

• FIFO, SJF, SRT: Primarily for batch systems

• FIFO is the simplest

• SJF & SRT have better average turnaround times:

1

1 n

ii

AVT rn

ri: the real time that the ith process spends in the system.

Page 47: 主講人:虞台文

Example 1

1 n

ii

AVT rn

arrival servicep1

p2

p3

t

t

t 3

4

2

1

Comparison ofaverage turnaround time

Page 48: 主講人:虞台文

Comparison of Methods

Time-sharing systems• Response time is critical• RR or MLF with RR within each queue are suitable

Time-sharing systems• Response time is critical• RR or MLF with RR within each queue are suitable

Page 49: 主講人:虞台文

Comparison of Methods

Time-sharing systems– Response time is critical– RR or MLF with RR within each queue are suitable

Choice of quantum determines overhead– When q , RR approaches FIFO– When q 0, context switch overhead 100%– When q >> context switch overhead,

n processes run concurrently at 1/n CPU speed

Page 50: 主講人:虞台文

Interactive Systems

1

2

n 1

nf rontrear

1

2

n 1

nf rontrearf rontrearf rontrear

Most dynamic schemes tend to move interactive and I/O-bound processes to the top of the priority queues and to let CPU-bound processes drift to lower levels.

MLF puts I/O-completed processes into highest priority queue.

Page 51: 主講人:虞台文

Comparison of Methods

• Real-time systems– Feasible = All deadlines are met– CPU utilization is defined as: U=∑ ti/di

– Schedule is feasible if U 1– EDF always yields feasible schedule (if U 1)– RM yields feasible schedule if U 0.7

• Real-time systems– Feasible = All deadlines are met– CPU utilization is defined as: U=∑ ti/di

– Schedule is feasible if U 1– EDF always yields feasible schedule (if U 1)– RM yields feasible schedule if U 0.7

Page 52: 主講人:虞台文

Example: RM and EDF

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4

1 2

1 2

1 30.85

4 5

t tU

d d

p1

p2

CPU Utilization

p1

p2

RM

p1

p2

EDF

Page 53: 主講人:虞台文

Example: RM and EDF

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4

1 2

1 2

1.5 30.975

4 5

t tU

d d

p2

CPU Utilization

p1

p2

RM

p1

p2

EDF

p1

fail

Page 54: 主講人:虞台文

Operating Systems PrinciplesProcess Management and Coordination

Lecture 5:Process and Thread Scheduling

Priority Inversion

Page 55: 主講人:虞台文

Priority Inversion Problem

p1

P(mutex)

CS_1

V(mutex)

Program1

p2

Program2

p3

P(mutex)

CS_3

V(mutex)

Program3

• Assume priority order p1 > p2 > p3.• p1 and p3 share common resource.• p2 is independent of p1 and p3 .

Page 56: 主講人:虞台文

Priority Inversion Problem

• Assume priority order p1 > p2 > p3.• p1 and p3 share common resource.• p2 is independent of p1 and p3 .

p1

P(mutex)

CS_1

V(mutex)

Program1

p1

P(mutex)

CS_1

V(mutex)

Program1

p2

Program2

p2

Program2

p3

P(mutex)

CS_3

V(mutex)

Program3

p3

P(mutex)

CS_3

V(mutex)

Program3

p1

P(mutex)

CS_1

V(mutex)

Program1

p1

P(mutex)

CS_1

V(mutex)

Program1

p2

Program2

p2

Program2

p3

P(mutex)

CS_3

V(mutex)

Program3

p3

P(mutex)

CS_3

V(mutex)

Program3

Page 57: 主講人:虞台文

Priority Inversion Problem

• Assume priority order p1 > p2 > p3.• p1 and p3 share common resource.• p2 is independent of p1 and p3 .

p1

P(mutex)

CS_1

V(mutex)

Program1

p1

P(mutex)

CS_1

V(mutex)

Program1

p2

Program2

p2

Program2

p3

P(mutex)

CS_3

V(mutex)

Program3

p3

P(mutex)

CS_3

V(mutex)

Program3

p1

P(mutex)

CS_1

V(mutex)

Program1

p1

P(mutex)

CS_1

V(mutex)

Program1

p2

Program2

p2

Program2

p3

P(mutex)

CS_3

V(mutex)

Program3

p3

P(mutex)

CS_3

V(mutex)

Program3

Page 58: 主講人:虞台文

Priority Inversion Problem

• Assume priority order p1 > p2 > p3.• p1 and p3 share common resource.• p2 is independent of p1 and p3 .

p1

P(mutex)

CS_1

V(mutex)

Program1

p1

P(mutex)

CS_1

V(mutex)

Program1

p2

Program2

p2

Program2

p3

P(mutex)

CS_3

V(mutex)

Program3

p3

P(mutex)

CS_3

V(mutex)

Program3

p1

P(mutex)

CS_1

V(mutex)

Program1

p1

P(mutex)

CS_1

V(mutex)

Program1

p2

Program2

p2

Program2

p3

P(mutex)

CS_3

V(mutex)

Program3

p3

P(mutex)

CS_3

V(mutex)

Program3

(Unrelated) p2 may delay p1 indefinitely.

(Unrelated) p2 may delay p1 indefinitely.

Page 59: 主講人:虞台文

Solutions

1. Make CSs nonpreemptable Practical is CSs are short and few Unsatisfactory if high-priority process often found themselves

waiting for lower-priority process, especially unrelated ones.

2. Naïve “solution”: Always run CS at priority of highest process that shares the CS

Problem: p1 cannot preempt lower-priority process inside CS, even when it does not try to enter CS -- a different form of priority inversion.

3. Dynamic Priority Inheritance See Next

Page 60: 主講人:虞台文

Dynamic Priority Inheritance

p3 is in its CS

p1 attempts to enter its CS

p3 inherits p1’s (higher) priority for the duration of CS

Sha, Rajkumar, and Lehocsky 1990

• Assume priority order p1 > p2 > p3.• p1 and p3 share common resource.• p2 is independent of p1 and p3 .

p1

P(mutex)

CS_1

V(mutex)

Program1

p1

P(mutex)

CS_1

V(mutex)

Program1

p2

Program2

p2

Program2

p3

P(mutex)

CS_3

V(mutex)

Program3

p3

P(mutex)

CS_3

V(mutex)

Program3

p1

P(mutex)

CS_1

V(mutex)

Program1

p1

P(mutex)

CS_1

V(mutex)

Program1

p2

Program2

p2

Program2

p3

P(mutex)

CS_3

V(mutex)

Program3

p3

P(mutex)

CS_3

V(mutex)

Program3

Page 61: 主講人:虞台文

Dynamic Priority Inheritance

p3 is in its CS

p1 attempts to enter its CS

p3 inherits p1’s (higher) priority for the duration of CS

• Assume priority order p1 > p2 > p3.• p1 and p3 share common resource.• p2 is independent of p1 and p3 .

p1

P(mutex)

CS_1

V(mutex)

Program1

p1

P(mutex)

CS_1

V(mutex)

Program1

p2

Program2

p2

Program2

p3

P(mutex)

CS_3

V(mutex)

Program3

p3

P(mutex)

CS_3

V(mutex)

Program3

p1

P(mutex)

CS_1

V(mutex)

Program1

p1

P(mutex)

CS_1

V(mutex)

Program1

p2

Program2

p2

Program2

p3

P(mutex)

CS_3

V(mutex)

Program3

p3

P(mutex)

CS_3

V(mutex)

Program3

Page 62: 主講人:虞台文

Dynamic Priority Inheritance

p3 is in its CS

p1 attempts to enter its CS

p3 inherits p1’s (higher) priority for the duration of CS

• Assume priority order p1 > p2 > p3.• p1 and p3 share common resource.• p2 is independent of p1 and p3 .

p1

P(mutex)

CS_1

V(mutex)

Program1

p1

P(mutex)

CS_1

V(mutex)

Program1

p2

Program2

p2

Program2

p3

P(mutex)

CS_3

V(mutex)

Program3

p3

P(mutex)

CS_3

V(mutex)

Program3

p1

P(mutex)

CS_1

V(mutex)

Program1

p1

P(mutex)

CS_1

V(mutex)

Program1

p2

Program2

p2

Program2

p3

P(mutex)

CS_3

V(mutex)

Program3

p3

P(mutex)

CS_3

V(mutex)

Program3

Page 63: 主講人:虞台文

References Rensselaer Polytechnic Institute

http://www.cs.rpi.edu/academics/courses/fall04/os/c8/ University of east London Inside the Windows NT Scheduler, Part 1 Inside the Windows NT Scheduler, Part 2