module 6: cpu scheduling - authorauthor.uthm.edu.my/uthm/www/content/lessons/5845/131021 -...

7
1 CHAPTER 5 CPU SCHEDULING Contents Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Basic Concepts Maximum CPU utilization obtained with multiprogramming CPUI/O Burst Cycle; Process execution consists of a cycle of CPU execution and I/O wait. CPU burst distribution Alternating Sequence of CPU And I/O Bursts Histogram of CPU-burst Times Diagram of Process State

Upload: doantram

Post on 16-Apr-2018

244 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Module 6: CPU Scheduling - AUTHORauthor.uthm.edu.my/uthm/www/content/lessons/5845/131021 - Chapte… · CPU SCHEDULING Contents ... Shortest-Remaining-Time-First (SRTF). 7 • SJF

1

CHAPTER 5

CPU SCHEDULING

Contents

• Basic Concepts

• Scheduling Criteria

• Scheduling Algorithms

• Multiple-Processor Scheduling

• Real-Time Scheduling

Basic Concepts

• Maximum CPU utilization obtained with multiprogramming

• CPU–I/O Burst Cycle;

– Process execution consists of a cycle of CPU execution and I/O wait.

• CPU burst distribution

Alternating Sequence of CPU And I/O Bursts

Histogram of CPU-burst Times Diagram of Process State

Page 2: Module 6: CPU Scheduling - AUTHORauthor.uthm.edu.my/uthm/www/content/lessons/5845/131021 - Chapte… · CPU SCHEDULING Contents ... Shortest-Remaining-Time-First (SRTF). 7 • SJF

2

CPU Scheduler (Short term scheduler)

• Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them.

• CPU scheduling decisions may take place when a process:

1. Switches from running to waiting state.

2. Switches from running to ready state.

3. Switches from waiting to ready.

4. Terminates.

CPU Scheduler

• Scheduling under 1 and 4 nonpreemptive

– Once the CPU has been allocated to a process, the process keeps the CPU until it releases the CPU either by terminating or by switching to the waiting state.

– Used by: MS Windows 3.1 & Apple Macintosh os

• All other scheduling preemptive.

Dispatcher • Dispatcher module gives control of the CPU to the process

selected by the short-term scheduler; this involves:

– switching context

– switching to user mode

– jumping to the proper location in the user program to restart that program

• Dispatch latency time it takes for the dispatcher to stop one process and start another running.

Scheduling Criteria 1. CPU utilization – keep the CPU as busy as possible

2. Throughput – number of processes that complete their execution per time unit

3. Turnaround time – amount of time to execute a particular process Sum of the periods spent for:

1. Waiting to get into memory

2. Waiting in the ready queue

3. Execute on the CPU

4. Doing I/O

4. Waiting time – amount of time a process has been waiting in the ready queue

5. Response time – amount of time it takes from when a request was submitted until the first response is produced, not output (for time-sharing environment)

Optimization Criteria • Max;

– CPU utilization

– throughput

• Min;

– turnaround time

– waiting time

– response time

• Most cases optimize the average measure

• Which criteria should be considered to guarantee that all users get good services???

Scheduling Algorithms

• First Come, First-Served (FCFS) Scheduling

• Shortest-Job-First (SJF) Scheduling

• Priority Scheduling

• Round-Robin (RR) Scheduling

• Multilevel Queue Scheduling

• Multilevel Feedback Queue Scheduling

Page 3: Module 6: CPU Scheduling - AUTHORauthor.uthm.edu.my/uthm/www/content/lessons/5845/131021 - Chapte… · CPU SCHEDULING Contents ... Shortest-Remaining-Time-First (SRTF). 7 • SJF

3

FCFS Scheduling

Process Burst Time (ms)

P1 24

P2 3

P3 3

• Suppose that the processes arrive in the order P1 , P2 , P3

The Gantt Chart for the schedule is:

• Waiting time for P1 = 0ms; P2 = 24ms; P3 = 27ms

