Você está na página 1de 47

Chapter 6: CPU Scheduling

Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation

Chapter 6

Operating Systems

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 An I/O bound program typically has many very short CPU bursts A CPU bound program typically have a few very long CPU bursts

Chapter 6

Operating Systems

Alternating Sequence of CPU And I/O Bursts

Histogram of CPU-burst Times


Chapter 6 Operating Systems 3

CPU 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 state. 4. Terminates. Scheduling under 1 and 4 is nonpreemptive (you dont have option) A new process must be selected for execution under 1 and 4 Under 2 and 3, we can: Allow the running process to continue: nonpreemptive. Perform scheduling: preemptive. Preemptive scheduling can lead to problems: what if a process is modifying shared data when it is preempted? Solutions in Chapter 7
Operating Systems 4

Chapter 6

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 process.

Chapter 6

Operating Systems

Dispatch Latency

Conflict phase: 1. Preemption of any processes running in the kernel 2. Low-priority processes release resources needed by the high-priority processes
Chapter 6 Operating Systems 6

Scheduling Criteria
CPU utilization: % of time CPU is busy Throughput: # of processes completed per time unit Depend on process length Turnaround time: total time elapsed between process creation and termination Include: waiting to get into memory, waiting in the ready queue, executing on the CPU, and doing I/O Waiting time: time spent in the ready queue Affected by CPU scheduling algorithm Response time: time between process creation and first response (not final output) Important for interaction (for time-sharing environment)
Chapter 6 Operating Systems 7

Optimization Criteria
Want maximize CPU utilization and throughput Want minimize turnaround time, waiting time, and response time Usually, want optimize the average measure Sometimes, want to optimize MIN or MAX values E.g. minimize the MAX response time For interactive systems, want minimize variance in response time Lead to predictability

Chapter 6

Operating Systems

CPU Scheduling Algorithms 1. 2. 3. 4. 5. FCFS scheduling Shortest-Job-First (SJR) Scheduling Priority Scheduling Round Robin Scheduling Multilevel Priority

Chapter 6

Operating Systems

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


Process Burst Time P1 24 P2 3 P3 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


Chapter 6 Operating Systems 10

FCFS Scheduling (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
Chapter 6 Operating Systems 11

2. Shortest-Job-First (SJR) Scheduling


Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time first. 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 know as the Shortest-Remaining-Time-First (SRTF). SJF is optimal gives minimum average waiting time for a given set of processes.
Chapter 6 Operating Systems 12

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)
P1 0 3 7 P3 8 P2 12 P4 16

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


Chapter 6 Operating Systems 13

Example of Preemptive SJF


Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 SJF (preemptive)
P1 0 2 P2 4 P3 5 P2 7 P4 11 P1 16

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


Chapter 6 Operating Systems 14

Determining Length of Next CPU Burst


There is no way to know the length of the next CPU burst. Can only estimate the length. Can be done by using the length of previous CPU bursts, using exponential averaging.
1. tn actual lenght of nthCPU burst 2. n1 predicted value for the next CPU burst 3. , 0 1 4. Define :

n+1 tn (1 - ) n .
Chapter 6 Operating Systems 15

Prediction of the Length of the Next CPU Burst

=0.5 0=10

Chapter 6

Operating Systems

16

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-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.

Chapter 6

Operating Systems

17

3. 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 (what information should be available ?) nonpreemptive SJF is a priority scheduling where priority is the predicted next CPU burst time. Problem Starvation low priority processes may never execute. Solution Aging as time progresses increase the priority of the process.
Chapter 6 Operating Systems 18

4. Round Robin (RR)


Suitable for time sharing systems. Each process gets a small unit of CPU time (time quantum or time slice), 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 (n1)q time units. What will happen after the time slice expires ? Is RR preemptive or nonpreemptive ? Performance q large FIFO q small q must be large with respect to context switch, otherwise overhead is too high.

Chapter 6

Operating Systems

19

Time Quantum and Context Switch Time

Minimize overhead (context swaps). Processor sharing: make time slice as minimum as possible
Selecting the suitable time quantum is a trade off.
Chapter 6 Operating Systems 20

Turnaround Time Varies With The Time Quantum

