Linux Process Scheduling — Nice Values, Realtime Policies, CPU Affinity

 

Chapter 35: Process Priorities & Scheduling
Linux Process Scheduling — Nice Values, Realtime Policies, CPU Affinity
7
HTML Files
14+
Code Examples
40+
Interview Q&A

What You Will Learn

This chapter covers everything about how Linux decides which process runs, when, and for how long. You will learn about the nice value system (soft priority), the full POSIX realtime scheduling API (SCHED_FIFO, SCHED_RR, SCHED_BATCH, SCHED_IDLE), and how to pin processes to specific CPUs using CPU affinity masks. These are critical topics for embedded Linux, real-time systems, and any performance-sensitive application development.

Key Concepts Covered

Nice Value getpriority() setpriority() SCHED_OTHER SCHED_FIFO SCHED_RR SCHED_BATCH SCHED_IDLE sched_setscheduler() sched_getscheduler() sched_yield() sched_rr_get_interval() CPU Affinity sched_setaffinity() cpu_set_t RLIMIT_NICE RLIMIT_RTPRIO SCHED_RESET_ON_FORK

Tutorial Files

📘 File 1 — Process Priorities: Nice Values
Understand how the kernel’s default round-robin scheduler works and how the nice value lets you influence CPU allocation without realtime privileges.
Round-Robin Time-Sharing Nice Value Range (-20 to +19) getpriority() / setpriority() nice() legacy call PRIO_PROCESS / PRIO_PGRP / PRIO_USER
📘 File 2 — Realtime Scheduling Overview
Why standard scheduling is not enough for real-time applications. Understand the guarantees needed and what POSIX realtime scheduling provides.
Realtime Requirements Soft vs Hard Realtime Priority Levels 1–99 SCHED_OTHER vs SCHED_RR vs SCHED_FIFO Multiprocessor Queues
📘 File 3 — SCHED_RR and SCHED_FIFO Policies
Deep dive into both realtime scheduling policies — how they differ, when each one is preempted, and queue behavior at each priority level.
SCHED_RR Round-Robin SCHED_FIFO First-In First-Out Preemption Rules Time Slice Behavior Queue Management
📘 File 4 — SCHED_BATCH and SCHED_IDLE Policies
Linux-specific non-realtime policies for background batch jobs and ultra-low-priority idle work.
SCHED_BATCH Purpose SCHED_IDLE Purpose Difference from SCHED_OTHER When to use each
📘 File 5 — Realtime Scheduling API: Set & Get
Complete coverage of the system calls to read and modify scheduling policies and priorities, including privilege rules and RLIMIT_RTPRIO.
sched_setscheduler() sched_getscheduler() sched_setparam() sched_getparam() RLIMIT_RTPRIO SCHED_RESET_ON_FORK
📘 File 6 — sched_yield() and RR Time Slice
How a realtime process voluntarily gives up the CPU, and how to query the SCHED_RR time slice length.
sched_yield() sched_rr_get_interval() Runaway Process Prevention RLIMIT_CPU / RLIMIT_RTTIME Watchdog Pattern
📘 File 7 — CPU Affinity
Pin processes to specific CPUs on multiprocessor systems. Understand soft vs hard affinity, cache performance, and the cpu_set_t macros.
Soft vs Hard CPU Affinity sched_setaffinity() sched_getaffinity() cpu_set_t Macros Cache Performance isolcpus Boot Option

Free Linux Tutorials at EmbeddedPathashala

All tutorials are free. No paywalls. Built for students and embedded engineers.

Visit Website

Leave a Reply

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