Download Virtual Memory Address Translation and Page Management and more Study notes Operating Systems in PDF only on Docsity!
DETERMINISTIC
SCHEDULING
DETERMINISTIC
SCHEDULING
New Subtopic: Process SchedulingNew Subtopic: Process Scheduling
• Multiprogramming permits us to run multiple jobs
(processes)
• More precisely, it permits us to run those jobs that are
READY to run (i.e. are queued on the READY QUEUE to wait
to be executed):
- a process is “ready” if:
- memory has been allocated
- process is not waiting for I/O
- process is not waiting for some semaphore or some critical code
• When one job has to wait, the OS switches CPU to execute
some other READY job.
• Two general types of processes:
- CPU-bound: <def?>
- I/O-bound: <def?>
• Multiprogramming permits us to run multiple jobs
(processes)
• More precisely, it permits us to run those jobs that are
READY to run (i.e. are queued on the READY QUEUE to wait
to be executed):
- a process is “ready” if:
- memory has been allocated
- process is not waiting for I/O
- process is not waiting for some semaphore or some critical code
• When one job has to wait, the OS switches CPU to execute
some other READY job.
• Two general types of processes:
- CPU-bound: <def?>
- I/O-bound: <def?>
Process Scheduling (Generic)Process Scheduling (Generic)
SEMAPHORES
DISK I/O I/O
READY
executable processes swapped out
TAPE I/O I/O
IN: long term scheduler allows jobs in (determines degree of multiprogramming)
CPU
“short-term scheduler” or “medium-term scheduler” “dispatcher”
D
O
N
E
TERMS:
“inswapped”: job in memory “outswapped”: job’s executable image stored in OS-owned area of hard disk
TERMS:
“inswapped”: job in memory “outswapped”: job’s executable image stored in OS-owned area of hard disk
Multiprogramming ExampleMultiprogramming Example
R1 W
R2 W
R
JOB 1 JOB 1
R2 R2 R2 R
JOB 2 JOB 2
R1 W1 R1 R2 R2 R2 W2 R2 R
R2 R2 R2 W2 R2 R2 R1 W1 R
R1 R2 R1 R2 R2 W2 R2 R
R2 R2 R2 R1 R2 R2 R
Scheduling Possibility 1Scheduling Possibility 1
Scheduling Possibility 2Scheduling Possibility 2
Scheduling Possibility 3Scheduling Possibility 3
Scheduling Possibility 4Scheduling Possibility 4
The benefit of multiprogramming is to increase CPU utilization and job throughput!
The benefit of multiprogramming is to increase CPU utilization and job throughput!
Common Scheduling PoliciesCommon Scheduling Policies
- First-Come First Served (FCFS or FIFO)
- Shortest Job First
- Round Robin
- Priority Queues
- MultiLevel Feedback Queues
- First-Come First Served (FCFS or FIFO)
- Shortest Job First
- Round Robin
- Priority Queues
- MultiLevel Feedback Queues
First-Come First ServedFirst-Come First Served
- Schedule processes in the order in which they request
service
- example:
- Schedule processes in the order in which they request
service
- example:^ JOB#JOB#^ Request OrderRequest Order^ CPU timeCPU time 1 2 3
1 2 3
1 2 3
1 2 3
8 2 3
8 2 3
(^00 11 22 33 44 55 66 77 88 99 1010 1111 1212 )
Turnaround Time =Turnaround Time = (^) Response Time =Response Time =
Countermeasures?
Problems?
Countermeasures?
Problems?
Wait Time =Wait Time =
First-Come First ServedFirst-Come First Served
- Consider FCFS in dynamic situation: 1 CPU-bound Process,
many I/O bound
- What happens?
- CPU-bound grabs CPU and holds it
- during this time, I/O-bound processes finish I/O and move into READY
queue.
- while they wait, I/O devices are idle!
- CPU-bound finishes, and moves to an I/O request
- I/O-bound finish with CPU quickly
- CPU idle!
- CPU-bound finishes I/O, goes back to CPU hogging
- This is a “Convoy Effect”
- Consider FCFS in dynamic situation: 1 CPU-bound Process,
many I/O bound
- What happens?
- CPU-bound grabs CPU and holds it
- during this time, I/O-bound processes finish I/O and move into READY
queue.
- while they wait, I/O devices are idle!
- CPU-bound finishes, and moves to an I/O request
- I/O-bound finish with CPU quickly
- CPU idle!
- CPU-bound finishes I/O, goes back to CPU hogging
- This is a “Convoy Effect”
Shortest Job First (SJF)Shortest Job First (SJF)
- LCFS, FCFS do not take job times into account, so let’s have
CPU execute jobs with smallest CPU demands first
- LCFS, FCFS do not take job times into account, so let’s have
CPU execute jobs with smallest CPU demands first
JOB# JOB# Request OrderRequest Order^ CPU timeCPU time 1 2 3
1 2 3
1 2 3
1 2 3
8 2 3
8 2 3
(^00 11 22 33 44 55 66 77 88 99 1010 1111 1212 )
Turnaround Time =Turnaround Time = (^) Response Time =Response Time =
Countermeasures?
Problems?
Countermeasures?
Problems?
Wait Time =Wait Time =
Round Robin SchedulingRound Robin Scheduling
- A small time quantum “q” is defined
- CPU is allocated to a job “i”
- If job finishes service before quantum expires,
then leave the CPU Q
else
go to end of CPU Q when quantum expires.
- Note: q --->^ ∞∞∞∞, RR becomes FCFS
q ---> 0, RR becomes “processor sharing”; each of n
jobs sees CPU executing at 1/n speed
- A small time quantum “q” is defined
- CPU is allocated to a job “i”
- If job finishes service before quantum expires,
then leave the CPU Q
else
go to end of CPU Q when quantum expires.
- Note: q --->^ ∞∞∞∞, RR becomes FCFS
q ---> 0, RR becomes “processor sharing”; each of n
jobs sees CPU executing at 1/n speed
Round RobinRound Robin
JOB# JOB# CPU timeCPU time 1 2 3
1 2 3
8 2 3
8 2 3
Turnaround Time =Turnaround Time = (^) Response Time =Response Time =
Countermeasures?
Problems?
Countermeasures?
Problems?
(^1 2 3 1 3 1 )
q= 2q= 2
Wait Time =Wait Time =
(^00 11 22 33 44 55 66 77 88 99 1010 1111 1212 )
Shortest Remaining TimeShortest Remaining Time
- Preemptive version of SPN
- CHOOSE JOB WITH SHORTEST EXPECTED REMAINING PROCESSING
TIME
- Can preempt whenever a new (shorter) job arrives
- no bias toward long-term jobs getting “better” response
times (unlike FCFS)
- Better turnaround (wait time) compared with SPN
- Preemptive version of SPN
- CHOOSE JOB WITH SHORTEST EXPECTED REMAINING PROCESSING
TIME
- Can preempt whenever a new (shorter) job arrives
- no bias toward long-term jobs getting “better” response
times (unlike FCFS)
- Better turnaround (wait time) compared with SPN
Priority Queues (multi-level queues)Priority Queues (multi-level queues)
- • Maintain multiple queues for different “priority-class” jobs.Maintain multiple queues for different “priority-class” jobs.
CPU
Faculty
System
Students
HIHI
LOWLOW
Jobs from a lower-priority queue are served
IFF
there are no jobs in a higher-level queue
Jobs from a lower-priority queue are served
IFF
there are no jobs in a higher-level queue
Race ConditionsRace Conditions
- Asynchronous concurrent processes/threads can access
shared data in an ARBITRARILY interleaved manner!
- Asynchronous concurrent processes/threads can access
shared data in an ARBITRARILY interleaved manner!
Example:Example:
Q.Front
WW XX
W.next X.next= nil
Two Processes:Two Processes:
Insert(Y, Q)
1. Get last Q element
2. set last element’s ptr to Y
Delete_Last(Q)
A. Get ptr to last Q element
B. set ptr to NIL
Statement Interleavings?Statement Interleavings?
Avoiding Race ConditionsAvoiding Race Conditions
- When can two statements be executed concurrently
(arbitrarily interleaved) and produce the same results? (and
avoid race conditions)
- When can two statements be executed concurrently
(arbitrarily interleaved) and produce the same results? (and
avoid race conditions)
- • Def: R(S) = set of variables referenced by “S” (Read Set)Def: R(S) = set of variables referenced by “S” (Read Set)
- • Def: W(S) = set of variables updated by “S” (Write Set)Def: W(S) = set of variables updated by “S” (Write Set)
- Example:
- S1: a = b - c; R(S1) = {b,c} W(S1) = {a}
- S2: d = a * 7;^ R(S2) = {a}^ W(S2) = {d}
- Example:
- S1: a = b - c;^ R(S1) = {b,c}^ W(S1) = {a}
- S2: d = a * 7;^ R(S2) = {a}^ W(S2) = {d}
- Answer: “Bernstein’s Conditions”
- R(S1)^ ∩∩∩∩^ W(S2) =^ ∅∅∅∅
- R(S2)^ ∩∩∩∩^ W(S1) =^ ∅∅∅∅
- W(S1)^ ∩∩∩∩^ W(S2) =^ ∅∅∅∅
- Answer: “Bernstein’s Conditions”
- R(S1)^ ∩∩∩∩^ W(S2) =^ ∅∅∅∅
- R(S2) ∩∩∩∩^ W(S1) = ∅∅∅∅
- W(S1)^ ∩∩∩∩^ W(S2) =^ ∅∅∅∅
Critical SectionsCritical Sections
- Defn: A section of code which modifies/accesses data
structures shared among several processes
- Problem: arbitrary interleaving of executions may result in
undefined or unexpected results
- shared code that is immune to critical section problem is called “re-entrant,”
or “thread-safe”
- DISCUSS: How can we solve “critical section problem?”
- Defn: A section of code which modifies/accesses data
structures shared among several processes
- Problem: arbitrary interleaving of executions may result in
undefined or unexpected results
- shared code that is immune to critical section problem is called “re-entrant,”
or “thread-safe”
- DISCUSS: How can we solve “critical section problem?”
Solving Race ConditionsSolving Race Conditions
- Any proposed method for preventing race conditions must
enforce the following criteria:
- MUTUAL EXCLUSION: only 1 process/thread in critical section at a time
- PROGRESS: processes/threads in NONcritical portions of code cannot
prevent others from entering a critical section
- BOUNDED WAITING: it must be impossible for a process/thread to wait
forever to enter its critical section.
- NB: solutions should not make assumptions about process
execution speeds.
- Any proposed method for preventing race conditions must
enforce the following criteria:
- MUTUAL EXCLUSION: only 1 process/thread in critical section at a time
- PROGRESS: processes/threads in NONcritical portions of code cannot
prevent others from entering a critical section
- BOUNDED WAITING: it must be impossible for a process/thread to wait
forever to enter its critical section.
- NB: solutions should not make assumptions about process
execution speeds.
“Main” Method“Main” Method
public class FishingProblem { /Globally shared variables here (see later slides) /
public static void main( ) { Fisherman first = new Fisherman(0); Fisherman second = new Fisherman(1); first.start( ); second.start( ); } }
Attempt 1 Attempt 1
Problems?Problems?
public void run( ) { while (true) { while (turn != 0) Thread.yield ( ); go_on_ice( ); turn = 1; work_off_ice( ); } }
public volatile int turn = 1;public volatile int turn = 1;
public void run( ) { while (true) { while (turn != 1) Thread.yield ( ); go_on_ice( ); turn = 0; work_off_ice( ); } }
Scenario for attempts 2,3,4Scenario for attempts 2,3,
DANGER!
Thin Ice
ICE2 ICE
Attempt 2Attempt 2
Problems?Problems?
public void run( ) { while (true) { while (ice[1] == true) Thread.yield ( ); ice[0] = true; go_on_ice( ); ice[0] = false; work_off_ice( ); } }
public volatile boolean[] ice = new boolean[2];
ice[ 0 ] = false; ice[ 1 ] = false;
public volatile boolean[] ice = new boolean[2];
ice[ 0 ] = false; ice[ 1 ] = false;
public void run( ) { while (true) { while (ice[0] == true) Thread.yield ( ); ice[1] = true; go_on_ice( ); ice[1] = false; work_off_ice( ); } }
Attempt 5 ScenarioAttempt 5 Scenario
DANGER!
Thin Ice
ICE2 ICE
Turn
Dekker’s SolutionDekker’s Solution
public volatile boolean[] ice = new boolean[2]; public volatile int turn = 0; /* or 1 */
ice[ 0 ] = false; ice[ 1 ] = false;
public volatile boolean[] ice = new boolean[2]; public volatile int turn = 0; /* or 1 */
ice[ 0 ] = false; ice[ 1 ] = false;
Problems?Problems?
public void run( ) { while (true) { ice[0] = true; turn = 1; while ((ice[1] == true) && (turn == 1)) Thread.yield ( ); go_on_ice( ); ice[0] = false; work_off_ice( ); } }
public void run( ) { while (true) { ice[1] = true; turn = 0; while ((ice[0] == true) && (turn == 0)) Thread.yield ( ); go_on_ice( ); ice[1] = false; work_off_ice( ); } }
Alternate Solutions to Race ConditionsAlternate Solutions to Race Conditions
- Disabling Interrupts?
- Test-and-Set Instruction
- single, atomic, uninterruptable instruction
- test & possibly modify a variable in a single step
- eg TSL on IBM 360:
- test-and-set-lock(lock) returns true indicating the lock has already been
set by some other process,
- OR returns false, indicating the lock had not been set, BUT NOW IS SET
BY CURRENT PROCESS/THREAD.
- Example use:
- Disabling Interrupts?
- Yes, but....
- Test-and-Set Instruction
- single, atomic, uninterruptable instruction
- test & possibly modify a variable in a single step
- eg TSL on IBM 360:
- test-and-set-lock(lock) returns true indicating the lock has already been
set by some other process,
- OR returns false, indicating the lock had not been set, BUT NOW IS SET
BY CURRENT PROCESS/THREAD.
REPEAT
WHILE (test-and-set(lock) == true)
do /*nothing */
run_critical_section;
lock := false;
FOREVER;
“Blocking” Solutions“Blocking” Solutions
- Avoid busy waiting by getting OS to “block,” or stop and
swap out, any processes/threads that are just waiting for
their critical section
- Semaphores to the “rescue!” (Dijkstra, 1966)
- Defn: a Semaphore is an integer variable that can only be
accessed through the ATOMIC operations DOWN and UP:
- DOWN(S): {if (S <= 0) BLOCK;
S := S - 1;}
WAKEUP one process (if any) blocked on S;}
- “General Semaphore” (counting semaphore) - S can take on
any value
- Avoid busy waiting by getting OS to “block,” or stop and
swap out, any processes/threads that are just waiting for
their critical section
- Semaphores to the “rescue!” (Dijkstra, 1966)
- Defn: a Semaphore is an integer variable that can only be
accessed through the ATOMIC operations DOWN and UP:
- DOWN(S): {if (S <= 0) BLOCK;
S := S - 1;}
WAKEUP one process (if any) blocked on S;}
- “General Semaphore” (counting semaphore) - S can take on
any value
Using Semaphores (Mutual Exclusion)Using Semaphores (Mutual Exclusion)
REPEAT
_____________
critical_section;
_____________
noncritical_section;
FOREVER;
Solving the Critical Section Problem:Solving the Critical Section Problem:
REPEAT
_x.down( )____
critical_section;
___x.up( )____
noncritical_section;
FOREVER;
Using Semaphores(Synchronization)Using Semaphores(Synchronization)
- Suppose statement S1 must be executed by process P
BEFORE statement S2 is executed by P2?
- Suppose statement S1 must be executed by process P
BEFORE statement S2 is executed by P2?
P1: P2:
BEGIN
A1;
A2;
______
S1;
______
A3;
END;
BEGIN
B1;
________
S2;
________
B2;
END;
BEGIN
A1;
A2;
______
S1;
x.up( );_
A3;
END;
BEGIN
B1;
x.down( );
S2;
________
B2;
END;
Implementing SemaphoresImplementing Semaphores
- Atomicity of semaphores is crucial to their use. How might
we guarantee this in their implementation?
- Solution #1:
- Atomicity of semaphores is crucial to their use. How might
we guarantee this in their implementation?
Problems?:Problems?:
- • Solution #2:Solution #2:
Java and SemaphoresJava and Semaphores
public class Semaphore {
private int s;
public void Semaphore (int initial_val) {
s = initial_val;
public void Semaphore ( ) {
s = 1;
public synchronized void down( ) {
if (s < = 0)
wait( );
s--;
public synchronized void up( ) {
s++;
notify( );