45
Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Embed Size (px)

Citation preview

Page 1: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Operating Systems (CS 340 D)Dr. Abeer Mahmoud

Princess Nora UniversityFaculty of Computer & Information

SystemsComputer science Department

Page 2: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

(Chapter-6)CPU Scheduling

Page 3: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

3

Chapter 5: CPU Scheduling

1. Basic Concepts

2. Scheduling Criteria

3. Scheduling Algorithms

Page 4: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

4

OBJECTIVES:

To introduce CPU scheduling, which is the basis for multiprogrammed operating systems

To describe various CPU-scheduling algorithms

Page 5: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

5

Basic Concepts

Page 6: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

6

Basic Concepts

Maximum CPU utilization (keep the CPU as busy as possible ) obtained with multiprogramming

CPU–I/O Burst Cycle o Process execution consists of a

cycle of CPU execution and I/O wait

o Process execution begins with a CPU burst…That is followed by an I/O burst, which is followed by another CPU burst, then another I/O burst, and so on.o Eventually, the final CPU burst

ends with a system request to terminate execution

Page 7: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

7

Histogram of CPU-burst Times

The durations of CPU bursts vary greatly from process to process.

There is large number of short CPU bursts and a small number of long CPU bursts.

An I/O-bound program >>>> has many short CPU bursts.

A CPU-bound program >>>> has a few long CPU bursts

Page 8: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

8

CPU Scheduler

CPU Scheduler (/ short-term scheduler):

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

o The ready queue is not necessarily a first-in, first-out (FIFO) queue. It can be implemented as a FIFO queue, a priority queue, a tree, or an unordered linked list.

Page 9: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

9

Preemptive Scheduling

CPU scheduling decisions may take place when a process:

1. Switches from running to waiting state2. Switches from running to ready state3. Switches from waiting to ready4. Terminates

Scheduling under 1 and 4 is non-preemptive (/cooperative).

scheduling under 2 and 3 is preemptive

Page 10: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

10

Preemptive Scheduling (cont..)

Under non-preemptive scheduling,

o 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

Page 11: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

11

Dispatcher

Dispatcher : is the module gives control of the CPU to the process selected by the short-term scheduler.

The dispatcher should be as fast as possible, since it is invoked during every process switch.

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

Page 12: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

12

Scheduling Criteria

Page 13: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

13

Scheduling Criteria

Throughput – # of processes that complete their execution per time unit (e.g. 10 processes /sec)

Turnaround time – amount of time to execute a particular process (the sum of the periods spent waiting to get into memory, waiting in the ready queue, executing on the CPU, and doing I/O.)

Page 14: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

14

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

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

Scheduling Criteria (cont)

Page 15: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

15

Scheduling Algorithm Optimization Criteria

Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time

Page 16: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

16

Scheduling Algorithms

Page 17: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

17

Scheduling Algorithms

1. First-Come, First-Served Scheduling2. Shortest-Job-First Scheduling3. Priority Scheduling4. Round-Robin Scheduling5. Multilevel Queue scheduling6. Multilevel Feedback Queue

Page 18: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

18

(1) First-Come, First-Served (FCFS)

The simplest CPU-scheduling algorithm

The process that requests the CPU first is allocated first.

Can be implemented using FIFO queue: When a process enters the ready queue, its

PCB is linked onto the tail of the queue. When the CPU is free, it is allocated to the process at the head of the queue. The running process is then removed from the queue.

FCFS algorithm is non-preemptive

Page 19: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

19

Gantt chart: is a bar chart that illustrates a particular schedule, including the start and finish times of each of the processes.

Example(1): Consider the following set of processes that arrive at time 0,with the length of the CPU burst given in milliseconds

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 = 0; P2 = 24; P3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17

P1 P2 P3

24 27 300

(1) First-Come, First-Served (FCFS) -cont..

Page 20: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

20

Example(2): Consider the same previous set of processes arrive at time 0,with the length of the CPU burst in milliseconds

Process Burst Time(ms)

P1 24

P2 3

P3 3