Chapter 6

Operating Systems

21

Example of RR with Time Quantum = 20


Process Burst Time P1 53 P2 17 P3 68 P4 24 The Gantt chart is:
P1 0 20 P2 37 P3 57 P4 77 P1 P3 97 117 P4 P1 P3 P3

121 134 154 162

Typically, higher average turnaround than SJF, but better response why?.
Chapter 6 Operating Systems 22

5. Multilevel Queue
Ready queue is partitioned into separate queues: foreground (interactive) background (batch) Each queue has its own scheduling algorithm based on the type of the processes that is allocated to that queue, foreground RR background 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, 20% to background in FCFS
Chapter 6 Operating Systems 23

Multilevel Feedback Queue


A process can move between the various queues; aging can be implemented this way. Attacks both efficiency and response time problems. 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
Chapter 6 Operating Systems 24

Multilevel Queue Scheduling

Can cause starvation why? How to solve this ?


Chapter 6 Operating Systems 25

Multilevel Feedback Queues

Chapter 6

Operating Systems

26

Example of Multilevel Feedback Queue


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. Usually, processes in Q1 are not served until Q0 is empty, and Q2 processes are not served until Q0 and Q1 are empty
Operating Systems 27

Chapter 6

Multiple-Processor Scheduling
CPU scheduling more complex when multiple CPUs are available (but load sharing is possible) Assume homogeneous systems (i.e., the processors are identical) Use a common ready queue Two scheduling approaches: Symmetric: Each processor is self-scheduling: each processor selects a process to execute from the common ready queue Must synchronize the processors to ensure two processors do not choose the same process and processes are not lost from queue Asymmetric: Appoint one processor as scheduler for the other processors Data sharing problem solved: only one processor accesses the queue.
Chapter 6 Operating Systems 28

Real-Time Scheduling
Hard real-time systems required to complete a critical task within a guaranteed amount of time. Soft real-time computing requires that critical processes receive priority over less fortunate ones. Priority Inversion Problem: A lower priority process is blocking a higher priority process from running. Solution: Priority Inheritance protocol

Chapter 6

Operating Systems

29

Real-Time Scheduling
Hard real-time systems: required to complete a critical task within a guaranteed amount of time A process is submitted with a time requirement for completion or performing I/O Scheduler either admits the process with guarantee that the deadline will be met or rejects the request Scheduler must know how long each OS function takes Cant use secondary storage or virtual memory Use special purpose dedicated hardware

Chapter 6

Operating Systems

30

Real-Time Scheduling
Soft real-time systems: requires that critical processes always have top priority, may lead to long delays or starvation of other processes (less restrictive than hard real time) Implementation issues: 1- Must use priority scheduling, real-time processes must have highest priority, need disallow process aging 2- Dispatch latency must be small Problem: many OS do not allow context switch during a system call (or even an IO), thus the dispatching will be delayed leading to high dispatch latency Solution: allow system calls to be preemptible

Chapter 6

Operating Systems

31

Modified slides link

http://www.2shared.com/file/1223 7657/7aee6df4/ch456term_project .html

Exam 8-4 from 3:30 to 4:30 ch 1-7

Chapter 6

Operating Systems

32

Algorithm Evaluation
How do we select a CPU-scheduling algorithm for a particular system? 1. Define desired criteria (e.g., maximize CPU utilization with the constraint that max response time < 1 second) 2. Evaluate/compare algorithms Four algorithm evaluation methods 1. Deterministic modeling 2. Queuing models 3. Simulations 4. Implementation
Chapter 6 Operating Systems 33

Deterministic Modeling
Use a specific predetermined workload and determines the performance of each algorithm for that workload What we did in class Simple and fast Results only valid for specific workload

Chapter 6

Operating Systems

34

Queuing Models
The computer system is viewed as a network of servers, each sever has a queue of waiting processes. E.g., CPU with ready queue, I/O device with device queue Given arrival rates and service rates, we can compute average queue length, average waiting time, etc. (called queuing-network analysis) Requires inaccurate/unrealistic assumptions for numerical solutions E.g. assume processes are independent, arrival rate follows Poisson distribution, service time follows exponential distribution Queuing models are only an approximation of a real system
Chapter 6 Operating Systems 35

