CPU Scheduling Algorithms (Report)
CPU Scheduling Algorithms (Report)
Submitted To: Mr. Kashif Alam [Lecturer, Lab Instructor Operating Systems] Submitted By: Amar Jeet [BECS/H/F10/0117] Uzair Ahmed [BECS/H/F10/0118]
Project Report
Operating Systems
1 | Abstract
This report, entitled CPU Scheduling Algorithms and their Implementation in Visual C++ Environment, contains a brief introduction to the basic algorithm techniques used for CPU scheduling and their implementation in Visual C++ environment. This report was put together by a group of 2 students as the fulfillment to the semester project assigned by Mr. Kashif Alam, lecturer Operating Systems at HIIT (Hamdard University, Main Campus, Karachi).
2 | Acknowledgment
We, the students who were assigned this report, hereby acknowledge that the material used/written in this report is not, wholly or partially, plagiarized; proper references have been mentioned where we used external printed/written/published material, and the instructor has the right to reject a portion of the report or the report altogether if he finds the fact to be something else than what has been mentioned above. We have tried our level best to keep the references correct and the facts & figures up-to-date, but being human, any error or lapses are expected and are requested to be tolerated.
Project Report
Operating Systems
Table of Contents
TOPIC 1 2 3 3.1 3.2 3.3 3.4 4 4.1 4.2 4.3 5 5.1 5.2 5.2a 5.2b 5.3 5.3a 5.3b 5.4 5.4a 5.4b 5.4c 5.5 5.5a 5.5b 5.6 5.6a 5.6b 5.6b Abstract Acknowledgment CPU SCHEDULING AN INTRODUCTION What is CPU Scheduling? Why do we need Scheduling? Goals of CPU Scheduling When to Schedule? TYPES OF SCHEDULING Long-term Scheduling Medium-term Scheduling Short-term Scheduling SCHEDULING ALGORITHMS Preemptive vs. Non-preemptive Algorithms FCFS Algorithm Advantages of FCFS Disadvantages of FCFS SJF/SRT Algorithms Advantages of SJF/SRT Disadvantages of SJF/SRT Round-Robin Algorithm The Quantum Issue Advantages of RR Disadvantages of RR Priority-based Scheduling Advantages of PBS Disadvantages of PBS MFQ Algorithm Working of MFQ Algorithm Advantages of MFQ Disadvantages of MFQ PAGE 1 1 4 4 4 4 4 5 5 5 5 6 7 7 7 8 8 8 9 9 9 10 10 10 10 10 11 11 11 11 contd
Project Report
Operating Systems
Project Report
Operating Systems
Project Report
Operating Systems
Third, when a process blocks on I/O, on a semaphore, or for some other reason, another process has to be selected to run. Sometimes the reason for blocking may play a role in the choice. For example, if A is an important process and it is waiting for B to exit its critical region, letting B run next will allow it to exit its critical region and thus let A continue. The trouble, however, is that the scheduler generally does not have the necessary information to take this dependency into account. Fourth, when an I/O interrupt occurs, a scheduling decision may be made. If the interrupt came from an I/O device that has now completed its work, some process that was blocked waiting for the I/O may now be ready to run. It is up to the scheduler to decide if the newly ready process should be run, if the process that was running at the time of the interrupt should continue running, or if some third process should run.
Project Report
Operating Systems
5 | SCHEDULING ALGORITHMS
Scheduling algorithms are the techniques used for distributing resources among parties which simultaneously and asynchronously request them. Scheduling disciplines are used in routers (to handle packet traffic) as well as in operating systems (to share CPU time among both threads and processes), disk drives (I/O scheduling), printers (print spooler), most embedded systems, etc. The main purposes of scheduling algorithms are to minimize resource starvation and to ensure fairness amongst the parties utilizing the resources. Scheduling deals with the problem of
Project Report
Operating Systems
deciding which of the outstanding requests is to be allocated resources. There are many different scheduling algorithms. In this section, we introduce several of them. There are several types of CPU scheduling algorithms, each having its own characteristics, advantages and disadvantages. We will discuss some of the most important and significant one of those in this report, namely: First-Come First-Served Shortest Job First (SJF) / Shortest Remaining Time (SRT) Round-Robin Priority-based Multi-level Feedback Queue
Before we move on to the algorithm techniques, it is necessary that we define the concept of preemption and non-preemption.
Project Report
Operating Systems
requires removing one from the front of the queue. Adding a new job or unblocked process just requires attaching it to the end of the queue. What could be simpler? 5.2-b | Disadvantages of FCFS: Unfortunately, FCFS also has a huge disadvantage, and it is that it pays no attention to processing time or prioritizes the processes. If a process arrives in the ready queue with processing time of 100ms and then several other processes arrive with processing time of 1ms each, the FCFS will start the 100ms process and other, much shorter processes will have to wait for 100ms. In this way, the turnaround time for FCFS can become significantly low as larger processes hog the resources.
Fig. 2 The working of SJF technique (a) Running four processes in original order (b) Running same processes in SJF order Take a look at the example of Fig. 2. In the original order, A runs first and hogs the system for shorter processes. If run in SJF order, the shorter jobs run first and A gets its turn last. 5.3-a | Advantages of SJF/SRT: The SJF/SRT algorithms do not have a bias in favor of longer processes as it happens in FCFS algorithms. It also provides the maximum throughput in most of the cases.
Project Report
Operating Systems
5.3-b | Disadvantages of SJF/SRT: Main disadvantages of SJF/SRT are that these algorithms need advanced knowledge of estimation and preemption. These aspects make it hard to implement these algorithms. Moreover, if a lot of short processes arrive and keep on arriving in an SRT-based system, the larger processes will suffer from starvation.
Fig. 3 Round-Robin Technique (a) The list of runnable processes (b) List of runnable processes after job B has used its Quantum (Time-slice) 5.4-a | The Quantum Issue: The only interesting issue with round robin is the length of the quantum. Switching from one process to another requires a certain amount of time for doing the administrationsaving and loading registers and memory maps, updating various tables and lists, flushing and reloading the memory cache, etc. Suppose that this process switch or context switch, as it is sometimes called, takes 1 msec, including switching memory maps, flushing and reloading the cache, etc. Also suppose that the quantum is set at 4 msec. With these parameters, after doing 4 msec of useful work, the CPU will have to spend 1 msec on process switching. Twenty percent of the CPU time will be wasted on administrative overhead. Clearly this is too much. To improve the CPU efficiency, we could set the quantum to, say, 100 msec. Now the wasted time is only 1 percent. But consider what happens on a timesharing system if ten interactive users hit the carriage return key at roughly the same time. Ten processes will be put on the list of runnable processes. If the CPU is idle, the first one will start immediately, the second one may not start until 100 msec later, and so on. The unlucky last one may have to wait 1 sec before getting a chance, assuming all the others use their full quanta. Most users will perceive a 1-sec response to a short command as sluggish.
Project Report
Operating Systems
Another factor is that if the quantum is set longer than the mean CPU burst, preemption will rarely happen. Instead, most processes will perform a blocking operation before the quantum runs out, causing a process switch. Eliminating preemption improves performance because process switches then only happen when they are logically necessary, that is, when a process blocks and cannot continue. 5.4-b | Advantages of RR: The biggest advantage that RR-algorithm has over other algorithm techniques is that it is fair to every process, giving all of them exactly the same time, and thus starvation is virtually impossible to happen. 5.4-c | Disadvantages of RR: The main problem with Round-Robin is that it assumes that all processes are equally important, thus each receives an equal portion of the CPU. This sometimes produces bad results. Consider three processes that start at the same time and each require three time slices to finish. A comparison of FIFO and RR in this case is explained by this figure:
Fig. 4 RR and FIFO (L.H.S.) Executing three processes with the FIFO algorithm (R.H.S.) Executing same processes with the RR algorithm The picture above describes the problem with RR algorithm. In the FIFO technique, Process A finishes after 3 slices; B, 6; and C, 9. The average is (3+6+9)/3 = 6 slices. But in the case of RR, Process A finishes after 7 slices; B, 8; and C, 9; so the average is (7+8+9)/3 = 8 slices. This implies that Round-Robin is fair, but uniformly inefficient in some of the cases.
10
Project Report
Operating Systems
11
Project Report
Operating Systems
12
Project Report
Operating Systems
13
Project Report
Operating Systems
Project Report
Operating Systems
15
Project Report
Operating Systems
Appendix B Bibliography
This appendix contains the references to the published/written/printed material that was referred during the compilation of this report.
Books
Operating Systems: Internals and Design Principles by W. Stallings Operating System Concepts by A. Silberschatz and P. B. Galvin
Other resources
MS Encarta Microsofts Encarta encyclopedia A Methodology for Comparing Service Policies Using a Trust Model a research paper by Henry Hexmoor, Southern Illinois University OS Concepts Compiled by Sami-ul-Haq
16