preview - cpp.edu

25
Preview CS 4310 Operating Syetems Process Scheduler Short Term Scheduler Long Term Scheduler Process Scheduling Algorithms for Batch System First Come First Serve Shortest Job First Shortest Remaining Job First Process Scheduling Algorithms for Interactive System Round Robin Priority Queue 1

Upload: others

Post on 05-Jun-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Preview - cpp.edu

Preview

CS 4310 Operating Syetems

◼ Process Scheduler

◼ Short Term Scheduler

◼ Long Term Scheduler

◼ Process Scheduling Algorithms for Batch System

◼ First Come First Serve

◼ Shortest Job First

◼ Shortest Remaining Job First◼ Process Scheduling Algorithms for Interactive

System◼ Round Robin◼ Priority Queue

1

Page 2: Preview - cpp.edu

Scheduler

CS 4310 Operating Syetems

◼ Chooses the next process (for execution), based on the rule(scheduling algorithm).

◼ Process scheduler is designed for making efficient use of theCPU.

◼ Long-Term Scheduler – selects a process from the pool ofjobs and loads into memory for execution

◼ Short-term scheduler – selects a process from the readyqueue and allocates to the CPU.

◼ Memory Scheduler – decides which processes should bekept in memory and which ones kept on disk when thenumber of processes is too large.

2

Page 3: Preview - cpp.edu

Three Level Scheduling

CS 4310 Operating Syetems 3

Page 4: Preview - cpp.edu

Effective CPU Scheduler is essential

CS 4310 Operating Syetems

The scheduler should make efficient use of the CPU because

process switching is very expensive.

When switching from user mode to kernel mode:

1.All information for the blocked (become ready state or blockstate) must be saved (state, register, stack pointer, PC, ..)

2.The memory map (information regarding memory management)must be saved.

3.A new process is selected by the scheduler.

4.Information for the new process must be loaded.

5.Start running the new process.

4

Page 5: Preview - cpp.edu

Process behavior

◼ CPU-bounded process – a process spends most of its timecomputing (using CPU)

◼ I/O-bounded process – a process spends most of its timewaiting for I/O.

CS 4310 Operating Syetems 5

Page 6: Preview - cpp.edu

When to make scheduling decision?

CS 4310 Operating Syetems

1. A process switching from “running” state

to the “blocked” state

2. A process switching from “running” state

to “ready” state

3. Terminating a process

4. A process switching from “blocked” state

to “ready” state

6

Page 7: Preview - cpp.edu

Scheduling Algorithm Goals

CS 4310 Operating Syetems

Different system environments have different goals,and different scheduling algorithms are needed.

◼Batch System –◼ Throughput – maximize jobs per hour

◼ Turnaround time – minimize time between submission

and termination

◼ CPU utilization – keep the CPU busy all the time

◼Interactive System –◼ Response time – response to request quickly

◼Real Time System –◼ Meeting deadlines – avoid losing data

7

Page 8: Preview - cpp.edu

First Come First Serve (Batch)

CS 4310 Operating Syetems

◼ Non-preemptive

◼ Simplest of all CPU-scheduling algorithms

◼ Implementation of the FCFS is easily

managed with FIFO queue.

◼ When a process enters the ready queue, its is

put on the end of the queue.

◼ When the CPU is free, scheduler chooses a

process from the head of ready queue.

◼ The average turnaround time might be long

with FCFS.

8

Page 9: Preview - cpp.edu

First Come First Serve (Batch)

24

Average Turnaround Time = (24+27+30)/3=27

P3 P1

3 6

Average Turnaround Time = (3+6+30)/3=13

P2 P3P1

0 27 30t

3 processes in the job queue in the

order of P1 P2 P3

FCFS

Optimal

t

CS 4310 Operating Syetems

0 30

Process CPU time

P1

P2

P3

24

3

3

24

P2

9

Page 10: Preview - cpp.edu

Shortest Job First (Batch)

CS 4310 Operating Syetems

◼ Non-Preemptive

◼ Just pick the shortest job

first

Process CPU run time needed

P1 6(msec)

P2 8(msec)

P3 7(msec)

P4 3(msec)

10

Page 11: Preview - cpp.edu

Shortest Job First (Batch)

SJF

FCFS

0 21 24

CS 4310 Operating Syetems

6 14

Average Turnaround Time = (6+14+21+24)/4=16.25

0 3 9

Average Turnaround Time = (3+9+16+24)/4=13

16 24

Assume that the arrivingtimes are in the order of

P1 > P2 > P >P3 4

P4 P1 P3 P2

P1 P2 P3 P4

Process CPU run time needed

P1 6(msec)

P2 8(msec)

P3 7(msec)

P4 3(msec)

11

Page 12: Preview - cpp.edu

Shortest Job First (Batch)

CS 4310 Operating Syetems

◼ When all the jobs are available

simultaneously, Shortest Job First

algorithm is optimal in that it gives

the minimal average turnaround

time for a given set of processes.

12

Page 13: Preview - cpp.edu

Shortest Remaining Time (Batch)

CS 4310 Operating Syetems

◼ Preemptive version of the SJF algorithm

is Shortest Remaining Time Next

algorithm.

◼ When a new process arrives at the

ready queue while previous process is

executing.

◼ The newly arrived process might be

shorter than what is left of the current

executing process preemption

13

Page 14: Preview - cpp.edu

Shortest Remaining Time (Batch)

CS 4310 Operating Syetems

0 1 2 3 5 10 17 26

Average Turnaround Time = ((17- 0) + (5-1) + (26-2)+(10-3))/4 = 13

