THINK FIRST·CODE LATER

← All labs

A mini CFS: virtual runtime and nice weights

Problem

Simulate the core idea of Linux's Completely Fair Scheduler on one CPU for CPU-bound tasks.

Input: first line: total simulated time in ms. Then name nice per line (nice from −20 to 19).

Use the Linux weight table (index nice + 20):

88761 71755 56483 46273 36291 29154 23254 18705 14949 11916
 9548  7620  6100  4904  3906  3121  2501  1991  1586  1277
 1024   820   655   526   423   335   272   215   172   137
  110    87    70    56    45    36    29    23    18    15

All tasks start with vruntime 0. Every 1 ms, run the task with the smallest vruntime (ties: first in input order) and add 1024 / weight to its vruntime.

Print First 10 ms: followed by the names that ran in each of the first 10 ms (fewer if the total is smaller), then for each task: NAME (nice N, weight W): ran X ms = P% (ideal I%), vruntime V — P = X/total, ideal = weight / Σweights (one decimal each), V with two decimals.

Input:

100
video 0
backup 5

Output:

First 10 ms: video backup video video video backup video video video backup
video (nice 0, weight 1024): ran 75 ms = 75.0% (ideal 75.3%), vruntime 75.00
backup (nice 5, weight 335): ran 25 ms = 25.0% (ideal 24.7%), vruntime 76.42

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.