Design and implement a simple task scheduler for a single CPU, single thread environment.
You need to support scheduling callbacks (tasks) using the following APIs:
-
scheduleOnce(task, runAtTime)
-
runs
task
exactly once at the specified absolute time.
-
scheduleWithDelay(task, delay)
-
runs
task
exactly once after the specified delay from “now”.
-
schedulePeriodic(task, initialDelay, period)
-
runs
task
first after
initialDelay
, then repeatedly every
period
.
Requirements
-
Only one task runs at a time (no parallelism).
-
If multiple tasks are due at the same time, execute them in a deterministic order (define one).
-
Define behavior if a periodic task’s next run time is missed because execution took too long.
-
Include how the scheduler waits/sleeps and wakes up to run the next task.
Deliverable
Describe the data structures and implement the scheduler logic.