Proces

s

Arrival

Time

CPU

time

P1 0 8

P2 1 4

P3 2 9

P4 3 5

P1 P2 P2 P2 P4 P1 P3

14

Page 15: Preview - cpp.edu

Round-Robin (Interactive)

CS 4310 Operating Syetems

◼ The Round-Robin Scheduling

algorithm is designed especially for

the timesharing systems.

◼ A small unit of time called time

quantum is defined (between 20 to 50

millisecond).

◼ Ready queue is treated as a circular

queue.

15

Page 16: Preview - cpp.edu

Round-Robin (Interactive)

CS 4310 Operating Syetems 16

Page 17: Preview - cpp.edu

Round-Robin (Interactive)

CS 4310 Operating Syetems

The main issue with round-robin

algorithm is the length of the quantum.

◼If quantum is too short, CPU time are

wasted for process switching – low CPU

utilization.

◼If quantum is too long, slow response

time.

17

Page 18: Preview - cpp.edu

Example Job Scheduling Problem

◼Q: Six batch jobs A through F, arrive at a computer center at almost the same time. They have estimated running times of 5, 2, 7, 4, 3, and 8 minutes. Their (externally determined) priorities are 2, 6, 5, 4, 3, and 1 respectively, with 6 being the highest priority. For each of the following scheduling algorithms, determine the average turnaround time. Ignore process switching overhead.

◼ (a) RR-3: Round robin (with time slice of 3 minute).

◼ (b) Priority scheduling.

◼ (c) First-come, first served (in order A, B, C, D, E, F).

◼ (d) Shortest job first.

CS 4310 Operating Syetems 18

Page 19: Preview - cpp.edu

Process A B C D E F

Running

Time5 2 7 4 3 8

Priority 2 6 5 4 3 1

A B C D E F A C D F C F

Time left: 2 0 4 1 0 5 0 1 0 2 0 0

3 5 8 11 14 17 19 22 23 26 27 29

Example Job

Scheduling Problem

Job# Start

Time

End

Time

Job Completion

A 0 3

B 3 5 Job B completed @5

C 5 8

D 8 11

E 11 14 Job E completed @14

F 14 17

A 17 19 Job A completed @19

C 19 22

D 22 23 Job D completed @23

F 23 26

C 26 27 Job C completed @27

F 27 29 Job F completed @29

(a) RR-3 Round Robin (3 minutes slices)

Average Turnaround Time

= (5+14+19+23+27+29)/6

= 19.5

Gantt Chart:

Schedule Table:

CS 4310 Operating Syetems 19

Page 20: Preview - cpp.edu

Process A B C D E F

Running

Time5 2 7 4 3 8

Priority 2 6 5 4 3 1

B C D E A F

0 2 9 13 16 21 29

Example Job

Scheduling Problem

Average Turnaround Time

= (2+9+13+16+21+29)/6 = 15

(b) Priority Scheduling

Job# Start

Time

End

Time

Job Completion

B 0 2 Job B completed @2

C 2 9 Job C completed @9

D 9 13 Job D completed @13

E 13 16 Job E completed @16

A 16 21 Job A completed @21

F 21 29 Job F completed @29

Gantt Chart:

Schedule Table: Priority: larger the number, higher the priority

CS 4310 Operating Syetems 20

Page 21: Preview - cpp.edu

◼ (c) First Come First Served (FCFS)

◼ Average Turnaround Time

◼ = (5+7+14+18+21+29)/6 = 15.667

A B C D E F

Process A B C D E F

Running

Time5 2 7 4 3 8

Priority 2 6 5 4 3 1

5 7 14 18 21 29

Example Job

Scheduling Problem

Schedule Table:

Gantt Chart:

Job# Start

Time

End

Time

Job Completion

A 0 5 Job A completed @5

B 5 7 Job B completed @7

C 7 14 Job C completed @14

D 14 18 Job D completed @18

E 18 21 Job E completed @21

F 21 29 Job F completed @29

CS 4310 Operating Syetems 21

Page 22: Preview - cpp.edu

◼ (d) Shortest Job First (SJF)

◼ Average Turnaround Time

◼ = (2+5+9+14+21+29)/6 = 13.333

Process A B C D E F

Running

Time5 2 7 4 3 8

Priority 2 6 5 4 3 1

B E D A C F

2 5 9 14 21 29

Example Job

Scheduling Problem

Gantt Chart:

Schedule Table:

Job# Start

Time

End

Time

Job Completion

B 0 2 Job A completed @5

E 2 5 Job B completed @7

D 5 9 Job C completed @14

A 9 14 Job D completed @18

C 14 21 Job E completed @21

F 21 29 Job F completed @29

CS 4310 Operating Syetems 22

Page 23: Preview - cpp.edu

Priority Queue Scheduling

CS 4310 Operating Syetems

◼ A priority is assigned to each process

◼ CPU is allocated to a process based on thepriority.

◼ Equal priority processes are scheduledusing FCFS or Round-Robin.

◼ Example:- School: faculty members have higherpriority than students.- Company: Customers who pay moremoney can get higher priority.

23

Page 24: Preview - cpp.edu

Priority Queue Scheduling

CS 4310 Operating Syetems 24

Page 25: Preview - cpp.edu

Priority Queue Scheduling

CS 4310 Operating Syetems

◼ A major problem with priority

scheduling algorithm is the starvation

of lower priority processes.

◼ A solution to the problem of starvation

is aging.

◼ Aging is a technique of gradually

increasing the priorities of processes

that wait in the system for a long time.

25