CPU Scheduling Algorithms (2024)

On a single-core processor, a CPU can only execute one instruction at a time. But what if multiple processes (computer programs) all want to run at the same time? This is known as multitasking, and for it to work, the OS must let each program in turn run a certain number of instructions on the CPU, swapping between them in short bursts. The OS will have to control how CPU time (and hence number of CPU instructions) is allocated between different processes - it will have to schedule the different processes.

On a single-core processor, a CPU can only execute one instruction at a time. But what if multiple processes (computer programs) all want to run at the same time? This is known as multitasking, and for it to work, the OS must let each program in turn run a certain number of instructions on the CPU, swapping between them in short bursts. The OS will have to control how CPU time (and hence number of CPU instructions) is allocated between different processes – it will have to schedule the different processes.

Scheduling Algorithms: An Overview

There are many different algorithms for scheduling computer programs, with different advantages and disadvantages. In all cases, we want to make sure that each program gets a fair amount of processor time, so that all of the programs have a chance to run and complete. We also want to make sure that we make the best use of our computing resources: for example, we don’t want a program to be blocking the use of the CPU while it waits for a file to be slowly read into RAM from a hard disk.

Here are a few common scheduling algorithms.

Want to keep learning?This content is taken from Raspberry Pi Foundation online course, Understanding Computer SystemsView Course

First Come, First Served (FCFS)

Whichever program is added to the queue first is run until it finishes. Then the next program in the queue is run, and so on.

Shortest Job Next (SJN)

For this algorithm, the OS needs to know (or guess) the time each program will take to run. It picks the program which will take the shortest amount of time as the one to run next.

Priority Scheduling

The OS assigns each program a priority. This could be based on how much memory they take, or how important they are for maintaining a responsive user interface. The program with the highest priority is picked to run next.

Shortest Remaining Time

This is similar to Shortest Job Next, except that if a new program is started, the OS compares the time it needs with the time the currently running program has left. If the new program would finish sooner, then the currently running program is switched out and the CPU starts processing the new program.

Round Robin (RR) scheduling

Time on the CPU is divided into equal parts called “time slices”. Time slices are allocated to each program equally and cyclically. This means that if we had a list of three programs running, the CPU would run:

  • Program 1 for one time slice
  • Program 2 for one time slice
  • Program 3 for one time slice

and would repeat this order until one of the programs finished.

Multilevel Queues

In this algorithm, programs are split into different queues by type — for example, system programs, or interactive programs. The programs of each type form a “queue”.

    • One algorithm will determine how CPU time is split between these queues. For example, one possibility is that the queues have different priorities, and programs in higher priority queues always run before those in lower priority queues (similar to priority scheduling).
    • For each queue, a different queue scheduling algorithm will decide how the CPU time is split within that queue. For example, one queue may use Round Robin scheduling, while another uses priority scheduling.

Analogy: A Real-Life Queue!

Most of these algorithms can be compared to different ways of managing a real-life queue, such as a school dinner queue. The most common way to serve school dinners is First Come First Served, but other methods are possible. The queue could prioritise those who want cold food, as they won’t take up much serving time (Shortest Job Next), or those who need to get to a lunch club (priority scheduling). Alternatively, different classes may be called to queue up for lunch at different times (multilevel queueing, with priority queueing between classes and FCFS within classes).

You may find it useful to ask students which of these they think are fairest, and why, before explaining the link to CPU scheduling.

Unplugged activity

The different scheduling algorithms can be demonstrated with an unplugged activity. Split students into groups of about five. In each group, one person will represent the CPU, while the others will represent different programs.

Take a pack of playing cards, and pass each “program” player a different number of playing cards (to simulate programs that require a lot of processor time and programs that are quick).

The person representing the CPU should then choose a scheduling algorithm to implement, and collect all the playing cards, one at a time, to represent allocating slices of CPU time. For example, for Round Robin scheduling, they would take one card at a time from each other group member in turn.

Once they have completed all the jobs, another person in the group should take the role of the CPU. The playing cards should be redistributed amongst the others in the group and the CPU should simulate a different algorithm.

You may want to use the suits to represent different programs to make it clearer what the CPU is currently running.

Ask your students the following.

  • For each algorithm, how long did it take to get a short job done? How about a long job?
  • Which algorithm is the most efficient for a long job / a short job / a job that arrives first?
  • Overall, what are the pros and cons of each algorithm?

You may want to give this a try yourself beforehand to develop your understanding of the algorithms.

Thoughts

When do you think each algorithm might be used in the real world?

Want to keep learning?This content is taken from Raspberry Pi Foundation online courseUnderstanding Computer SystemsView Course
CPU Scheduling Algorithms (2024)

FAQs

Which is the most efficient CPU scheduling algorithm? ›

