Process Scheduling & States in Linux

Process Scheduling & States in Linux

INTRODUCTION

Process scheduling is at the heart of how Linux manages system resources, determining which process gets to use the CPU and when. For system administrators, developers, and performance analysts, understanding how the Linux scheduler works—and how processes transition between different states—is essential for optimizing performance and ensuring system responsiveness. This guide explores the various process states in Linux, the scheduling policies used by the kernel (including real-time and normal policies), and tools that can be used to monitor and control process behavior. Whether you’re debugging performance issues or tuning a system for specific workloads, a solid grasp of Linux process scheduling is key.

  1. Process States in Linux

    A process in Linux can be in one of the following states:

    1. Running (R) – The process is either executing on the CPU or ready to run.
    2. Sleeping (S & D)
      • Interruptible Sleep (S) – The process is waiting for an event and can be woken up by signals.
      • Uninterruptible Sleep (D) – The process is waiting for I/O and cannot be interrupted.
    3. Stopped (T) – The process is paused, usually due to signals like SIGSTOP or SIGTSTP.
    4. Zombie (Z) – The process has completed execution but still has an entry in the process table because its parent has not retrieved its exit status.
    5. Idle (I) – Used by kernel threads that are not actively doing any work.

    You can view process states using the ps command:

       ps aux

    Process Scheduling in Linux

    Linux uses a priority-based scheduling mechanism. The scheduler decides which process gets CPU time based on scheduling policies and priorities.

    Types of Scheduling Policies

    Linux supports several scheduling policies, divided into real-time and normal policies.

    1. Normal Scheduling Policies

    These are used for standard user-space processes:

    • SCHED_OTHER: Default time-sharing policy, using the Completely Fair Scheduler (CFS).
    • SCHED_BATCH: Used for batch processing tasks that do not need interactivity.
    • SCHED_IDLE: Lowest priority for background tasks.
    1. Real-Time Scheduling Policies

    These have higher priority and are used for time-sensitive tasks:

    • SCHED_FIFO (First In, First Out): A real-time scheduling policy where the highest-priority process runs until it voluntarily yields the CPU.
    • SCHED_RR (Round Robin): Similar to FIFO but includes time slices to ensure fairness.

    You can check or modify the scheduling policy of a process using chrt:

    chrt -p

    Linux Scheduler: Completely Fair Scheduler (CFS)

    Linux primarily uses the Completely Fair Scheduler (CFS), which assigns CPU time proportionally based on a process’s priority.

    Concepts of CFS:

    • vruntime (Virtual Runtime): Determines the amount of time a process has run; lower vruntime processes get CPU access sooner.
    • Nice Value: Ranges from -20 (highest priority) to 19 (lowest priority), affecting scheduling.

    You can change a process’s priority using nice and renice:

    nice -n 10 ./myprogram

    renice -n -5 -p

    Monitoring Process Scheduling

    Use the following commands to inspect and manage scheduling:

    • top – Displays real-time CPU usage and process priorities.
    • htop – An interactive process viewer.
    • schedtool – Displays and sets process scheduling policies.

    Example to check a process’s priority:

         ps -eo pid,comm,pri,nice