THINK FIRST·CODE LATER

← All labs

CPU limits and CFS throttling

Problem

Simulate CFS bandwidth control in 1 ms ticks.

Input: quota period threads work cores (quota and period in ms; each of threads threads needs work ms of CPU; the machine has cores cores). All threads start at t = 0.

In each 1 ms tick: the quota is refilled at the start of every period (t = 0, period, 2 × period, …); the number of threads that run is min(runnable threads, cores, remaining quota); running threads (lowest index first) each progress 1 ms and consume 1 ms of quota. A period is throttled if at some tick fewer threads ran than min(runnable, cores) because the quota was exhausted.

Print:

Limit: 0.20 CPUs (quota 20 ms per 100 ms)
Work: 1 thread x 50 ms on 4 cores
Finish time: 210 ms (without limit: 50 ms), throttled periods: 2

(limit as an integer if whole, else 2 decimals; "without limit" = ⌈threads × work / min(threads, cores)⌉.)

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.

os12-3-data-1.txt

20 100 1 50 4
What your program must print for os12-3-data-1.txt
Limit: 0.20 CPUs (quota 20 ms per 100 ms)
Work: 1 thread x 50 ms on 4 cores
Finish time: 210 ms (without limit: 50 ms), throttled periods: 2

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 < os12-3-data-1.txt

Windows PowerShell does not understand <:

Get-Content os12-3-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.