Some of the well known efficient CPU scheduling algorithms are Round Robin scheduling (RR), Shortest job first(SJF), Preemptive version of SJF(SRTF), First Come First Serve (FCFS) etc.

What is the optimal CPU scheduling algorithm? ›

Shortest Job First (SJF) is an optimal scheduling algorithm as it gives maximum Throughput and minimum average waiting time(WT) and turnaround time (TAT) but it is not practically implementable because the burst time of a process can't be predicted in advance.

How many CPU scheduling algorithms are there? ›

There are two main types of CPU scheduling, preemptive and non-preemptive. Preemptive scheduling is when a process transitions from a running state to a ready state or from a waiting state to a ready state. Non-preemptive scheduling is employed when a process terminates or transitions from running to waiting state.

What are the disadvantages of SRTF? ›

Disadvantages
  • The context switch is done a lot more times significantly in SRTF than in SJN and consumes the CPU's important time for handling. ...
  • It has the potential for process starvation Starvation or indefinite blocking is a phenomenon associated with the Preemptive scheduling algorithms.

Which CPU scheduling algorithm is best and why? ›

The FCFS is better for a small burst time. The SJF is better if the process comes to processor simultaneously. The last algorithm, Round Robin, is better to adjust the average waiting time desired. Round Robin quantum time will set it towards more SJF or FCFS value.

What is the most popular scheduling algorithm? ›

There are a number of popular process scheduling algorithms teams use to optimize businesses processes:
  • First Come First Served (FCFS)
  • Shortest Job First (SJF)
  • Round Robin Scheduling.
  • Priority Scheduling.
  • Shortest Time Remaining.
Jun 13, 2023

What is the shortest job for CPU scheduling algorithm? ›

Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive) The shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next. SJN, also known as Shortest Job Next (SJN), can be preemptive or non-preemptive.

Which CPU scheduling algorithm works with highest waiting time? ›

In FCFS, the Average Waiting Time is comparatively high. FCFS is a Non-Preemptive CPU scheduling algorithm, which means after allocating the process to the CPU, it will release the CPU only after it finishes executing the process.

Which CPU scheduling algorithm causes starvation? ›

Generally, Starvation occurs in Priority Scheduling or Shortest Job First Scheduling. In the Priority scheduling technique, we assign some priority to every process we have, and based on that priority, the CPU will be allocated, and the process will be executed.

What is the fastest scheduling algorithm? ›

Shortest Job First (SJF)

Starting with the Advantages: of Shortest Job First scheduling algorithm. According to the definition, short processes are executed first and then followed by longer processes. The throughput is increased because more processes can be executed in less amount of time.

Which scheduling algorithm is most appropriate for real-time systems? ›

Rate-monotonic scheduling algorithm (RM) is by far the most used real-time algorithm and it is one of the easiest policies to implement. RM is a static-priority scheduling algorithm for real-time systems [5] . It is a preemptive algorithm that assigns higher priorities to the tasks with shorter periods Ti. ...

Which CPU scheduling algorithm does Windows use? ›

In the Windows operating system, the default job scheduling algorithm used is the Multilevel Feedback Queue algorithm. Windows also supports a subset of scheduling algorithms through its Windows Scheduler API, including First Come First Served (FCFS), Shortest Job First (SJF), Round Robin, and priority scheduling.

Can SRTF scheduling cause starvation? ›

Shortest remaining time first scheduling may cause starvation: True. In the shortest remaining time first scheduling policy, the process with the shortest remaining time gets the CPU next. However, if short processes keep coming, a long process may never get the CPU, causing starvation.

Is there starvation in SRTF? ›

Like shortest job next, it has the potential for process starvation: long processes may be held off indefinitely if short processes are continually added.

Is SRTF a preemptive algorithm? ›

SJF algorithm is generally used in the case of the non-preemptive kernel. We pick the shortest job and process it. But the SRTF algorithm is used in the case of the preemptive kernel. We pick the shortest job, which can be preempted in case a further shorter one arrives.

What is the most efficient algorithm efficiency? ›

The highest efficiency level in algorithm complexity is the O(1) or constant time complexity. An algorithm with O(1) complexity is highly efficient, as its performance does not depend on the size of the input data. It takes the same amount of time to execute, regardless of the input size.

Which scheduling algorithm is fastest? ›

Shortest Job First (SJF)

Starting with the Advantages: of Shortest Job First scheduling algorithm. According to the definition, short processes are executed first and then followed by longer processes. The throughput is increased because more processes can be executed in less amount of time.

Which disk scheduling algorithm is better? ›

LOOK disk scheduling algorithm can handle requests more effectively than SSTF. SSTF Scheduling has higher throughput and lower response time than FCFS Policy. The main advantage of this scheduling is that it only performs sweeps large enough to service all requests.

Top Articles
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 6047

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.