THINK FIRST·CODE LATER

← All labs

Linux in practice: nice values and CPU shares

Problem

Linux's default scheduler gives each runnable task a CPU share proportional to a weight derived from its nice value (−20 … 19). Some weights: nice −5 → 3121, nice 0 → 1024, nice 5 → 335, nice 10 → 110.

  1. Predict: two CPU-bound processes share one core, one with nice 0 and one with nice 5. What percentage of the core should each get? Repeat for nice 0 vs. nice 10.
  2. Measure: on Linux, pin both to core 0 and run them:
    taskset -c 0 nice -n 0 yes > /dev/null &
    taskset -c 0 nice -n 5 yes > /dev/null &
    top -d 2          # watch %CPU for the two 'yes' processes
    
    Record %CPU, NI and PR for both. Then kill them and repeat with nice 10.
  3. Compare prediction and measurement. Why is the measurement not exactly the prediction?
  4. What would happen without taskset on a machine with several idle cores? Try it.
  5. Explain how this relates to this chapter's fair-share and lottery ideas, and why a cloud provider would want weights per container rather than per process.

Work it out on paper, in a document or here, then compare with the model answer. Your answer stays in your browser — it is never sent to or stored on the server.