Simulations
Two ways to generate the data to drive the simulation 1. Generate workload according to certain probability distributions 2. Use trace tapes that record the sequence of actual events in the real system Gather statistics of the algorithm performance as the simulation executes Non-trivial to design, code, and debug a simulator Simulations often require many hours of CPU time
Chapter 6 Operating Systems 36

Evaluation of CPU Schedulers by Simulation

Chapter 6

Operating Systems

37

Implementation
Implement the scheduling algorithm on a real system The accurate evaluation technique. Cost is tremendous: coding algorithm, modifying kernel data structures, system must be taken down Ideally, we want flexible scheduling algorithms During OS build-time, boot time, or run time, variables used by the scheduler can be changed by system manager or by user Few operating systems allow tunable scheduling. can you think of an example of tuning ?
Chapter 6 Operating Systems 38

Scheduling in Practice Solaris 2 Windows 2000

Chapter 6

Operating Systems

39

Solaris 2 Scheduling
Priority based, four classes of scheduling: real-time, system, time sharing, and interactive Each class includes a set of priorities Default scheduling class for a process is time sharing Time sharing: use multilevel feedback queue, the higher the priority, the smaller the time slice (inverse relation). Interactive: same as time sharing Systems: for kernel processes only Priority is fixed, no time-slicing. Thread runs until it blocks or is preempted by a higher priority thread Real-time: highest priority (guaranteed response time) In this model, when two processes have the same priority number, use RR.
Chapter 6 Operating Systems 40

Solaris 2 Scheduling

Chapter 6

Operating Systems

41

Windows 2000 Scheduling


Priority-based, preemptive scheduling 32 levels of priority, large integer = high priority Variable class contains threads having priority from 1 to 15 and real-time class contains threads having priority from 16 to 31 Ready queue for each scheduling priority, RR scheduling Dispatcher traverses the priority queue from highest to lowest priorities and select the first process that is ready to be scheduled. If no ready process the dispatcher executes Idle process

Chapter 6

Operating Systems

42

Windows 2000 Scheduling


There is a relationship between the numeric priority of the windows2000 kernel and the WIN32 API WIN32 identifies six priority classes: real-time, high, above normal, normal, below normal, idle. Except real-time, all priority classes are variable class priority. Seven levels of relative priority for each one of the above six classes An integer priority is assigned to a thread based on the (class, relative priority) pair Relative priority may be raised or lowered Lowered when a threads time quantum runs out Increased when a thread is released from a wait operation => Give good response time to interactive threads When a process moves into the foreground, increase time quantum by some factor (typically by 3)

Chapter 6

Operating Systems

43

Windows 2000 Priorities


Priority-based and preemptive scheduling 32 Priority levels with two classes:

Variable class: 1-15


Real-time class: 16-31 Value of each priority class Relative Priority
Chapter 6 Operating Systems 44

Thread Scheduling
Local Scheduling How the threads library decides which thread to put onto an available LWP Global Scheduling How the kernel decides which kernel thread to run next

Chapter 6

Operating Systems

45

Pthread Scheduling API


#include <pthread.h> #include <stdio.h> #define NUM THREADS 5 int main(int argc, char *argv[]) { int i; pthread t tid[NUM THREADS]; pthread attr t attr; /* get the default attributes */ pthread attr init(&attr); /* set the scheduling algorithm to PROCESS or SYSTEM */ pthread attr setscope(&attr, PTHREAD SCOPE SYSTEM); /* set the scheduling policy - FIFO, RT, or OTHER */ pthread attr setschedpolicy(&attr, SCHED OTHER); /* create the threads */ for (i = 0; i < NUM THREADS; i++) pthread create(&tid[i],&attr,runner,NULL);
Chapter 6 Operating Systems 46

Pthread Scheduling API


/* now join on each thread */ for (i = 0; i < NUM THREADS; i++) pthread join(tid[i], NULL); } /* Each thread will begin control in this function */ void *runner(void *param) { printf("I am a thread\n"); pthread exit(0); }

Chapter 6

Operating Systems

47

Você também pode gostar