Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System...

23
Chapter 5: CPU Scheduling Chapter 5: CPU Scheduling (Continuation) (Continuation)

Transcript of Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System...

Page 1: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

Chapter 5: CPU SchedulingChapter 5: CPU Scheduling(Continuation)(Continuation)

Page 2: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.2 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Determining Length of Next CPU BurstDetermining Length of Next CPU Burst

Can only estimate the length

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

contains the most recent information, contains the past history

α is the relative weight of recent and past history; more commonly it is taken α=1/2, so recent history and past history are equally weighted

nt n

1

1. actual length of CPU burst

2. stores the past history

3. predicted value for the next CPU burst

4. , 0 1

5. Define:

nth

n

n

nt

1 1n n nt

Page 3: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.3 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Philosophy of Exponential AveragingPhilosophy 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 -j + …

+(1 - )n +1 0

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

Page 4: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.4 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Prediction of the Length of the Next CPU Prediction of the Length of the Next CPU BurstBurst

0 010; 1/2; 6t

1 1n n nt

Page 5: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.5 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Priority SchedulingPriority Scheduling

A priority number (integer) is associated with each process

Smaller number higher priority, larger number lower priority

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

Preemptive (preempt the CPU if the priority of newly arrived process is higher than the priority of the currently running process)

Nonpreemptive (put the new process at the head of the ready queue if its priority is higher than the priority of other processes in the ready queue )

Equal-priority processes are scheduled in FCFS order.

Page 6: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.6 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Priority SchedulingPriority Scheduling

SJF is a special case of the general priority scheduling. In this case the priority is the inverse of the predicted next CPU burst time: the larger the CPU burst, the lower the priority, and vice versa.

Problem Starvation – low priority processes may never execute

Solution Aging – as time progresses increase the priority of the process that wait in the system for a long time

Page 7: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.7 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Round Robin (RR) SchedulingRound Robin (RR) Scheduling

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 the time frames of at most q time units at once. No process waits more than (n-1)q time units.

Performance

q large FCFS

q too small (about 1 millisecond) processor sharing q must be larger, with respect to context switch, otherwise overhead is too high

Page 8: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.8 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Time Quantum and Context Switch TimeTime Quantum and Context Switch Time

Page 9: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.9 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Example of RR with Time Quantum = 20Example 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

Page 10: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.10 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Turnaround Time Varies With The Time QuantumTurnaround Time Varies With The Time Quantum

•If the time quantum is too large, RR scheduling degenerates to FCFS policy

•About 80% of the CPU bursts should be shorter than the time quantum

Page 11: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.11 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Multilevel QueueMultilevel Queue

Ready queue is partitioned into separate queues:foreground (interactive)background (batch)

Each queue has its own scheduling algorithm

foreground – Round Robin (RR)

background – First come, first served (FCFS)

Scheduling must be done between the queues

Fixed priority scheduling; (i.e., serve all 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 and 20% to background in FCFS

Page 12: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.12 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Multilevel Queue SchedulingMultilevel Queue Scheduling

Page 13: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.13 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Multilevel Feedback QueueMultilevel Feedback Queue

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

Multilevel-feedback-queue scheduler is 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

Page 14: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.14 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Example of Multilevel Feedback QueueExample of Multilevel Feedback Queue

Three queues:

Q0 – RR with time quantum 8 milliseconds

Q1 – RR time quantum 16 milliseconds

Q2 – FCFS, only when Q0 and Q1 are empty

Scheduling

A new job enters queue Q0 . 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 receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2.

Page 15: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.15 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Multilevel Feedback QueuesMultilevel Feedback Queues

Page 16: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.16 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Thread SchedulingThread Scheduling

On operating systems that support user-level and kernel-level threads the latter are being scheduled by the operating system

User-level threads are managed by a thread library. To run on a CPU, user-level thread must be mapped to an associate kernel-level thread. This mapping may use a lightweight process (LWP - a mean of achieving multitasking. In contrast to a regular (full-blown) process, an LWP shares all (or most of) its logical address space and system resources with other process(es); in contrast to a thread, a lightweight process has its own private process identifier and parenthood

relationships with other processes)

Page 17: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.17 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Thread SchedulingThread Scheduling

Local Scheduling (Process-Contention Scope) – how the threads library decides which thread to put onto an available lightweight process (LWP)

Global Scheduling (System-Contention Scope) – how the kernel decides which kernel thread to run next

Page 18: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.18 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Windows XP SchedulingWindows XP Scheduling

Windows XP schedules threads using a priority-based, preemptive scheduling algorithm.

The Windows XP scheduler ensures that the higher priority thread will always run.

The portion of the Windows XP kernel that handles scheduling is called the dispatcher.

A thread selected to run by the dispatcher will run until it is preempted by a higher priority thread, until it terminates, until its time quantum ends, or until it calls a blocking system call (such as for Input/Output operation)

If a higher priority real-time thread becomes ready while a lower priority thread is running, the lower-priority thread will be preempted.

Page 19: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.19 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Windows XP SchedulingWindows XP Scheduling

There are two priority classes:

The variable class contains threads having priorities from 1 to 15

The real-time class contains threads with priorities from 16 to 31.

A single thread running at priority 0 is used for memory management.

Each scheduling priority has a separate queue of the corresponding processes

The dispatcher uses a queue for each scheduling priority and traverses the set of queues from highest to lowest until it finds a thread that is ready to run.

If no ready thread is found, the dispatcher will execute a special thread called the idle thread.

Page 20: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.20 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Windows XP PrioritiesWindows XP Priorities

The relative priorities The priority

within a class classes

By default, the base priority is the value of the Normal relative priority for the specific class

Page 21: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.21 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Windows XP Priorities: Some RulesWindows XP Priorities: Some Rules

Processes are typically members of the NORMAL_PRIORITY_CLASS.

A process will belong to this class unless the parent of the process was of the IDLE_PRIORITY_CLASS or unless another class was specified when the process was created.

The initial priority of a thread is typically the base priority of the process the thread belongs to.

When a thread’s time quantum runs out, the thread is interrupted; if the thread is in the variable-priority class, its priority is lowered. However, the priority is never lowered below the base priority.

Page 22: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

5.22 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005

Windows XP Priorities: Some RulesWindows XP Priorities: Some Rules

Lowering the thread’s priority tends to limit the CPU consumption of compute-bound threads.

When a variable-priority thread is released from a wait operation, the dispatcher boosts the priority. The amount of boost depends on what the thread is waiting for:

A thread that was waiting for keyboard I/O would get a large increase

A threat that was waiting for a disk operation would get a moderate increase

Windows XP distinguishes between the foreground process that is currently selected on the screen and the background processes that are not currently selected. When a process moves into the background, Windows XP increases the scheduling quantum by some factor – typically by 3.

Page 23: Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.

End of Chapter 5End of Chapter 5