BCE-C712 Linux System Administration

0 of 75 lessons complete (0%)

Starting up and Shutting Down

The Scheduler in Linux

You don’t have access to this lesson

Please register or sign in to access the course content.

The scheduler in Linux is a core component of the kernel that determines how processes (or tasks) are allocated CPU time. Since a system’s CPU can only handle one task at a time per core, the scheduler’s job is to manage the time each process spends running on the CPU. It ensures that all processes get a fair share of CPU resources while maintaining system performance and responsiveness.

What is Scheduling?

Scheduling in computing refers to the method by which the operating system decides which tasks (or processes) should be executed by the CPU and in what order. In Linux, the scheduler decides how long each task runs before being paused to allow another task to run. This switching happens so quickly that it appears as if multiple tasks are running simultaneously (multitasking).

Types of Scheduling in Linux:

  1. Preemptive Scheduling:
    • In preemptive scheduling, the operating system can interrupt a running process to switch the CPU to another process that needs more urgent attention.
    • This ensures that higher-priority tasks are given more immediate access to the CPU.
  2. Non-Preemptive Scheduling:
    • In non-preemptive scheduling, once a process starts running, it continues to run until it either completes or voluntarily yields control of the CPU.
    • This type is less common in modern operating systems because it can lead to inefficiencies if a process takes too long.

Scheduling Algorithms:

Linux uses different scheduling algorithms to manage various types of processes. These algorithms are part of the kernel’s scheduler.

  1. Completely Fair Scheduler (CFS):
    • The default scheduler in Linux, CFS aims to allocate CPU time fairly among all processes.
    • CFS operates on the concept of “fairness,” trying to ensure that each process gets an equal share of the CPU, relative to its priority and other processes.
    How it Works:
    • CFS maintains a virtual runtime for each process, which tracks how much CPU time it has used.
    • The scheduler picks the process with the smallest virtual runtime (the one that has had the least CPU time) to run next, ensuring fairness.
    Example:
    • If you’re running multiple programs, such as a browser, a text editor, and a file download, CFS tries to ensure that all these processes get CPU time proportionally to their needs, so one process doesn’t hog the CPU.
  2. Real-Time Scheduling:
    • Linux also supports real-time scheduling, which is crucial for tasks that require immediate or predictable responses, such as controlling machinery or handling multimedia streaming.
    • Real-time tasks have higher priority than normal tasks and are scheduled using fixed-priority policies like Round-Robin (RR) and First-Come, First-Served (FCFS).
    Example:
    • In a video streaming application, real-time scheduling ensures that video frames are processed in time to avoid delays or glitches.
  3. Batch Scheduling:
    • Batch processes are tasks that can run without user interaction and typically do not require immediate CPU access. These tasks are often scheduled with lower priority.
    • The scheduler might use algorithms like Completely Fair Queuing (CFQ) for batch processes, ensuring they don’t interfere with interactive tasks.
    Example:
    • A large data analysis job running in the background can be scheduled as a batch process, so it doesn’t slow down your active work on the system.

Factors Influencing Scheduling:

  1. Process Priority:
    • Each process in Linux is assigned a priority, determining how much CPU time it should get relative to others.
    • Higher-priority processes are scheduled before lower-priority ones.
    • Nice Values: Users can influence a process’s priority using the nice command, where a lower “nice” value means higher priority.
    Example:
    • Running a critical task, like compiling code, with a lower nice value will ensure it gets more CPU time than a background task like file synchronization.
  2. Load Balancing:
    • In multi-core systems, the scheduler also balances the load across different CPU cores to optimize performance.
    • The goal is to ensure that no single core is overburdened while others are idle.
    Example:
    • If you’re running multiple programs, the scheduler distributes them across all available CPU cores, so no single core is overwhelmed.
  3. Time Slices:
    • The scheduler allocates a time slice (a small amount of time) to each process. After its time slice expires, the process is paused, and the CPU is given to the next process in line.
    • Time slices are typically short, so users perceive the system as responsive, even when multiple processes are running.
    Example:
    • If you’re typing in a text editor and downloading a file simultaneously, each process gets a small slice of CPU time. The alternation between these tasks is so fast that it seems like they’re running at the same time.

Example: Understanding Scheduling with a Practical Scenario

Imagine you’re working on a Linux system where you are compiling code, browsing the web, and running a backup in the background:

  1. Fair CPU Allocation:
    • The scheduler ensures that your web browser remains responsive while also giving enough CPU time to the code compiler and the backup process.
  2. Real-Time Needs:
    • If you start a video call, the scheduler prioritizes the processes related to the call to ensure smooth video and audio playback, without interrupting your code compilation.
  3. Background Processes:
    • The backup process is scheduled with lower priority because it doesn’t need immediate attention. The scheduler ensures it runs without affecting your active tasks.

Why the Scheduler is Important:

  1. System Responsiveness:
    • The scheduler ensures that interactive tasks, like typing or browsing, remain responsive even when the system is under load.
  2. Efficient Resource Utilization:
    • By balancing the CPU load across processes and cores, the scheduler optimizes the use of system resources, leading to better performance.
  3. Fairness:
    • The scheduler ensures that no single process monopolizes the CPU, giving all processes a fair chance to run, according to their needs and priorities.