100. RAII Shutdown Ordering

In embedded systems, hardware components must shut down in a strict order.
If the shutdown sequence is incorrect, peripherals may hang, buses may lock, or clocks may remain enabled unnecessarily.

You are given three system components:

  • Clock – provides the base clock signal
  • Bus – depends on the Clock
  • Driver – depends on the Bus

Your task is to design a system where:

  • All components start automatically when the system is created
  • All components shut down automatically when the system goes out of scope
  • Shutdown happens in the correct dependency order
  • main() does not manually start or stop any component

This must be achieved only using composition and destructors (RAII), without explicit cleanup calls.

What You Must Do

  • Create a class named SystemController
  • SystemController must own:
    • Clock
    • Bus
    • Driver
  • Dependencies must be wired so that:
    • Bus uses Clock
    • Driver uses Bus
  • Do not add any shutdown or cleanup calls in main()
  • Correct shutdown must occur only because objects go out of scope

Program Flow

  1. Read an address and a value from input
  2. Create a SystemController object inside a local scope
  3. All components start automatically
  4. The driver performs one write operation
  5. The scope ends
  6. Driver shuts down
  7. Bus shuts down
  8. Clock shuts down

Input

Two integers provided via standard input:

  • Address
  • Value

Output

The output must match exactly, including order and formatting:

Clock enabled
Bus initialized
Driver started
Bus write: <addr> <value>
Driver stopped
Bus stopped
Clock disabled

Constraints

  • Clock, Bus, and Driver must NOT be created in main()
  • SystemController must be the only owner of all components
  • No manual cleanup calls
  • No dynamic memory allocation
  • No smart pointers
  • No inheritance
  • Output must match exactly

 

 

 

Loading...

Input

5 10

Expected Output

Clock enabled Bus initialized Driver started Bus write: 5 10 Driver stopped Bus stopped Clock disabled