CPU Scheduling in Operating System

In this article, we will learn about CPU scheduling in operating system.
CPU scheduling is a critical aspect of modern operating systems that play a vital role in managing the allocation of resources in a computer system. It is the process by which the operating system decides which program or process should be executed by the CPU at any given time. The goal of CPU scheduling is to ensure that the CPU is used efficiently and that all programs and processes are given a fair share of the CPU’s resources. The appropriate scheduling algorithm will depend on the specific requirements of the system and the types of processes being run.
But before moving on to the types of scheduling algorithms in OS, let’s first see why CPU scheduling in OS is needed-

Why is CPU Scheduling in OS Needed:

In this section, we will see why CPU scheduling in operating system need
The main function of a CPU scheduling algorithm is to determine which process should be executed by the CPU at any given time. This is done by allocating a certain amount of time, called a time slice or quantum, to each process. The process that is currently using the CPU will be preempted and moved to the back of the ready queue after its time slice has expired, allowing the next process in the queue to begin execution.

One of the main advantages of using a CPU scheduling algorithm is that it can help to prevent a single process from monopolizing the CPU, which can cause other processes to be starved of resources and slow down the entire system. This is particularly important in multi-user systems, where many processes may be running simultaneously.

There are several different CPU scheduling algorithms that can be used, each with its own strengths and weaknesses. Some of the most popular algorithms include First-Come, First-Served (FCFS), Shortest Job First (SJF), Round Robin (RR), Priority Scheduling, and Multi-level Queue Scheduling.
Now, moving further in this article we will see various times related to the process in OS and some of the types of CPU scheduling algorithms in the operating system

Various Times related to the Process in OS:

A process is a program in execution, and there are several different times associated with a process that is important to understand when working with operating systems. These times include:

  1. Arrival Time: This is the time at which a process arrives in the system and is ready to be executed.
  2. Burst Time: This is the amount of time that a process needs to execute its next instruction or complete its next task.
  3. Waiting Time: This is the amount of time that a process spends waiting in the ready queue for the CPU to become available.
  4. Turnaround Time: This is the total time that elapses from the time a process arrives in the system to the time it completes execution. It is the sum of the burst time and the waiting time.
  5. Response Time: This is the amount of time that elapses from the time a process submits a request to the time it receives a response.
  6. Execution Time: This is the amount of time that a process spends executing on the CPU.

Each of these times can provide important information about the performance of a process and the overall system. For example, a high waiting time may indicate that the system is overloaded, while a low turnaround time may indicate that a process is executing quickly and efficiently.

In addition to understanding the different times related to a process, it is also important to understand how these times are affected by different CPU scheduling algorithms. For example, the SJF algorithm prioritizes processes based on their burst time and can lead to shorter waiting times and turnaround times, while the RR algorithm uses a time slice or quantum to ensure that all processes are given a fair share of the CPU’s resources, which can lead to lower response times.

Types of Scheduling Algorithms in OS:

There are several different algorithms used for CPU scheduling, each with its own strengths and weaknesses. Some of the most popular algorithms are discussed below:

  1. First-Come, First-Served (FCFS): One of the most basic types of scheduling algorithms is the first-come, first-served (FCFS) algorithm. This algorithm is the simplest form of CPU scheduling. It simply executes processes in the order that they arrive in the ready queue. While this algorithm is easy to implement, it can lead to poor performance if a process with a large CPU burst arrives just before a process with a small CPU burst because this algorithm allocates the CPU to the process that arrives first, regardless of the process’s priority or resource requirements

  2. Shortest Job First (SJF): This algorithm does CPU scheduling in operating system which allocates the CPU to the process with the shortest estimated execution time, in an effort to minimize the amount of time a process spends waiting for the CPU and to maximize CPU utilization. However, this algorithm can be difficult to implement as it requires accurate estimates of the execution time for each process. SJF can be further divided into two types, non-preemptive SJF and preemptive SJF. In non-preemptive SJF, once the CPU is allocated to a process, it cannot be taken away. In contrast, in preemptive SJF, the CPU can be taken away from a process if a shorter process arrives.

    Note – This algorithm prioritizes processes based on the length of their CPU burst. The process with the shortest burst time is executed first, followed by the process with the next shortest burst time, and so on. While this algorithm can lead to better performance than FCFS, it can be difficult to predict the length of a process’s CPU burst.

  3. Round Robin (RR): A third commonly used algorithm that does CPU scheduling in operating system from the types of scheduling algorithms is the round-robin algorithm. This algorithm allocates the CPU to each process for a fixed time slice, known as a time quantum. After the time quantum expires, the process is moved to the end of the queue and the next process in the queue is given the CPU. This algorithm is designed to provide a fair allocation of resources among all processes, but it can lead to poor performance if the time quantum is set too high. The round-robin algorithm is often used in time-sharing systems such as multi-tasking environments. The time quantum is usually set to a small value such as 10-100 milliseconds, which ensures that all processes get a fair share of the CPU.

    Note – This algorithm is similar to FCFS, but it uses a time slice or quantum to limit the amount of time that a process can execute before it is moved to the back of the ready queue. This helps to prevent a process from monopolizing the CPU for too long.

  4. Priority Scheduling: Another important algorithm from types of scheduling algorithms is priority scheduling. In this algorithm, each process is assigned a priority, and the process with the highest priority is allocated the CPU. Priority scheduling can be further divided into two types: non-preemptive priority scheduling and preemptive priority scheduling.

    • In non-preemptive priority scheduling, once the CPU is allocated to a process, it cannot be taken away.
    • whereas in preemptive priority scheduling, the CPU can be taken away from a process if a higher priority process arrives.

    If two or more processes have the same priority, they are executed using one of the other algorithms, such as FCFS or RR.

  5. Multi-level Queue Scheduling: this algorithm divides the ready queue into multiple smaller queues, each with its own scheduling algorithm. Processes are placed into the appropriate queue based on their priority or other criteria and are then executed according to the scheduling algorithm used by that queue.

  6. Multi-level Feedback Queue Scheduling: it is an extension of multi-level queue scheduling. In this algorithm, processes are also divided into different queues, but the scheduling algorithm used for each queue is different. The scheduling algorithm used for a particular queue is based on the behavior of the processes in that queue. Processes that have used a lot of CPU time are moved to a lower-priority queue, while processes that have used little CPU time are moved to a higher-priority queue.

Each of these algorithms has its own advantages and disadvantages, and the choice of which algorithm to use will depend on the specific requirements of the operating system and the type of workload that it is expected to handle.

Summary:
In this article we have learned, CPU scheduling in os is a crucial component of modern operating systems that ensures the efficient usage of CPU resources. Different algorithms exist and they each have their advantages and disadvantages. Factors such as the workload, the system requirements, and the specific use case should be taken into account when choosing the appropriate algorithm.

Leave a Reply

Your email address will not be published. Required fields are marked *