• Average waiting time: (0 + 24 + 27)/3 = 17ms

P1 P2 P3

24 27 30 0

FCFS Scheduling What will happened if the processes arrive in the order P2 , P3 , P1

• The Gantt chart for the schedule is:

• Waiting time for P1 = 6ms; P2 = 0ms; P3 = 3ms • Average waiting time: (6 + 0 + 3)/3 = 3ms • Much better than previous case. • Convoy effect short process behind long process All other processes wait for the one big process to get off

the CPU (this effect results in lower CPU and device utilization)

P1 P3 P2

6 3 30 0

Shortest-Job-First (SJF) Scheduling

• Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time.

• Two schemes:

– nonpreemptive – once CPU given to the process it cannot be preempted until completes its CPU burst.

– preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is known as Shortest-Remaining-Time-First (SRTF).

• SJF is optimal – gives minimum average waiting time for a given set of processes.

Example of Non-Preemptive SJF

Process Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

• SJF (non-preemptive)

• Average waiting time = (0 + 6 + 3 + 7)/4 4

P1 P3 P2

7 3 16 0

P4

8 12

Example of Preemptive SJF (SRTF)

Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 • SJF (preemptive)

• Average waiting time = (9 + 1 + 0 +2)/4 3

P1 P3 P2

4 2 11 0

P4

5 7

P2 P1

16

Determining Length of Next CPU Burst

• Can only estimate the length.

• Can be done by using the length of previous CPU bursts, using exponential averaging.

:Define 4.

10 , 3.

burst CPU next the for value predicted 2.

burst CPU of lenght actual 1.

1n

thn nt

.t nnn 11

Page 4: Module 6: CPU Scheduling - AUTHORauthor.uthm.edu.my/uthm/www/content/lessons/5845/131021 - Chapte… · CPU SCHEDULING Contents ... Shortest-Remaining-Time-First (SRTF). 7 • SJF

4

Prediction of the Length of the Next CPU Burst Examples of Exponential Averaging

• =0

– n+1 = n

– Recent history does not count.

• =1

– n+1 = tn

– Only the actual last CPU burst counts.

• If we expand the formula, we get:

n+1 = tn+(1 - ) tn -1 + …

+(1 - )j tn -1 + …

+(1 - )n=1 tn 0

• Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor.

Priority Scheduling • A priority number (integer) is associated with each process

• The CPU is allocated to the process with the highest priority (smallest integer highest priority).

– Preemptive

– Nonpreemptive

• SJF is a priority scheduling where priority is the predicted next CPU burst time (the larger the CPU burst, the lower the priority)

• Problem Starvation – low priority processes may never execute.

• Solution Aging – as time progresses increase the priority of the process.

• Equal priority processes are scheduled in FCFS order.

Priority Scheduling

PROCESS BURST TIME PRIORITY

P1 10 3

P2 1 1

P3 2 4

P4 1 5

P5 5 2

Priority Scheduling

P2

0 6

• waiting time; • P1 = 6 ms • P2 = 0 ms • P3 = 16 ms • P4 = 18 ms • P5 = 1 ms

• Average waiting time; = ( 6 + 0 + 16 + 18 + 1 )/ 5 = 8.2 ms

P3 P1 P5 P4

1 18 16 19

Round Robin (RR)

• Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue.

• If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units.

• Performance

– q large FIFO

– q small q must be large with respect to context switch, otherwise overhead is too high.

Page 5: Module 6: CPU Scheduling - AUTHORauthor.uthm.edu.my/uthm/www/content/lessons/5845/131021 - Chapte… · CPU SCHEDULING Contents ... Shortest-Remaining-Time-First (SRTF). 7 • SJF

5

RR – Example (1)

P1

0 10 7 18

• waiting time; • P1 = (0 + (10-4)) = 6 ms • P2 = 4 ms • P3 = 7 ms

• Average waiting time; = ( 6 + 4 + 7 )/ 3 = 5.66 ms

P1 P3 P2 P1 P1 P1 P1

