P. 1
Slides 4 Scheduling

Slides 4 Scheduling

|Views: 12|Likes:
Published by André Perdigão

More info:

Categories:Types, School Work
Published by: André Perdigão on Oct 18, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

12/03/2011

pdf

text

original

Operating Systems

4. CPU Scheduling

Luís Moura e Silva Email: luis@dei.uc.pt Departamento de Eng. Informática Universidade de Coimbra

Disclaimer
 

This slides and notes are heavily based on the companion material of [Silberschatz05].The original material can be found at:
 

http://codex.cs.yale.edu/avi/os-book/os7/slide-dir/index.html

 

In some cases, material from [Stallings04] may also be used. The original material can be found at:
 

http://williamstallings.com/OS/OS5e.html

 

The respective copyrights belong to their owners.

2

System Queues

3

Preemptive vs. Non-Preemptive
 

Non-Preemptive Scheduling Once the CPU is allocated to a process, the process keeps the CPU until it terminates or it blocks in a I/O operations and switches to the waiting state (e.g. in Windows 98). Preemptive Scheduling The operating system may decide and is able to take the CPU away from a running process (e.g. in WindowsXP, Linux)

 

4

Scheduling Criteria
 

CPU utilization
 

Keep the CPU as busy as possible

 

Throughput
 

Number of processes that complete their execution per time unit

 

Turnaround time
 

Amount of time to complete a particular process

 

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
5

Optimization Criteria…
   

As seen before, it all depends on what type of system we are aiming at… But, we would like:
         

Maximum CPU utilization Maximum throughput Minimum turnaround time Minimum waiting time Minimum response time

That’s a multi-objective function, not really possible…

6

Why can we optimize?
CPU-IO Cycle CPU-burst Times

7

Scheduling Algorithms

  FCFS   SJF (or SPN)  SRTF   PRIORITY   ROUND-ROBIN   HRRN   MULTILEVEL FEEDBACK
8

Algorithm 1: First-Come First-Served (FCFS)
Process P1 P2 P3
 

Burst Time 24 3 3

Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is:
P1 0 24 P2 27 P3 30

     

Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17 This is a non-preemptive algorithm
9

First-Come First-Served (FCFS) – Cont.
   

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

       

Waiting time for P1 = 6; P2 = 0; P3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3 Much better than previous case. Convoy effect
   

Short process behind long process I/O bound vs. CPU bound problem

10

Algorithm 2: Shortest-Job-First (SJF)
 

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

How do you determine that? Can you guess the future? Also known as Shortest-Job-Next (SJN), which is more correct.

 

Two schemes:
 

SJF (non-premptive): once CPU given to the process it cannot be preempted until completes its CPU burst. SRTF (preemptive): 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).

 

11

(Non-Preemptive) Shortest-Job-First (SJF)

 

Process P1 P2 P3 P4

Arrival Time 0.0 2.0 4.0 5.0

Burst Time 7 4 1 4

SJF (non-preemptive)
P1 0 3 P3 7 8 P2 12 P4 16

     

Average waiting time = (0 + 6 + 3 + 7)/4 = 4 Can be proved optimal (minimizes waiting time) for a set of processes Used frequently in long-term scheduling
12

(Preemptive) Shortest-Remaining-Time-First (SRTF)

 

Process P1 P2 P3 P4

Arrival Time 0.0
2.0

Burst Time 7 4 1 4

4.0 5.0

SRTF (preemptive)
P1 0 2 P2 P3 4 5 P2 7 P4 11 P1 16

 

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

How to predict the next CPU burst time?
 

Based on an exponential moving average based on the past.

14

Priority Scheduling
   

A priority number is associated with each process The CPU is allocated to the process with the highest priority:
   

Preemptive Non-preemptive

 

SJF can be seen as priority scheduling where priority is the predicted next CPU burst time Problem ≡ Starvation
 

   

Low priority processes may never execute As time progresses increase the priority of the process
15

Solution ≡ Aging
 

Starvation @MIT
When the system managers shutdown the IBM 7094 at MIT in 1973, they found a low-priority process that had been submitted in 1967 and had not yet run…

IBM 7094
16

Priority-Based Scheduler

17

Algorithm 3: 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.
 

New processes are added to the tail 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 ⇒ FCFS q small ⇒ q must be large with respect to context switch, otherwise overhead is too high. If so, having N processes is like having a machine running at 1/N of the speed.
18

RR with a quantum of 20

 

Process P1 P2 P3 P4

Burst Time
53 17 68 24

The Gantt chart is:

 

Typically, higher average turnaround than SJF, but better response time
19

Timeline charts are relevant!

20

Time Quantum and Context Switches

21

Turnaround Time vs. Time Quantum
   

Turnaround time depends on time quantum but the relation is not direct. In general, turnaround time can be improved if most processes finish on a time quantum.

22

HRRN: Highest Response Ratio Next
 

Choose next process with the highest ratio (R):

R = time spent waiting + expected service time expected service time

     

This algorithm accounts for the age of the process. Shorted jobs are favored (smaller denominator) But aging without service (waiting time) increases R.
23

Multilevel Queue Scheduling

24

Multilevel Queue Scheduling (2)
 

Ready queue is partitioned into separate queues. E.g.
   

Foreground (interactive) Background (batch)

 

Each queue has its own scheduling algorithm:
   

Foreground – RR Background – FCFS

 

Scheduling must be done between the queues.
   

Fixed priority scheduling (i.e., serve all processes from foreground; then from background). Possibility of starvation. Time slice: each queue gets a certain amount of CPU time which it can schedule amongst its processes; i.e., 80% to foreground in RR; 20% to background in FCFS
25

