What Is a Time Slice? A Quick Interview Question Explained
The article defines a time slice (time quantum) as the fixed CPU interval allocated to each ready process in a time‑sharing scheduler, explains its role in round‑robin and CFS algorithms, discusses size trade‑offs, Linux implementation details, context‑switch costs, and typical interview follow‑up questions.
Definition
Time slice (time quantum) is a fixed amount of CPU time that the operating system assigns to each ready process or thread during scheduling in a time‑sharing system.
How a time slice works
The CPU is divided into consecutive slices. When a slice expires, a hardware timer generates an interrupt, the scheduler saves the current task’s state (registers, program counter, memory mappings) and restores the next runnable task, performing a context switch.
Round‑Robin execution
Fair queuing : each process receives an equal‑length slice.
Timer‑interrupt driven : a hardware timer fires when the slice ends, causing the CPU to yield.
Context switch : the OS saves the current process state and restores the next process, incurring overhead.
Size trade‑off
Too large : behaves like exclusive scheduling, leading to slow response and poor interactivity, but reduces context‑switch overhead.
Too small : appears more responsive, yet frequent switches waste CPU cycles; overhead dominates.
In practice a slice between 10 ms and 200 ms balances user‑perceived latency and switch cost.
Linux CFS implementation
Since kernel 2.6.23 Linux uses the Completely Fair Scheduler (CFS) instead of fixed slices. The core parameter is sched_latency, default 6 ms (computed as 750000 * (1 + ilog(ncpus))); newer kernels may use larger values (e.g., 24 ms in 6.6). Each runnable process receives a slice equal to sched_latency / number_of_runnable_processes. Example: with sched_latency = 6 ms and three equal‑priority processes, each gets 2 ms . Higher‑priority processes have larger weight and thus longer slices.
CFS also defines sched_min_granularity (default 0.75 ms ) to guarantee a minimum execution time per process.
/proc/sys/kernel/sched_latency_ns # target scheduling latency, default 6000000ns = 6 ms
/proc/sys/kernel/sched_min_granularity # minimum per‑process run time, default 0.75 ms
/proc/sys/kernel/sched_wakeup_granularity # wake‑up preemption granularity“CFS was replaced by the EEVDF scheduler in Linux 6.6 (2023); newer kernels may show different values.”
Related concepts
Context switch : the action after a slice expires; on modern x86 Linux the cost is roughly 1‑7 µs (common range 1.3‑3.5 µs).
Preemptive scheduling : slice expiration forces a preemptive switch regardless of the process’s willingness.
Timer interrupt : periodic hardware timer that underlies the slice mechanism.
Typical interview follow‑ups
What happens to a process’s state after its time slice ends? (Running → Ready, re‑queued.)
What problems arise from setting the slice too large or too small?
How does Linux CFS decide the slice length? (Dynamic division of sched_latency by runnable tasks, weighted by priority.)
What is a context switch and why does it affect performance? (State save/restore overhead.)
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Architect Handbook
Focused on Java interview questions and practical article sharing, covering algorithms, databases, Spring Boot, microservices, high concurrency, JVM, Docker containers, and ELK-related knowledge. Looking forward to progressing together with you.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