14 4 26 22 30

Process Burst Time (ms)

P1 24 P2 3

P3 3

time quantum = 4 ms

RR – Example (2)

Process Burst Time (ms)

P1 10 P2 29

P3 3

P4 7

P5 12

time quantum = 10 ms

RR – Example (2)

P1

0 23 20 40

• waiting time; • P1 = 0 ms • P2 = (10 + (40 – 20) + (52 – 50)) = 32 ms • P3 = 20 ms • P4 = 23 ms • P5 = (30 + (50 – 40)) = 40 ms

• Average waiting time; = ( 0 + 32 + 20 + 23 + 40 )/5 = 23 ms

P4 P3 P2 P2 P2 P5 P5

30 10 52 50 61

Example of RR with Time Quantum = 20

Process Burst Time

P1 53

P2 17

P3 68

P4 24

• The Gantt chart is:

• Typically, higher average turnaround than SJF, but better response.

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

Time Quantum and Context Switch Time

Turnaround Time Varies With The Time Quantum

Page 6: Module 6: CPU Scheduling - AUTHORauthor.uthm.edu.my/uthm/www/content/lessons/5845/131021 - Chapte… · CPU SCHEDULING Contents ... Shortest-Remaining-Time-First (SRTF). 7 • SJF

6

Multilevel Queue Scheduling • Ready queue is partitioned into separate queues:

foreground (interactive) processes background (batch) processes

• Each queue has its own scheduling algorithm, foreground – RR background – FCFS

• Scheduling must be done between the queues.

1. Fixed priority scheduling; (i.e., serve all from foreground then from background). Possibility of starvation.

2. Time slice – each queue gets a certain amount of CPU time which it can schedule among its processes; • i.e., 80% of CPU time to foreground in RR and 20% of CPU time to

background in FCFS

Multilevel Queue Scheduling

Multilevel Feedback Queue Scheduling

• A process can move between the various queues; aging can be implemented this way.

• Separate processes according to CPU burst.

– Too much CPU time moved to a lower priority queue

– I/O bound and interactive processes higher priority queue

– A process that waits too long in a lower priority queue may be moved to a higher priority queue to prevent starvation.

Example of Multilevel Feedback Queue Scheduling

• Three queues: – Q0 – time quantum 8 milliseconds – Q1 – time quantum 16 milliseconds – Q2 – FCFS

• Scheduling – A new job enters queue Q0 which is served FCFS. When it

gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q1.

– At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2.

– Processes in Q2 are run on FCFS basis only when queues 0 and 1 are empty.

Multilevel Feedback Queues Scheduling Multiple-Processor Scheduling

• CPU scheduling more complex when multiple CPUs are available.

• **System in which the processors are identical -Homogeneous processors within a multiprocessor (in terms of their functionality).

• Load sharing – All processes go into one queue and are scheduled onto any available processor.

• Approaches to Multiple-Processor Scheduling: 1. Asymmetric multiprocessing – only one processor accesses

the system data structures (it handles scheduling decisions, I/O processing and other system activities), reducing the need for data sharing.

2. Symmetric Multiprocessing (SMP) – each processor is self-scheduling (has a common ready queue or each processor has its own private queue of ready processes)

Page 7: Module 6: CPU Scheduling - AUTHORauthor.uthm.edu.my/uthm/www/content/lessons/5845/131021 - Chapte… · CPU SCHEDULING Contents ... Shortest-Remaining-Time-First (SRTF). 7 • SJF

7

Real-Time Scheduling

• Hard real-time systems – required to complete a critical task within a guaranteed amount of time. – E.g. airplane system

• Soft real-time computing – requires that critical processes receive priority over less fortunate ones.

Exercise

Process Burst time

P1 30

P2 20

P3 10

P4 5

Draw the gantt chart, calculate the waiting time and average

waiting time if the processes are scheduled using listed algorithm:

a. FCFS

b. SJF (preemptive)

c. Round Robin (time quantum/period = 10)