Multilevel Feedback Queue Scheduling
 

Processes can move among queues!
 

Aging can be implemented this way

 

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

26

Example of a scheduler with three queues

Note: queue serving is priority-based!
27

Example of a scheduler with three queues (2)
 

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.

     

Gives high-priority to any process with CPU bursts of 8ms or less.
 

Grabs CPU, do processing, move along to next IO cycle…

Process with bursts of >8ms and <16ms are served quickly but with less priority Long running processes since to Q2 and are served on any CPU cycles remaining.
28

Characteristics of Scheduling Algorithms
from [Stallings05]

29

Operating Systems
6. Scheduling: examples from Stallings

Luís Moura e Silva Email: luis@dei.uc.pt Departamento de Eng. Informática Universidade de Coimbra

Example
 

This example is taken from [Stallings04]

 

Homework
   

Calculate Finish-Time, Waiting-Time, Turnaround Time for each process Calculate Average Waiting Time and Average Turnaround Time
31

FCFS

     

Short process may have to wait a very long time before it can execute Favors CPU-bound processes I/O processes have to wait until CPU-bound process completes
32

Round-Robin (q=1)

       

Uses preemption based on a clock When an interrupt occurs, the running process is placed in the ready queue Next ready job is selected Known as time slicing
33

SPN / SJF

     

Non-preemptive policy Process with shortest expected processing time is selected next Short process jumps ahead of longer processes
34

SRTF

   

Preemptive version Must estimate processing time
35

Multilevel Feedback

 

Penalize jobs that are longer

36

Comparison of Scheduling Algorithms

37

Comparison of Scheduling Algorithms (2)

38

Exercise_A: SCHEDULING ALGORITHMS

Considere o conjunto de 5 processos (P1...P5) com o seguinte comportamento:

(i)  (ii)  (iii)  (iv) 

3.1- Represente de uma forma gráfica o escalonamento dos processos para os seguintes algoritmos: Round-Robin (quantum = 3) SRT HRRN Multilevel Feedback (3 filas: Q0,Q1,Q2 com os algoritmos RR(1), RR(2), FCFS) 3.2- Para cada um dos algoritmos apresente uma tabela com os seguintes valores: (a) Finish Time; (b) Turnaround Time; (c) Waiting Time.

39

Exercise_B: SCHEDULING ALGORITHMS

Considere o conjunto de 5 processos (P1...P5) com o seguinte comportamento:

(i)  (ii)  (iii)  (iv) 

3.1- Represente de uma forma gráfica o escalonamento dos processos para os seguintes algoritmos: Round-Robin (quantum = 3) SRT HRRN Multilevel Feedback (3 filas: Q0,Q1,Q2 com os algoritmos RR(1), RR(2), FCFS) 3.2- Para cada um dos algoritmos apresente uma tabela com os seguintes valores: (a) Finish Time; (b) Turnaround Time; (c) Waiting Time.

40

Exercise_C: SCHEDULING ALGORITHMS

Considere o conjunto de 5 processos (P1...P5) com o seguinte comportamento:

(i)  (ii)  (iii)  (iv) 

3.1- Represente de uma forma gráfica o escalonamento dos processos para os seguintes algoritmos: Round-Robin (quantum = 3) SRT HRRN Multilevel Feedback (3 filas: Q0,Q1,Q2 com os algoritmos RR(1), RR(2), FCFS) 3.2- Para cada um dos algoritmos apresente uma tabela com os seguintes valores: (a) Finish Time; (b) Turnaround Time; (c) Waiting Time.

41

Operating Systems
6. CPU Scheduling: O.S.

Luís Moura e Silva Email: luis@dei.uc.pt Departamento de Eng. Informática Universidade de Coimbra

Traditional UNIX Scheduling
 

Multilevel feedback using RoundRobin within each queue If a process does not block or complete within a second, it is preempted Priorities are recomputed once per second Base priority divides all processes into fixed bands of priority levels
Process Px CPUx(i) = CPUx (i-1) / 2 Px(i) = Basex + (CPUx(i) / 2) + nicex The priority of each process is recomputed once per second.
43

 

 

 

       

Traditional Unix Scheduling
Base priority=60

CPU count: incremented very second

44

Windows XP Scheduling
 

Priority-based Preemptive Scheduling
   
   

The highest priority queue is always served first 32 levels of priorities, two classes of threads: The priorities from all threads vary except if they are in a REAL_TIME_PRIORITY_CLASS Within each class, there are relative priorities Base priority = normal (by default) When the quantum expires, the priority is lowered down After a wait operation, the priority is increased
Real-time, having a priority from 16 to 31 Variable, having a priority from 1 to 15

         

45

Linux 2.6 Scheduling
   

Preemptive Priority-Based Scheduler Two separate priority ranges:
   

Real-time (0-99) and Nice (100-140) Two algorithms: time-sharing and real-time

 

Scheduling (Time-Sharing Algorithm  Fair preemptive):
   

   

Active Array and Expired Array Scheduling is done from the active array. The scheduler chooses the task from the active array for execution. It runs until its time slice is gone, or it is blocked. Whenever a task has used all its time slice, it’s moved to the expired array. Recalculation of its priority is done at that time. Execution goes on until the active array is empty. When the active array is empty, it’s switched with the expired array.

46

Linux 2.6 Scheduling (2)
 

Higher priority tasks are assigned a longer time quantum

 

Two arrays are used for scheduling

47

Reference
 

Chapter 5: CPU Scheduling
 

5.1, 5.2, 5.3, 5.6, 5.8

48

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->