cpu scheduling algorithms

Download CPU Scheduling Algorithms

Post on 06-May-2015




3 download

Embed Size (px)


A presentation on implementing CPU Scheduling Algorithms in C++


  • 1.CPUSchedulingAlgorithms Group No. 2 1

2. Overview Basic Concepts Scheduling Criteria Scheduling Algorithms Implementation in C++ Demonstration Involvement of Operating SystemCPU Scheduling Algorithms 2 3. Basic Concepts Main objective of multiprogramming is to keep onrunning processes all the time for maximum CPUutilization. Scheduling is fundamental function of OS. The task of selecting the processes in memory thatare ready to execute, and allocating them to theCPU is performed by the CPU Scheduler. CPU Scheduling Algorithms 3 4. CPU Scheduler CPU scheduling decisions may take place when aprocess:o 1. Switches from running to waiting stateo 2. Switches from running to ready stateo 3. Switches from waiting to readyo 4. Terminates Scheduling under 1 and 4 is non preemptive. All other scheduling is preemptive.CPU Scheduling Algorithms 4 5. CONTCPU Scheduler NonpreemptiveOnce a process is allocated the CPU, it does notleave unless:o it has to wait, e.g., for I/O requesto it terminates Preemptiveo OS can force (preempt) a process from CPU atanytimeo E.g., to allocate CPU to another higher-priorityprocessCPU Scheduling Algorithms 5 6. Scheduling Criteria CPU utilization: keep the CPU as busy as possible Maximize Throughput: No of processes that complete theirexecution per time unit Maximize Turnaround time: amount of time to execute aparticular process (time from submission totermination) Minimize CPU Scheduling Algorithms 6 7. CONTScheduling Criteria Waiting time: amount of time a process has beenwaiting in the ready queue (sum of time waiting inready queue)o Minimize Response time amount of time it takes from whena request was submitted until the first response isproduced, notoutput(for time-sharingenvironment)o Minimize CPU Scheduling Algorithms 7 8. Scheduling Algorithms First Come, First Served Shortest Job First Priority Round Robin CPU Scheduling Algorithms 8 9. Implementation in C++ Class: cpuschedule Attributes: o n number of processes o Bu[ ] Array to store Burst Time o A[ ] Array to store Arrival Time o Wt[ ] Array to store Waiting Time o Twt Total Waiting Time o Awt Average Waiting Time CPU Scheduling Algorithms 9 10. CONTImplementation in C++ Operations: o Getdata() To get number of processes and Burst Times from the user o Fcfs() First Come, First Served Algorithm o Sjf() Shortest Job First (normal) Algorithm o SjfP() Shortest Job First (Preemption) Algorithm o SjfNp() Shortest Job First (non preemption) Algorithm o Priority() Priority Algorithm o RoundRobin() Round Robin AlgorithmCPU Scheduling Algorithms 10 11. First Come, First ServedProcess Burst TimeP124P2 3P33 Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is: P1 P2P302427 30 Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17 CPU Scheduling Algorithms 11 12. First Come FirstCONTServed Suppose that the processes arrive in the order :P2 , P3 , P1 (P1:24,P2:3,P3:3) The Gantt chart for the schedule is:P2 P3P103630 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 processCPU Scheduling Algorithms 12 13. Shortest Job First Normal SJF Process Burst Time P17 P23 P34 The Gantt Chart for SJF (Normal) is:P2P3P103 7 14 Average waiting time = (0 + 3 + 7)/3 = 3.33CPU Scheduling Algorithms 13 14. Shortest Job FirstCONT Non-Preemptive SJFProcessArrival TimeBurst TimeP1 0.0 7P22.0 4P34.0 1P45.0 4 The Gantt Chart for SJF (non-preemptive) is:P1P3 P2 P4 024 57812 16 Average waiting time = (0 + 6 + 3 + 7)/4 = 4CPU Scheduling Algorithms 14 15. Shortest Job First CONTPreemptive SJFProcessArrival Time Burst TimeP1 0.07P2 2.04P3 4.01P4 5.04 The Gantt Chart for SJF (preemptive) is:P1 P2 P3 P2 P4P102457 11 16 Averagewaiting time = (9 + 1 + 0 +2)/4 = 3 CPU Scheduling Algorithms 15 16. Shortest Job FirstCONT Associate with each process the length of its next CPUburst. Use these lengths to schedule the process with theshortest time. Two schemes:o Non-Preemptive: once CPU given to the process itcannot be preempted until completes its CPU burst.o Preemptive: if a new process arrives with CPU burstlength less than remaining time of current executingprocess, preempt. This scheme is know as theShortest-Remaining-Time-First (SRTF). SJF is optimal: gives minimum average waiting time for agiven set of processes.CPU Scheduling Algorithms 16 17. PriorityProcess Burst TimePriority P110 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 Gantt ChartP2 P5P1 P3P4016 1618 19 Average waiting time = (6 + 0 + 16 + 18 + 1)/5 = 8.2 CPU Scheduling Algorithms17 18. PriorityCONTA priority number (integer) is associated with eachprocess. Lager the CPU burst lower the priority. The CPU is allocated to the process with the highestpriority (smallest integer highest priority) Starvation (Infinity blocking): low priority processesmay never execute. Aging: as time progresses increase the priority of theprocess.CPU Scheduling Algorithms 18 19. Round Robin Process Burst Time P124 P2 3 P3 3 Quantumtime = 4 milliseconds The Gantt chart is: P1 P2 P3P1P1P1P1P1 047101418222630 Average waiting time = {[0+(10-4)]+4+7}/3 = 5.6 CPU Scheduling Algorithms 19 20. Round Robin CONT Typically, higher average turnaround than SJF, butbetter response Each process gets a small unit of CPU time (timequantum), usually 10-100 milliseconds. After thistime has elapsed, the process is preempted andadded to the end of the ready queue. Performanceo q large FCFSo q small q must be large with respect tocontext switch, otherwise overhead is too high CPU Scheduling Algorithms 20 21. Involvement of OSSource Code(.c)Compiler Conversion(.i , .o)Executable (.exe)Microsoft (load executable Micro-kernelWindowsdirectly to memory) MemoryCPUSchedulingAlgorithmsExecuteCPU Scheduling Algorithms21 22. CPUSchedulingAlgorithmsGroup No. 2Group No. 2 22


View more >