Introduction to CPU Scheduling | Operating System - Computer Science Engineering (CSE) PDF Download

CPU Scheduling in Operating Systems

Scheduling of processes/work is done to finish the work on time.

Below are different time with respect to a process.

  • Arrival Time: Time at which the process arrives in the ready queue.
  • Completion Time: Time at which process completes its execution.
  • Burst Time: Time required by a process for CPU execution.
  • Turn Around Time: Time Difference between completion time and arrival time.
    Turn Around Time = Completion Time – Arrival Time
  • Waiting Time(W.T): Time Difference between turn around time and burst time.
    Waiting Time = Turn Around Time – Burst Time

Why do we need scheduling?

A typical process involves both I/O time and CPU time. In a uni programming system like MS-DOS, time spent waiting for I/O is wasted and CPU is free during this time. In multi programming systems, one process can use CPU while another is waiting for I/O. This is possible only with process scheduling.

Objectives of Process Scheduling Algorithm

  • Max CPU utilization [Keep CPU as busy as possible]
  • Fair allocation of CPU.
  • Max throughput [Number of processes that complete their execution per time unit]
  • Min turnaround time [Time taken by a process to finish execution]
  • Min waiting time [Time a process waits in ready queue]
  • Min response time [Time when a process produces first response]

Different Scheduling Algorithms

  1. First Come First Serve (FCFS): Simplest scheduling algorithm that schedules according to arrival times of processes. First come first serve scheduling algorithm states that the process that requests the CPU first is allocated the CPU first. It is implemented by using the FIFO queue. When a process enters the ready queue, its PCB is linked onto the tail of the queue. When the CPU is free, it is allocated to the process at the head of the queue. The running process is then removed from the queue. FCFS is a non-preemptive scheduling algorithm.
    Note: First come first serve suffers from convoy effect.
  2. Shortest Job First (SJF): Process which have the shortest burst time are scheduled first.If two processes have the same bust time then FCFS is used to break the tie. It is a non-preemptive scheduling algorithm.
  3. Longest Job First (LJF): It is similar to SJF scheduling algorithm. But, in this scheduling algorithm, we give priority to the process having the longest burst time. This is non-preemptive in nature i.e., when any process starts executing, can’t be interrupted before complete execution.
  4. Shortest Remaining Time First (SRTF): It is preemptive mode of SJF algorithm in which jobs are schedule according to shortest remaining time.
  5. Longest Remaining Time First (LRTF): It is preemptive mode of LJF algorithm in which we give priority to the process having largest burst time remaining.
  6. Round Robin Scheduling: Each process is assigned a fixed time(Time Quantum/Time Slice) in cyclic way. It is designed especially for the time-sharing system. The ready queue is treated as a circular queue. The CPU scheduler goes around the ready queue, allocating the CPU to each process for a time interval of up to 1-time quantum. To implement Round Robin scheduling, we keep the ready queue as a FIFO queue of processes. New processes are added to the tail of the ready queue. The CPU scheduler picks the first process from the ready queue, sets a timer to interrupt after 1-time quantum, and dispatches the process. One of two things will then happen. The process may have a CPU burst of less than 1-time quantum. In this case, the process itself will release the CPU voluntarily. The scheduler will then proceed to the next process in the ready queue. Otherwise, if the CPU burst of the currently running process is longer than 1-time quantum, the timer will go off and will cause an interrupt to the operating system. A context switch will be executed, and the process will be put at the tail of the ready queue. The CPU scheduler will then select the next process in the ready queue.
    • Priority Based scheduling (Non-Preemptive): In this scheduling, processes are scheduled according to their priorities, i.e., highest priority process is scheduled first. If priorities of two processes match, then schedule according to arrival time. Here starvation of process is possible.
    • Highest Response Ratio Next (HRRN): In this scheduling, processes with highest response ratio is scheduled. This algorithm avoids starvation.
      Response Ratio = (Waiting Time + Burst time) / Burst time
    • Multilevel Queue Scheduling: According to the priority of process, processes are placed in the different queues. Generally high priority process are placed in the top level queue. Only after completion of processes from top level queue, lower level queued processes are scheduled. It can suffer from starvation.
    • Multi level Feedback Queue Scheduling: It allows the process to move in between queues. The idea is to separate processes according to the characteristics of their CPU bursts. If a process uses too much CPU time, it is moved to a lower-priority queue.

Some useful facts about Scheduling Algorithms:

  1. FCFS can cause long waiting times, especially when the first job takes too much CPU time.
  2. Both SJF and Shortest Remaining time first algorithms may cause starvation. Consider a situation when the long process is there in the ready queue and shorter processes keep coming.
  3. If time quantum for Round Robin scheduling is very large, then it behaves same as FCFS scheduling.
  4. SJF is optimal in terms of average waiting time for a given set of processes,i.e., average waiting time is minimum with this scheduling, but problems are, how to know/predict the time of next job.

Exercise:
Q.1. Which of the following is false about SJF?
S1: It causes minimum average waiting time
S2: It can cause starvation
(a) Only S1
(b) Only S2
(c) Both S1 and S2
(d) Neither S1 nor S2
Ans: d
Explanation: S1 is true SJF will always give minimum average waiting time.
S2 is true SJF can cause starvation.

Q.2. Consider the following table of arrival time and burst time for three processes P0, P1 and P2. (GATE-CS-2011)

