Unix - Advanced Process Scheduling and Priority Management in UNIX

Process scheduling in UNIX refers to how the operating system decides which process should use the CPU at any given time. Since multiple processes may be ready to run simultaneously, the scheduler ensures efficient CPU utilization, fairness, and responsiveness.

At the core of UNIX scheduling is the concept of a process state. A process can be in states such as running, ready, waiting, or terminated. The scheduler selects processes from the ready queue and assigns CPU time based on specific algorithms and priorities.

Scheduling Policies

Modern UNIX-like systems (such as Linux) use a combination of scheduling policies:

  • Time-sharing (SCHED_OTHER or CFS - Completely Fair Scheduler)
    This is the default scheduling policy. It ensures that all processes get a fair share of CPU time. The scheduler dynamically adjusts priorities to maintain fairness, giving more CPU time to processes that have received less in the past.

  • Real-time scheduling
    Real-time processes have higher priority than normal processes and are scheduled using:

    • SCHED_FIFO (First-In-First-Out): Processes run until they finish or voluntarily yield the CPU.

    • SCHED_RR (Round Robin): Similar to FIFO but assigns a fixed time slice to each process.

Real-time scheduling is used in systems where timing is critical, such as embedded or control systems.

Process Priority

Each process in UNIX is assigned a priority that influences its scheduling. The scheduler prefers processes with higher priority.

There are two important concepts:

  • Static priority: Defined when the process is created or manually set.

  • Dynamic priority: Adjusted by the scheduler based on process behavior (CPU usage, waiting time, etc.).

In Linux, the Completely Fair Scheduler uses a concept called virtual runtime, which tracks how much CPU time a process has consumed relative to others.

Nice Value

The “nice” value is a user-controlled way to influence process priority. It ranges from:

  • -20 (highest priority)

  • +19 (lowest priority)

Lower nice values mean higher priority. By default, processes start with a nice value of 0.

Commands used:

  • nice: Starts a process with a specified priority

  • renice: Changes the priority of an existing process

Example:

nice -n 10 command_name

This runs the command with lower priority.

Only privileged users can assign negative nice values (higher priority), preventing misuse.

Preemption

UNIX systems use preemptive scheduling, meaning the scheduler can interrupt a running process to assign the CPU to another process. This ensures that no single process can monopolize the CPU.

Preemption is especially important in:

  • Multi-user systems

  • Interactive environments

  • Real-time systems

Context Switching

When the scheduler switches from one process to another, it performs a context switch. This involves saving the state of the current process (registers, program counter, memory mappings) and loading the state of the next process.

Although necessary, context switching introduces overhead, so efficient scheduling aims to minimize unnecessary switches.

CPU Affinity

Advanced UNIX systems allow setting CPU affinity, which binds a process to a specific CPU core. This improves cache efficiency and performance in multi-core systems.

Command example:

taskset -c 0,1 command_name

This restricts the process to CPU cores 0 and 1.

Load Balancing

In multi-processor systems, the scheduler distributes processes across CPUs to ensure balanced workload. This prevents some CPUs from being overloaded while others remain idle.

Starvation and Aging

  • Starvation occurs when low-priority processes never get CPU time.

  • To prevent this, UNIX systems use aging, gradually increasing the priority of waiting processes so they eventually get executed.

Summary

Advanced process scheduling in UNIX combines fairness, responsiveness, and efficiency. Through mechanisms like dynamic priorities, nice values, preemptive scheduling, and real-time policies, the system ensures optimal CPU usage across different workloads. Priority management gives both the system and users control over execution order, making UNIX suitable for everything from general-purpose computing to high-performance and real-time applications.