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

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

os5-4-data-1.txt

100
video 0
backup 5
What your program must print for os5-4-data-1.txt
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

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < os5-4-data-1.txt

Windows PowerShell does not understand <:

Get-Content os5-4-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

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.