290 likes | 588 Views
CS423UG Operating Systems . CPU Scheduling. Indranil Gupta Lecture 5 Sep 2, 2005. Schedule for Today’s Lecture. Why Scheduling? Scheduling Levels Basic Scheduling Algorithm (FCFS). Oh, the Process!. What is a Process?
E N D
CS423UG Operating Systems CPU Scheduling Indranil Gupta Lecture 5 Sep 2, 2005
Schedule for Today’s Lecture • Why Scheduling? • Scheduling Levels • Basic Scheduling Algorithm (FCFS) CS 423UG - Operating Systems,Indranil Gupta
Oh, the Process! • What is a Process? • It’s one executing instance of a “program” • Consists of code, data, heap, stack, PC, regs, and a little other information in the PCB • Process state, priority, accounting • Program counter, register variables, stack pointers, etc • Open files and devices • It can start/manipulate/kill other processes CS 423UG - Operating Systems,Indranil Gupta
Threads: Lightweight Processes • A thread is a single execution • Stack • Program counter • Registers • All threads within a process share resources • Address space • Text, data, heap • Open files, etc • Implementations of threads • User-level, Kernel-level, Hybrid, Pop-up CS 423UG - Operating Systems,Indranil Gupta
Scheduling • Deciding which process/thread should occupy a resource (CPU, disk, etc.) (CPU (horsepower)) I want to ride it Whose turn is it? Process 2 Process 3 Process 1 CS 423UG - Operating Systems,Indranil Gupta
When to schedule? “to schedule”=change the process that is in “running” state Options for when to schedule: • A new process starts • The running process exits • The running process is blocked/makes a syscall • I/O interrupt received (some processes may now be ready) • Clock interrupt (every 10 milliseconds) CS 423UG - Operating Systems,Indranil Gupta
Preemptive vs. Non-preemptive • Non-preemptive scheduling: • The running process keeps the CPU until it voluntarily gives up the CPU • process exits • switches to blocked state • 1and 4 only (no 3) • Preemptive scheduling: • The running process can be preempted and must release the CPU (can be forced to give up CPU) 4 Terminated Running 1 3 Ready Blocked CS 423UG - Operating Systems,Indranil Gupta
What are objectives of scheduling? (CPU (horsepower)) I want to ride it Whose turn is it? Process 2 Process 3 Process 1 CS 423UG - Operating Systems,Indranil Gupta
Scheduling Objectives • Fairness (nobody cries) • Priority (ladies first) • Efficiency (make best use of equipment) • Encourage good behavior (good boy/girl) • Support heavy loads (degrade gracefully) • Adapt to different environments (interactive, real-time, multimedia) CS 423UG - Operating Systems,Indranil Gupta
Concretely: Performance Metrics • Efficiency: percentage of time a resource is being used • Throughput: # of processes that complete per unit time • Turnaround Time (also called elapse time) • (process finish time – process entry time) : could be > process’s own total running time (why?) • Waiting Time • total amount of time process waits (in ready state) • Response Time • amount of time from when a request was first submitted until first response is produced: useful for interactive processes • Others • Fairness • Policy Enforcement: seeing that stated policy is carried out • Proportionality: meet users' expectation • Meeting Deadlines: avoid losing data CS 423UG - Operating Systems,Indranil Gupta
Different Systems, Different Focuses • For all • Fairness, policy enforcement, resource efficiency, resource balance • Batch Systems (e.g., supercomputers) • Max throughput, min turnaround time, max CPU utilization • Interactive Systems (e.g., PCs) • Min Response time, best proportionality • Real-Time Systems (e.g., Space Shuttle) • Predictability, meeting deadlines CS 423UG - Operating Systems,Indranil Gupta
Process Profiles • CPU – Bound • Time spent in I/O < Time spent computing in CPU • I/O – Bound • Time spent in I/O > Time spent computing in CPU • Process may spend time waiting for interrupts • Process Mix • Scheduling should balance load between I/O bound and CPU-bound processes • Ideal would be to run all equipment at 100% utilization but that would not necessarily be good for response time CS 423UG - Operating Systems,Indranil Gupta
Program Behaviors Considered in Scheduling I/O I/O • Is it I/O bound? Example? • Is it CPU bound? Example? • Batch or interactive environment • Urgency • Priority • Frequency of preemption • Frequency of page faults • How much execution time it has already received • How much execution time it needs to complete I/O compute compute CS 423UG - Operating Systems,Indranil Gupta
Scheduling Levels “Job”=Process • Long-term scheduler: Which jobs allowed to compete for the CPU and other resources • Medium-term scheduler: Which jobs to temporarily suspend or resume to smooth fluctuations in system load • Short-term scheduler: Assigning CPU to a ready process P3 P5 P8 P10 P2 Ready Queue (of processes) CS 423UG - Operating Systems,Indranil Gupta
CPU Scheduler • Proc 1: 14 time units • Proc2: 8 time units • Proc3: 8 time units • Dispatcher CPU Dispatcher Proc 1 Proc 10 Proc 2 Proc 11 Proc 3 Proc 12 Ready queue Blocked queue CS 423UG - Operating Systems,Indranil Gupta
Dispatcher • Gives the control of the CPU to the process that is awarded CPU by short-term scheduler • Functions: • switching context • switching back to user mode • jumping to the proper location in the user process • Dispatch Latency = delay of above operations • Needs to be fast CS 423UG - Operating Systems,Indranil Gupta
Single Processor Scheduling Algorithms • Batch systems • First Come First Serve (FCFS) • Shortest Job First • Interactive Systems • Round Robin • Priority Scheduling • Multi Queue & Multi-level Feedback • Shortest process time • Guaranteed Scheduling • Lottery Scheduling • Fair Sharing Scheduling CS 423UG - Operating Systems,Indranil Gupta
First Come First Serve (FCFS) • Process that requests the CPU FIRST is allocated the CPU FIRST • Called “FIFO”, Non-preemptive, Used in Batch Systems • Real life analogy: Any ticket counter • Implementation: FIFO queues • A new process enters the tail of the queue • The scheduler selects from the head of the queue. • Performance Metric: Average Waiting Time (AWT) • Given Parameters: • Burst Time (in ms), Arrival Time and Order • Can be generalized to processes with alternate CPU and I/O bursts: blocking process goes to queue’s tail CS 423UG - Operating Systems,Indranil Gupta
FCFS Example The final schedule: P1 (24) P2 (3) P3 (4) 24 27 0 P1 waiting time: 0-0 P2 waiting time: 24-3 P3 waiting time: 27-4 The average waiting time: (0+21+23)/3 = 14.667 CS 423UG - Operating Systems,Indranil Gupta
Problems with FCFS • Non-preemptive • Not optimal average waiting time (AWT) • Cannot utilize resources in parallel: • Suppose there is 1 CPU-bound process and many I/O-bound processes • Result: Convoy effect, low CPU and I/O Device utilization CS 423UG - Operating Systems,Indranil Gupta
What are Convoy Effects? • Consider n-1 jobs in system that are I/O bound and 1 job that is CPU bound. • I/O bound jobs pass quickly through the ready queue and suspend themselves waiting for I/O. • CPU bound job arrives at head of queue and executes until complete. • I/O bound jobs rejoin ready queue and wait for CPU bound job to complete. • I/O devices idle until CPU bound job completes. • When CPU bound job completes, other processes rush to wait on I/O again. • CPU becomes idle. CS 423UG - Operating Systems,Indranil Gupta
Mathematical Understanding of Scheduling: Queuing Theory ARRIVAL RATE processes/sec Server (one process at a time) CPU Input Queue SERVICE RATE 1/ sec CS 423UG - Operating Systems,Indranil Gupta
Queueing Theory • Poisson arrival with constant arrival rate (customers per unit time) • memoryless, each arrival is independent of previous arrivals • if is time to next process arrival, for Poisson: Probability( t ) = 1- e–t CS 423UG - Operating Systems,Indranil Gupta
Analysis of Queueing Behavior • Probability n customers arrive in time interval t is: e–t tn/ n! • Server/CPU: Assume exponential service times (similar to Poisson): • = (constant service rate) customers serviced per unit time • If ’ is time to execute current process, for Poisson: Probability( ’t ) = 1- e–t CS 423UG - Operating Systems,Indranil Gupta
A Useful Fact from Queuing Theory CS 423UG - Operating Systems,Indranil Gupta
Analysis of FIFO • Server Utilization: ρ = λ/μ • Time in System : W = 1/(μ-λ) • Time in Queue: Wq = ρ/(μ-λ) • Number in Queue (Little): Lq = ρ2/(1-ρ) CS 423UG - Operating Systems,Indranil Gupta
Work-Out • Example scheduler system • Arrival 2 jobs/sec • Service 3 jobs/sec • FIFO queue • Utilization ? • Time in system ? • Time in queue ? • Length of queue ? • Server Utilization: ρ = λ/μ • Time in System : W = 1/(μ-λ) • Time in Queue: Wq = ρ/(μ-λ) • Number in Queue (Little): Lq = ρ2/(1-ρ) CS 423UG - Operating Systems,Indranil Gupta
Work-Out • Example scheduler system • Arrival 2 jobs/sec • Service 3 jobs/sec • FIFO queue • Utilization 66.66% • Time in system 1 sec • Time in queue .6666 sec • Length of queue 1.3333 • Server Utilization: ρ = λ/μ • Time in System : W = 1/(μ-λ) • Time in Queue: Wq = ρ/(μ-λ) • Number in Queue (Little): Lq = ρ2/(1-ρ) CS 423UG - Operating Systems,Indranil Gupta
Summary • Scheduler: long-term, medium-term, short-term • FCFS • Queuing theory • Reading for this Lecture was: 2.5.0-2.5.2 • Next Monday: no lecture! • Reading for next Wed lecture: 2.5.3-2.5.6 • MP1 and HW1 ongoing • Have a good break! CS 423UG - Operating Systems,Indranil Gupta