Introduction to CPU Scheduling | Operating System - Computer Science Engineering (CSE)

The pre-emptive shortest job first scheduling algorithm is used. Scheduling is carried out only at arrival or completion of processes. What is the average waiting time for the three processes?
(a) 5.0 ms
(b) 4.33 ms
(c) 6.33
(d) 7.33
Ans: a
Explanation: Process P0 is allocated processor at 0 ms as there is no other process in the ready queue. P0 is preempted after 1 ms as P1 arrives at 1 ms and burst time for P1 is less than remaining time of P0. P1 runs for 4ms. P2 arrived at 2 ms but P1 continued as burst time of P2 is longer than P1. After P1 completes, P0 is scheduled again as the remaining time for P0 is less than the burst time of P2.
P0 waits for 4 ms, P1 waits for 0 ms and P2 waits for 11 ms. So average waiting time is (0 + 4 + 11) / 3 = 5.

Q.3. Consider the following set of processes, with the arrival times and the CPU-burst times given in milliseconds (GATE-CS-2004)

Introduction to CPU Scheduling | Operating System - Computer Science Engineering (CSE)

What is the average turnaround time for these processes with the preemptive shortest remaining processing time first (SRPT) algorithm?
(a) 5.50
(b) 5.75
(c) 6.00
(d) 6.25
Ans: a
Explanation:
The following is Gantt Chart of execution

Introduction to CPU Scheduling | Operating System - Computer Science Engineering (CSE)

Turn Around Time = Completion Time – Arrival Time
Avg Turn Around Time  =  (12 + 3 + 6 +  1) / 4 = 5.50

Q.4. An operating system uses the Shortest Remaining Time first (SRTF) process scheduling algorithm. Consider the arrival times and execution times for the following processes:

Introduction to CPU Scheduling | Operating System - Computer Science Engineering (CSE)

What is the total waiting time for process P2?
(a) 5
(b) 15
(c) 40
(d) 55
Ans: b
Explanation: At time 0, P1 is the only process, P1 runs for 15 time units.
At time 15, P2 arrives, but P1 has the shortest remaining time. So P1 continues for 5 more time units.
At time 20, P2 is the only process. So it runs for 10 time units
At time 30, P3 is the shortest remaining time process. So it runs for 10 time units
At time 40, P2 runs as it is the only process. P2 runs for 5 time units.
At time 45, P3 arrives, but P2 has the shortest remaining time. So P2 continues for 10 more time units.
P2 completes its execution at time 55
Total waiting time for P2 = Completion time - (Arrival time + Execution time)
= 55 - (15 + 25)
= 15

The document Introduction to CPU Scheduling | Operating System - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Operating System.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
10 videos|99 docs|33 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Introduction to CPU Scheduling - Operating System - Computer Science Engineering (CSE)

1. What is CPU scheduling in operating systems?
Ans. CPU scheduling in operating systems is the process of determining the order in which processes should be executed by the central processing unit (CPU). It determines which process gets to use the CPU and for how long, aiming to achieve maximum CPU utilization and fairness among processes.
2. Why is CPU scheduling important in operating systems?
Ans. CPU scheduling is important in operating systems because it helps in efficient utilization of the CPU. By scheduling processes effectively, the CPU can be kept busy executing tasks, reducing idle time and maximizing throughput. It also ensures fairness by providing equal opportunities for all processes to run.
3. What are the different types of CPU scheduling algorithms?
Ans. There are several types of CPU scheduling algorithms, including: - First-Come, First-Served (FCFS): Processes are executed in the order they arrive. - Shortest Job Next (SJN): The process with the shortest burst time is executed next. - Round Robin (RR): Each process gets a fixed time slice or quantum to execute. - Priority Scheduling: Processes are assigned priorities, and the highest priority process is executed next. - Multilevel Queue Scheduling: Processes are divided into multiple priority queues, and each queue has its own scheduling algorithm.
4. What factors are considered while selecting a CPU scheduling algorithm?
Ans. Several factors are considered when selecting a CPU scheduling algorithm, such as: - CPU utilization: The algorithm should aim for maximum CPU utilization. - Throughput: The number of processes completed per unit of time. - Turnaround time: The time taken for a process to complete from its arrival to its completion. - Waiting time: The time a process spends waiting in the ready queue. - Response time: The time taken for the first response from the system after a request is made.
5. What are the advantages and disadvantages of Round Robin scheduling?
Ans. The advantages of Round Robin scheduling include fair allocation of CPU time to processes, good response time for interactive tasks, and suitability for time-sharing systems. However, it may lead to higher waiting times for long processes, and the choice of time quantum can affect its performance. Additionally, it may not be efficient for CPU-bound tasks as the time quantum needs to be small enough for responsiveness, which can result in frequent context switches.
10 videos|99 docs|33 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

study material

,

shortcuts and tricks

,

Objective type Questions

,

Important questions

,

Viva Questions

,

Extra Questions

,

MCQs

,

ppt

,

video lectures

,

Introduction to CPU Scheduling | Operating System - Computer Science Engineering (CSE)

,

Exam

,

Previous Year Questions with Solutions

,

Semester Notes

,

mock tests for examination

,

Free

,

Sample Paper

,

Summary

,

Introduction to CPU Scheduling | Operating System - Computer Science Engineering (CSE)

,

Introduction to CPU Scheduling | Operating System - Computer Science Engineering (CSE)

,

pdf

,

practice quizzes

,

past year papers

;