NETW 3005 Scheduling Reading For this lecture you

  • Slides: 61
Download presentation
NETW 3005 Scheduling

NETW 3005 Scheduling

Reading • For this lecture, you should have read Chapter 5 (Sections 1 -3,

Reading • For this lecture, you should have read Chapter 5 (Sections 1 -3, 7). NETW 3005 (Operating Systems) Lecture 04 - Scheduling 2

Last Lecture • Cooperating processes and datasharing NETW 3005 (Operating Systems) Lecture 04 -

Last Lecture • Cooperating processes and datasharing NETW 3005 (Operating Systems) Lecture 04 - Scheduling 3

This lecture • Criteria for scheduling algorithms • Some scheduling algorithms: – first-come-first-served, –

This lecture • Criteria for scheduling algorithms • Some scheduling algorithms: – first-come-first-served, – shortest-job-first, – priority scheduling, – round-robin scheduling, – multilevel queue scheduling. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 4

Important Notice • There is a TUTORIAL TEST on CPU scheduling in tutorial 10

Important Notice • There is a TUTORIAL TEST on CPU scheduling in tutorial 10 B (8 -9 May). • It’s worth 9. 5%. • The questions in Tutorial 10 A are practice for this exam. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 5

CPU scheduling: a recap ready queue I/O I/O device queue CPU I/O request fork

CPU scheduling: a recap ready queue I/O I/O device queue CPU I/O request fork interrupt mechanism NETW 3005 (Operating Systems) Lecture 04 - Scheduling 6

Context switching Process P 0 Operating System executing Process P 1 idle Interrupt or

Context switching Process P 0 Operating System executing Process P 1 idle Interrupt or system call save state into PCB 0 reload state from PCB 1 idle executing Interrupt or system call save state into PCB 1 reload state from PCB 0 executing NETW 3005 (Operating Systems) idle Lecture 04 - Scheduling 7

Scheduler and dispatcher • The scheduler decides which process to give the CPU to

Scheduler and dispatcher • The scheduler decides which process to give the CPU to next, and when to give it. • Its decisions are carried out by the dispatcher. • Dispatching involves: – Switching context – Switching to ‘user mode’ – Jumping to the proper location in the new process. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 8

Why do we want a scheduler? (1) • Simple answer – to keep the

Why do we want a scheduler? (1) • Simple answer – to keep the CPU busy. • This means removing processes from the CPU while they’re waiting. • If processes never had to wait, then scheduling wouldn’t increase CPU utilisation. • However, it’s a fact that processes tend to exhibit a CPU burst cycle. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 9

How long is a CPU burst? • Simple answer – it varies depending on

How long is a CPU burst? • Simple answer – it varies depending on machine and job mix. • Typically, majority are around 4 – 6 ms. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 10

Why do we want a scheduler? (2) • Another reason for having a scheduler

Why do we want a scheduler? (2) • Another reason for having a scheduler is so that processes don’t have to spend too much time waiting for the CPU. • Even if the CPU is always busy, executing processes in different orders can change the average amount of time a process spends queuing for the CPU. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 11

P 1 NETW 3005 (Operating Systems) P 2 P 3 Lecture 04 - Scheduling

P 1 NETW 3005 (Operating Systems) P 2 P 3 Lecture 04 - Scheduling P 4 12

Why do we want a scheduler? (3) • One question is how long a

Why do we want a scheduler? (3) • One question is how long a process spends waiting for the CPU in total. • A different question is how long, on average, it waits in between visits to the CPU (important for interactive processes. ) NETW 3005 (Operating Systems) Lecture 04 - Scheduling 13

Metrics for scheduling algorithms(1) • CPU utilisation: the percentage of time that the CPU

Metrics for scheduling algorithms(1) • CPU utilisation: the percentage of time that the CPU is usefully busy. • Throughput: the number of processes that are completed per time unit. • Turnaround time (per process): the elapsed time between submission time and completion time. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 14

Metrics for scheduling algorithms(2) • Waiting time (per process): the total amount of time

Metrics for scheduling algorithms(2) • Waiting time (per process): the total amount of time the process spends waiting for the CPU. • Response time (per process): the average time from the submission of a request to a process until the first response is produced. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 15

The ready queue • Remember: there are two kinds of waiting: – Waiting for

The ready queue • Remember: there are two kinds of waiting: – Waiting for the CPU (in the ready queue) – Waiting for an I/O device (in a device queue). • Don’t be confused when you hear about processes waiting in the ready queue! NETW 3005 (Operating Systems) Lecture 04 - Scheduling 16

Preemption • In a non-preemptive scheduling system, a process keeps the CPU until it

Preemption • In a non-preemptive scheduling system, a process keeps the CPU until it switches to the waiting state (pending I/O completion or termination of a child process), or terminates. • In a preemptive scheduling system, the currently executing process can be removed from the CPU at any time. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 17

Interrupts • A possible context switch is triggered – when a new process arrives

Interrupts • A possible context switch is triggered – when a new process arrives in the ready queue; – when a waiting process finishes waiting; – when a certain amount of time has elapsed. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 18

Problems • What if a process is preempted while a system call is being

Problems • What if a process is preempted while a system call is being executed? Kernel data (e. g. I/O queues) might be left in an inconsistent state. • Earlier versions of UNIX dealt with this problem by waiting until system calls were completed before switching context. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 19

Preemptive systems • MS Windows 3. 1 and below were nonpreemptive. • Windows 95,

Preemptive systems • MS Windows 3. 1 and below were nonpreemptive. • Windows 95, NT, XP etc are preemptive. • Linux is fully preemptive as of 2. 6. • Mac. OS 9 and earlier versions weren’t (although they claimed to be). Mac. OS X is. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 20

Gantt charts • The operation of a scheduling algorithm is commonly represented in a

Gantt charts • The operation of a scheduling algorithm is commonly represented in a Gantt chart. Process P 1 P 2 P 3 Arrival time Burst time t 1(= 0). b 1 t 2 b 2 t 3 b 3 (N. B. We’re just looking at the initial CPU burst for each process. ) NETW 3005 (Operating Systems) Lecture 04 - Scheduling 21

First-come-first-served scheduling • The simplest method is to execute the processes in the ready

First-come-first-served scheduling • The simplest method is to execute the processes in the ready queue on a firstcome-first-served (FCFS) basis. • When a process becomes ready, it is put at the tail of the queue. • When the currently executing process terminates, or waits for I/O, the process at the front of the queue is selected next. • This algorithm is non-preemptive. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 22

Gantt Charts for FCFS Process P 1 P 2 P 3 Arrival time 0

Gantt Charts for FCFS Process P 1 P 2 P 3 Arrival time 0 1 2 P 1 P 2 24 P 1 P 2 P 3 NETW 3005 (Operating Systems) Burst time 24 3 3 Lecture 04 - Scheduling P 3 27 30 23

Waiting times? • • P 1? P 2? P 3? Average? NETW 3005 (Operating

Waiting times? • • P 1? P 2? P 3? Average? NETW 3005 (Operating Systems) Lecture 04 - Scheduling 24

Waiting times? • • P 1? 0 ms P 2? P 3? Average? NETW

Waiting times? • • P 1? 0 ms P 2? P 3? Average? NETW 3005 (Operating Systems) Lecture 04 - Scheduling 25

Waiting times? • • P 1? 0 ms P 2? 24 – 1 =

Waiting times? • • P 1? 0 ms P 2? 24 – 1 = 23 ms P 3? Average? NETW 3005 (Operating Systems) Lecture 04 - Scheduling 26

Waiting times? • • P 1? 0 ms P 2? 24 – 1 =

Waiting times? • • P 1? 0 ms P 2? 24 – 1 = 23 ms P 3? 27 – 2 = 25 ms Average? NETW 3005 (Operating Systems) Lecture 04 - Scheduling 27

Waiting times? • • P 1? 0 ms P 2? 24 – 1 =

Waiting times? • • P 1? 0 ms P 2? 24 – 1 = 23 ms P 3? 27 – 2 = 25 ms Average? (0 + 23 + 25) / 3 = 16 ms NETW 3005 (Operating Systems) Lecture 04 - Scheduling 28

Gantt Charts for FCFS (2) Process P 1 P 2 P 3 P 2

Gantt Charts for FCFS (2) Process P 1 P 2 P 3 P 2 Arrival time 2 0 1 P 3 3 Burst time 24 3 3 P 1 6 30 P 1 P 2 P 3 NETW 3005 (Operating Systems) Lecture 04 - Scheduling 29

Waiting times? • • P 1? P 2? P 3? Average? NETW 3005 (Operating

Waiting times? • • P 1? P 2? P 3? Average? NETW 3005 (Operating Systems) Lecture 04 - Scheduling 30

Waiting times? • • P 1? 6 – 2 = 4 ms P 2?

Waiting times? • • P 1? 6 – 2 = 4 ms P 2? P 3? Average? NETW 3005 (Operating Systems) Lecture 04 - Scheduling 31

Waiting times? • • P 1? 6 – 2 = 4 ms P 2?

Waiting times? • • P 1? 6 – 2 = 4 ms P 2? 0 ms P 3? Average? NETW 3005 (Operating Systems) Lecture 04 - Scheduling 32

Waiting times? • • P 1? 6 – 2 = 4 ms P 2?

Waiting times? • • P 1? 6 – 2 = 4 ms P 2? 0 ms P 3? 3 – 1 = 2 ms Average? NETW 3005 (Operating Systems) Lecture 04 - Scheduling 33

Waiting times? • • P 1? 6 – 2 = 4 ms P 2?

Waiting times? • • P 1? 6 – 2 = 4 ms P 2? 0 ms P 3? 3 – 1 = 2 ms Average? (4 + 0 + 2) / 3 = 2 ms NETW 3005 (Operating Systems) Lecture 04 - Scheduling 34

Advantages of FCFS • Easy to implement. • Easy to understand. NETW 3005 (Operating

Advantages of FCFS • Easy to implement. • Easy to understand. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 35

Disadvantages of FCFS • Waiting time not likely to be minimal. • Convoy effect:

Disadvantages of FCFS • Waiting time not likely to be minimal. • Convoy effect: lots of small processes can get stuck behind one big one. • Poor response time — bad for timesharing systems). • Q: could throughput (no of processes passing through the system per unit time) be improved? NETW 3005 (Operating Systems) Lecture 04 - Scheduling 36

Shortest-job-first scheduling • If we knew in advance which process on the list had

Shortest-job-first scheduling • If we knew in advance which process on the list had the shortest burst time, we could choose to execute that process next. • Processes with equal burst times are executed in FCFS order. • This method is called shortest-job-first (SJF) scheduling. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 37

Example P 4 3 NETW 3005 (Operating Systems) Process P 1 P 2 P

Example P 4 3 NETW 3005 (Operating Systems) Process P 1 P 2 P 3 P 4 Burst time 6 8 7 3 P 1 P 3 9 Lecture 04 - Scheduling P 2 16 24 38

Advantages of SJF • Provably optimal average waiting time. NETW 3005 (Operating Systems) Lecture

Advantages of SJF • Provably optimal average waiting time. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 39

Disadvantages of SJF • You never know in advance what the length of the

Disadvantages of SJF • You never know in advance what the length of the next CPU burst is going to be. • Possibility of long processes never getting executed? NETW 3005 (Operating Systems) Lecture 04 - Scheduling 40

Predicting CPU burst length • It’s likely to be similar in length to the

Predicting CPU burst length • It’s likely to be similar in length to the previous CPU bursts. • A commonly-used formula: the exponential average CPU burst length. τn+1 = α tn + (1 − α)τn τn: the predicted length of CPU burst n. tn: the actual length of CPU burst n. α: a value between 0 and 1. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 41

Preemption and SJF scheduling • Scenario: – A process P 1 is currently executing.

Preemption and SJF scheduling • Scenario: – A process P 1 is currently executing. – A new process P 2 arrives before P 1 is finished. – P 2’s (predicted) burst time is shorter than the (expected) remaining burst time of P 1. • Non-preemptive SJF: P 1 keeps the CPU. • Preemptive SJF: P 2 takes the CPU. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 42

Tiny example (with preemption): Process P 1 P 2 P 1 0 Arrival time

Tiny example (with preemption): Process P 1 P 2 P 1 0 Arrival time 0 1 P 2 1 NETW 3005 (Operating Systems) Burst time 8 4 P 1 5 Lecture 04 - Scheduling 12 43

Priority scheduling • In priority scheduling: – each process is allocated a priority when

Priority scheduling • In priority scheduling: – each process is allocated a priority when it arrives; – the CPU is allocated to the process with highest priority. • Priorities are represented by numbers, with low numbers being highest priority. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 44

Questions • Can priority scheduling be preemptive? NETW 3005 (Operating Systems) Lecture 04 -

Questions • Can priority scheduling be preemptive? NETW 3005 (Operating Systems) Lecture 04 - Scheduling 45

Questions • Can priority scheduling be preemptive? Yes, no reason why not. NETW 3005

Questions • Can priority scheduling be preemptive? Yes, no reason why not. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 46

Questions • Can priority scheduling be preemptive? Yes, no reason why not. • What’s

Questions • Can priority scheduling be preemptive? Yes, no reason why not. • What’s the relation of SJF scheduling to priority scheduling? NETW 3005 (Operating Systems) Lecture 04 - Scheduling 47

Questions • Can priority scheduling be preemptive? Yes, no reason why not. • What’s

Questions • Can priority scheduling be preemptive? Yes, no reason why not. • What’s the relation of SJF scheduling to priority scheduling? SJF is a type of priority scheduling, specifically one where the priority of a process is set to be the estimated next CPU burst time. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 48

Starvation and aging • Starvation occurs when a process waits indefinitely to be allocated

Starvation and aging • Starvation occurs when a process waits indefinitely to be allocated the CPU. • Priority scheduling algorithms are susceptible to starvation. – Imagine a process P 1 is waiting for the CPU, and a stream of higher-priority processes is arriving. – If these processes arrive sufficiently fast, P 1 will never get a chance to execute. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 49

Starvation and aging • A solution to the starvation problem is to increase the

Starvation and aging • A solution to the starvation problem is to increase the priority of processes as a function of how long they’ve been waiting for the CPU. (Called aging. ) NETW 3005 (Operating Systems) Lecture 04 - Scheduling 50

Round-Robin scheduling • Round-robin (RR) scheduling is designed for time-sharing systems. • A small

Round-Robin scheduling • Round-robin (RR) scheduling is designed for time-sharing systems. • A small unit of time (time quantum) is defined. • The ready queue is treated as a circular list. • The CPU scheduler goes round the ready queue, allocating the CPU to each process for a time interval of up to 1 time quantum. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 51

RR Example Process P 1 P 2 P 3 Arrival time 0 1 4

RR Example Process P 1 P 2 P 3 Arrival time 0 1 4 Burst time 8 4 4 Assume that the time quantum is set to 3 NETW 3005 (Operating Systems) Lecture 04 - Scheduling 52

RR Example (continued) P 2=1 P 1=5 P 1 0 P 2 3 NETW

RR Example (continued) P 2=1 P 1=5 P 1 0 P 2 3 NETW 3005 (Operating Systems) P 3=1 P 1=2 P 1 6 P 2=0 P 3 9 P 1=0 P 3=0 P 2 P 1 P 3 12 13 15 16 Lecture 04 - Scheduling 53

Changing the Time Quantum • If the time quantum is set to be infinitely

Changing the Time Quantum • If the time quantum is set to be infinitely large, RR scheduling reduces to FCFS scheduling. • If the time quantum is set to be very small, we can talk of processor sharing. • If we make the time quantum the same as the context switch time, we’ll spend half our time on context switching! NETW 3005 (Operating Systems) Lecture 04 - Scheduling 54

RR: advantages and disadvantages • If the time quantum is big, we get the

RR: advantages and disadvantages • If the time quantum is big, we get the advantages/disadvantages of FCFS scheduling. • If it’s very small, we get faster response time but slower throughput. • Why? NETW 3005 (Operating Systems) Lecture 04 - Scheduling 55

RR: advantages and disadvantages • If the time quantum is big, we get the

RR: advantages and disadvantages • If the time quantum is big, we get the advantages/disadvantages of FCFS scheduling. • If it’s very small, we get faster response time but slower throughput. • Why? Because there’s more time spent context-switching. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 56

RR: advantages and disadvantages • Even if you ignore context-switch time, turnaround time goes

RR: advantages and disadvantages • Even if you ignore context-switch time, turnaround time goes down if most processes complete within a single quantum. • For instance, say there are 3 processes with a next CPU burst of 10. – If the quantum size is 1, then average turn-around time is 29. – If quantum size is 10, then they finish in the next CPU burst, and the average turnaround time is 20. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 57

Multilevel queue scheduling • Let’s say you have two groups of processes: – interactive

Multilevel queue scheduling • Let’s say you have two groups of processes: – interactive processes; – batch processes. • What you really want is to run different scheduling algorithms for the two groups. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 58

Multilevel queue scheduling • Split the ready queue into a number of different queues,

Multilevel queue scheduling • Split the ready queue into a number of different queues, each with its own scheduling algorithm. • Implement a scheduling algorithm to decide which queue is allocated next. • Preemptive priority scheduling is often used. NETW 3005 (Operating Systems) Lecture 04 - Scheduling 59

Exercise The Linux command nice changes priority for a process. Read the man page

Exercise The Linux command nice changes priority for a process. Read the man page for nice. Using any Linux program, try the following: /bin/nice -n 19 <program> /bin/nice -n 10 <program> Is there a difference in speed? (You may want to use the time command as well. ) NETW 3005 (Operating Systems) Lecture 04 - Scheduling 60

Next Lecture Process Scheduling Chapter 6 (Sections 1– 7).

Next Lecture Process Scheduling Chapter 6 (Sections 1– 7).