THINK FIRST·CODE LATER

← All labs

Serverless cold starts and keep-alive

Problem

Simulate a serverless platform where each instance handles one request at a time.

Input: keepAlive coldMs execMs, then request arrival times (ms, non-decreasing).

For each request at time t:

  1. Examine existing instances in creation order. An instance that is free (busy until ≤ t) but whose last finish time is more than keepAlive before t has expired: add keepAlive to its idle time and retire it.
  2. Among the remaining free instances, choose the one with the latest last finish time (most recently used; ties → lowest index). Add (t − last finish) to its idle time; the request runs warm: latency = execMs.
  3. If none, start a new instance: cold start, latency = coldMs + execMs.

Print t=900: warm on I3, latency 100 ms or t=0: COLD start I1, latency 600 ms. At the end, add keepAlive of final idle time to each instance not retired, and print Requests: n, cold starts: c (p%), average latency X ms (one decimal) and Instances: k, idle warm time billed: T ms.

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

600 500 100
0 50 100 900 1000 1100 5000 5050
What your program must print for os12-4-data-1.txt
t=0: COLD start I1, latency 600 ms
t=50: COLD start I2, latency 600 ms
t=100: COLD start I3, latency 600 ms
t=900: warm on I3, latency 100 ms
t=1000: warm on I3, latency 100 ms
t=1100: warm on I3, latency 100 ms
t=5000: COLD start I4, latency 600 ms
t=5050: COLD start I5, latency 600 ms
Requests: 8, cold starts: 5 (63%), average latency 412.5 ms
Instances: 5, idle warm time billed: 3200 ms

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

Windows PowerShell does not understand <:

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