Suppose that the processes arrive in the order: P2 , P3 , P1 The Gantt chart for the schedule is:

Waiting time for P1 = 6; P2 = 0; P3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3 >>>>Much better

than example (1)

P1P3P2

63 300

(1) First-Come, First-Served (FCFS) -cont..

Page 21: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

21

FCFS Pros. (++): Simplest algorithm

FCFS Cons. (--): The average waiting time is generally not minimal

and affected by processes’ order. Lower CPU and device utilization because of

convoy effect Not suitable for time-shared systems

Convoy effect - short processes wait for the one big process to get off the CPU. -This effect results in lower CPU and device utilization than might be possible if the shorter processes were allowed to go first.

(1) First-Come, First-Served (FCFS) -cont..

Page 22: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

(2) 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 If the next CPU bursts of two processes are the same, FCFS

scheduling is used to select the next process

Two schemes:

22

Non-preemptive –Preemptive

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

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

Page 23: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

23

Example(3): Consider the following set of processes with the length of the CPU burst given in milliseconds

Process Burst Time(ms)

P1 6

P2 8

P3 7

P4 3 SJF scheduling chart

Average waiting time = (3 + 16 + 9 + 0) / 4 = 7

P4 P3P1

3 160 9

P2

24

(2) Shortest-Job-First (SJF) Scheduling

Page 24: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

24

Example(4): Consider the following set of processes with the length of the CPU burst given in milliseconds

Process Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

Non-preemptive SJF

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

P1 P3 P2

73 160

P4

8 12

(2) Shortest-Job-First (SJF) Scheduling

Page 25: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

25

Example(5): Consider the following set of processes with the length of the CPU burst given in milliseconds

Process Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

Preemptive SJF

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

P1 P3P2

42 110

P4

5 7

P2 P1

16

(2) Shortest-Job-First (SJF) Scheduling

Page 26: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

26

SJF Pros. (++): SJF is optimal – gives minimum average waiting time for a given set of processes

SJF Cons. (--):The difficulty is knowing the length of the next CPU request ( some times this time is predicted)

(2) Shortest-Job-First (SJF) Scheduling

Page 27: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

(3) Priority Scheduling (cont..)

A priority number (integer) is associated with each process. The CPU is allocated to the process with the highest priority

Equal-priority processes are scheduled in FCFS order.

Text book assumes (smallest integer highest priority)

Two schemes:

27

Non-preemptive –Preemptive

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

if a new process arrives with priorty higher of current executing process, preempt

Page 28: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

Example(6): Consider the following set of processes with the length of the CPU burst given in milliseconds

Process Burst Time PriorityP1 10 3 P2 1 1 P3 2 4 P4 1 5

P5 5 2 Priority scheduling chart

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

28

(3) Priority Scheduling (cont..)

Page 29: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

29

Priority scheduling Pros. (++): Simple algorithm

Priority scheduling Cons. (--): Main Problem - Starvation ( /indefinite

blocking) low priority processes may never execute

Solution >> Aging >>as time progresses increase the priority of the process

(3) Priority Scheduling (cont..)

Page 30: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

(4) Round Robin Scheduling (RR)

(RR) algorithm is designed especially for timesharing

systems.

Each process gets a small unit of CPU time (time

quantum ).

After this time has elapsed, the process is preempted

and added to the end of the ready queue.

Time quantum (/ time slice ) (q)>> usually 10-100

ms.

The ready queue is treated as a circular queue and

implemented as FIFO queue

30

Page 31: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

Example(6): Consider the following set of processes with the length of the CPU burst given in milliseconds

time quantum = 4 ms

Process Burst TimeP1 24 P2 3 P3 3

The Gantt chart is:

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

P1 P2 P3 P1 P1 P1 P1 P1

0 4 7 10 14 18 22 26 30

31

(4) Round Robin Scheduling (RR)

Page 32: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

32

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 (depends on the size of the time quantum) If q is very large RR is same as FCFS If q is very small decrease the performance because of

context switch time and increase system overhead

(4) Round Robin Scheduling (RR)

Switching the CPU to another process requires performing a state save of the current process and a

