Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Operating Systems Part of E1.9 - “Principles of Computers and Software Engineering” Lecture 4: Process scheduling Objectives To introduce process scheduling algorithms Non-priority based Priority based Examine their advantages and disadvantages E1.9 - Operating systems (Lect 4) 2 Concepts (1) Bursts Processes consist of CPU and I/O bursts. Majority of processes alternate between the two. CPU-bound processes spend most of their time computing, while I/O-bound spend most of their time waiting for I/O. E1.9 - Operating systems (Lect 4) 3 Concepts (2) Scheduler and interrupts Short-term scheduling is invoked by interrupts Timer interrupts: they are set by the OS (to regain control regularly) Hardware interrupts System call interrupts Short-term scheduling is invoked to decide which process (from the ready queue) will be dispatched to the processor E1.9 - Operating systems (Lect 4) 4 Concepts (3) Preemptive and non-preemptive scheduling A non-preemptive scheduling algorithm selects a process and allows it to run until: It blocks (e.g. asks for I/O) It voluntarily releases the CPU It terminates Requires cooperative processes and users A preemptive scheduling algorithm selects a process and allows it to run for a maximum of some fixed time. Uses the timer interrupts to regain control E1.9 - Operating systems (Lect 4) 5 Concepts (4) Short-term Scheduling criteria Vary according to particular needs, but usually Maximize: CPU utilization Throughput, i.e. number of processes completed per unit of time Fairness Minimize: Response time (time from submission of request till first response) Waiting time (time a process is waiting for the CPU) Turnaround time (time it takes a process to complete) E1.9 - Operating systems (Lect 4) 6 Scheduling algorithms (1) Overview (1) We will examine the following algorithms: First-come, first served (FCFS) Shortest job first (SJF) Shortest remaining job first (SRJF) Round-robin (RR) Priority scheduling (PS) Multilevel queue scheduling (MQS) Multilevel feedback queue scheduling (MFQS) E1.9 - Operating systems (Lect 4) Non priority Priority based 7 Scheduling algorithms (1) Overview (2) We will use the following example in order to compare the algorithms: Process A B C D E Processing Time 3s 6s 4s 5s 2s Arrival Time 0s 2s 4s 6s 8s Example and Gantt charts courtesy of Daniel Ruckert E1.9 - Operating systems (Lect 4) 8 Scheduling algorithms (2) First-come, first-served (1) Algorithm description CPU is allocated to the process that requests it first Advantages Simple implementation (FIFO queue) Disadvantages Can result in long waits for short processes, and for I/Obound processes E1.9 - Operating systems (Lect 4) 9 Scheduling algorithms (2) First-come, first-served (2) Process A (3s) Process B (6s) Process C (4s) Process D (5s) Process E (2s) 5s 10 s 15 s 20 s Average Waiting time = (0 + 1 + 5 + 7 + 10) / 5 = 4.6s Average Turnaround time = (3 + 7 + 9 + 12 + 12) / 5 = 8.6s E1.9 - Operating systems (Lect 4) 10 Scheduling algorithms (3) Shortest Job First (1) Algorithm description CPU is allocated to the process that has the shortest next CPU burst Advantages Provably optimal in that it gives the minimum average waiting time for a given set of processes Disadvantages Knowing the length of next CPU burst is difficult; frequently this is predicted utilising previous lengths as estimates, or is user-specified. Can result in long waits for long processes E1.9 - Operating systems (Lect 4) 11 Scheduling algorithms (3) Shortest Job First (2) Process A (3s) Process B (6s) Process C (4s) Process D (5s) Process E (2s) 5s 10 s Average Waiting time = (0 + 1 + 7 + 9 + 1) / 5 = 3.6s Average Turnaround time = (3 + 7 + 11 + 14 + 3) / 5 = 7.6s E1.9 - Operating systems (Lect 4) 15 s 20 s 12 Scheduling algorithms (4) Shortest Remaining Job First (1) Algorithm description Variation of SJF, adding preemption; CPU again is allocated to the process that has the shortest next CPU burst; if a new process comes along that has a shorter CPU burst than the remaining one in the running process, the running process is removed and the new process is allocated the CPU. Advantages Allows new short jobs to get a good service Good handling of interactive processes since it results in a short response time Disadvantages Same as SJF Plus, this algorithm can be particularly bad for long processes (starvation). E1.9 - Operating systems (Lect 4) 13 Scheduling algorithms (4) Shortest Remaining Job First (2) Process A (3s) Process B (6s) Process C (4s) Process D (5s) Process E (2s) 5s 10 s Average Waiting time = (0 + 7 + 0 + 9 + 0) / 5 = 3.2s Average Turnaround time = (3 + 13 + 4 + 14 + 2) / 5 = 7.2s E1.9 - Operating systems (Lect 4) 15 s 20 s 14 Scheduling algorithms (5) Round Robin (1) Algorithm description The CPU is allocated to a process for a time quantum (or time-slice); if process is still running at the end of the quantum, CPU is preempted and given to the next process. Preempted process is put at the end of the queue. New processes are added at the end of the queue. Advantages Simple to implement and fair Disadvantages Difficult to determine appropriate time quantum: Too small: good response time, but large overheads (scheduler is called too often) Too large: bad response time E1.9 - Operating systems (Lect 4) 15 Scheduling algorithms (5) Round Robin (2) Time-slice of 1s Process A (3s) Process B (6s) Process C (4s) Process D (5s) Process E (2s) 5s 10 s 15 s 20 s Average Waiting time = (1 + 10 + 9 + 9 + 5) / 5 = 6.8s Average Turnaround time = (4 + 16 + 13 + 14 + 7) / 5 = 10.8s E1.9 - Operating systems (Lect 4) 16 Scheduling algorithms (5) Round Robin (3) Time-slice of 4s Process A (3s) Process B (6s) Process C (4s) Process D (5s) Process E (2s) 0 2s 5s 8s 10 s Average Waiting time = (0 + 9 + 3 + 9 + 9) / 5 = 6 s Average Turnaround time = (3 + 15 + 7 + 14 + 11) / 5 = 10 s E1.9 - Operating systems (Lect 4) 15 s 20 s 17 Scheduling algorithms (5) Comparison Algorithm Average waiting time Average turnaround time Type FCFS 4.6 s 8.6 s Nonpreemptive SJF 3.6 s 7.6 s Nonpreemptive SRJF 3.2 s 7.2 s Preemptive RR 6s 10 s Preemptive 6.8 s 10.8 s Preemptive (Time slice of 4s) RR (Time slice of 1s) E1.9 - Operating systems (Lect 4) 18 Scheduling algorithms (6) Priority-based algorithms Algorithms shown so far assume all processes are of equal importance. Frequently, we need to incorporate levels of priority to the different processes that request the CPU to indicate their relative importance. We will examine: Priority scheduling Multilevel queue scheduling Multilevel feedback queue scheduling E1.9 - Operating systems (Lect 4) 19 Scheduling algorithms (7) Priority scheduling (1) Algorithm description A priority is associated with each process; CPU is allocated to the process with the highest priority. FCFS is used to resolve situations involving processes with equal priority. There are two versions, preemptive and non-preemptive. Priority can be static, or dynamic. Advantages Takes into account external factors regarding the importance of the various processes Disadvantages Might result in starvation of low-priority processes – this can be avoided using an aging procedure; processes that wait for too long have their priorities gradually increased. E1.9 - Operating systems (Lect 4) 20 Scheduling algorithms (7) Priority scheduling (2) The following priorities are added to demonstrate this algorithm (smaller numbers indicate higher priority): Process A B C D E Processing Time 3s 6s 4s 5s 2s Arrival Time 0s 2s 4s 6s 8s E1.9 - Operating systems (Lect 4) Priority 3 5 4 2 0 21 Scheduling algorithms (7) Priority scheduling (3) Non-preemptive version Process A (3s) – Priority 3 Process B (6s) – Priority 5 Process C (4s) – Priority 4 Process D (5s) – Priority 2 Process E (2s) – Priority 0 5s 10 s Average Waiting time = (0 + 1 + 13 + 5 + 1) / 5 = 4 s Average Turnaround time = (3 + 7 + 16 + 10 + 3) / 5 = 7.8 s E1.9 - Operating systems (Lect 4) 15 s 20 s 22 Scheduling algorithms (7) Priority scheduling (4) Preemptive version Process A (3s) – Priority 3 Process B (6s) – Priority 5 Process C (4s) – Priority 4 Process D (5s) – Priority 2 Process E (2s) – Priority 0 5s 10 s Average Waiting time = (0 + 12+ 7 + 2 + 0) / 5 = 4.2 s Average Turnaround time = (3 + 18 + 11 + 7 + 2) / 5 = 8.4 s E1.9 - Operating systems (Lect 4) 15 s 20 s 23 Scheduling algorithms (8) Multilevel queue scheduling (MQS) Algorithm description Ready processes are divided into several separate queues; each queue has a priority associated with it. Queues with higher priority can be given more time-slices for each process. Different scheduling algorithms can be used for scheduling within queues. E.g. separate ready-queue in foreground (for interactive processes) and background (for batch processes) queues. Assign low priority to background queue and high to foreground queue. Implement RR on the foreground queue and FCFS to background queue. Advantages Flexible; allows more fine control of scheduling Disadvantages Does not allow for possibility of processes that change requirements throughout their execution (e.g. a process that started with a long CPU-burst, but requires interaction later – such process will be penalised indefinitely) E1.9 - Operating systems (Lect 4) 24 Scheduling algorithms (9) Multilevel feedback queue scheduling (MFQS) Algorithm description Same with MQS, but processes are allowed to move between queues For example, if a process uses too much CPU it will be moved to a lower-priority queue; if a process is waiting for too long in a lower priority queue, it can moved to a higher priority queue. Advantages Same with MQS, with the added adaptability that feedback brings This is the most general scheme you can have Disadvantages It is also the most complex – have to decide on several issues: Number of queues and the scheduling algorithm for each queue Method used to determine how to promote or demote a process Method used to determine which queue a process will enter Tuning of several parameters will be required to define the best scheduler E1.9 - Operating systems (Lect 4) 25 Summary Several process scheduling algorithms were presented Can be divided in non-priority and priority-based algorithms Their characteristics (incl. average waiting time and average turnaround time) were examined Next lecture: Process synchronization E1.9 - Operating systems (Lect 4) 26 Recommended Reading Read chapter 6 of Silberschatz’s book Read chapter 2, section 2.5 of Tanenbaum’s book Read chapter 9 of Stallings’ book E1.9 - Operating systems (Lect 4) 27