state restore of a different process.

Page 33: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

33

Turnaround time depends on the size of the time quantum.

The average turnaround time can be improved if most processes finish their next CPU burst in a single time quantum.

(4) Round Robin Scheduling (RR)

Page 34: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

34

RR Scheduling Pros. (++):

Suitable to time-shared system (better response time)

RR Scheduling Cons. (--):

The average waiting time under the RR policy is often long

Context switch overhead is higher

(4) Round Robin Scheduling (RR)

Page 35: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

(5) Multilevel Queue Scheduling

Processes are easily classified into different groups, Such as:o Foreground (interactive) processes (may have higher

priority)o Background (batch) processes

Ready queue is partitioned into separate queues The processes are permanently assigned to one queue, Each queue has its own scheduling algorithm.

E.g.: foreground processes >> scheduled by RR & background process >> scheduled by FCFS

Scheduling must be done between the queues.

35

Page 36: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

Example (8): A multilevel queue scheduling algorithm with five queues, listed in order of priority

Each queue has absolute priority over lower-priority queues. E.g. No process in the batch

queue could run unless the queues for system processes, interactive processes, and interactive editing processes were all empty.

If an interactive editing process entered the ready queue while a batch process was running, the batch process would be preempted.36

(5) Multilevel Queue Scheduling

Page 37: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

37

Multilevel Queue Scheduling Pros. (++):

Low scheduling overhead Consider different process prosperities &

requirements

Multilevel Queue Scheduling Cons. (--):

Inflexible: a process can’t change it’s queue Starvation possibility

(5) Multilevel Queue Scheduling

Page 38: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

(6) Multilevel Feedback Queue

A process can move between the various queues

The idea is to separate processes according to the characteristics of their CPU bursts. If a process uses too much CPU time, it will be move

to a lower-priority queue.

This scheme leaves I/O-bound and interactive processes in the higher-priority queues.

A process that waits too long in a lower-priority queue may be moved to a higher-priority queue. This form of aging prevents starvation.

38

Page 39: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

Example(9): consider a multilevel

feedback queue scheduler with three queues: Q0 – RR with time quantum 8 milliseconds

(higher priority) Q1 – RR time quantum 16 milliseconds

Q2 – FCFS

Scheduling Processes in lower priority queue is selected

if the higher queues are empty A new job enters queue Q0 which is served

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

If Q0 is empty, process at Q1 job is again served RR and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2.39

Q0 ( highest priority)

Q1

Q2 (lowest priority)

(6) Multilevel Feedback Queue

Page 40: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

40

Multilevel-feedback-queue scheduler defined by the following parameters:

Number of queues Scheduling algorithms for each queue Method used to determine when to upgrade a process Method used to determine when to demote a process Method used to determine which queue a process will

enter when that process needs service

(6) Multilevel Feedback Queue

Page 41: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

41

Multilevel Feedback Queue Scheduling Pros. (++):

Very flexible>>>it is the most general CPU-scheduling algorithm.

Can be configured to prevent starvation.

Multilevel Feedback Queue Scheduling Cons. (--): Most complex algorithm

(6) Multilevel Feedback Queue

Page 42: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

42

Multiple-Processor Scheduling

Page 43: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

Multiple-Processor Scheduling

CPU scheduling is more complex when multiple CPUs are available.

load sharing becomes possible

Homogeneous processors -processors are identical in functionality (i.e. any processor can run any process in the ready queue)

43

Page 44: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud

44

Approaches to Multiple-Processor Scheduling

Multiple-Processor Scheduling

Asymmetric multiprocessing

Symmetric multiprocessing (SMP)

• Master processor executes system code & slave processors execute user code

• Only the master processor has all scheduling decisions, I/O processing, and other system activities

• Simple & reduce the need for data sharing

• Each processor is self-scheduling,

• All processes in common ready queue, or each has its own private queue of ready processes

• OS must ensure that two processors do not choose the same process and that processes are not lost from the queue.

Page 45: Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department

Dr. Abeer Mahmoud45

Thank you

End of Chapter 5