linuxunixhibernation

How to keep a C++ code running when the PC is in sleep mode?


I have a basic C++ code for a timer which works perfectly when the code is running, and the PC is not sleeping. I would like it to keep running even after the pc goes into sleep mode. Is it possible somehow? I am running Linux.


Solution

  • If you mean suspend to ram or suspend to disk with sleep mode (ACPI power states S1 to S4) then it is not possible. The cpu is not just idle in these modes but not processing anything. You would need to wake up the computer (ACPI S0 state).

    The Wikipedia article quotes the ACPI specifications (ACPI Spec Rev 5.0 - dated December 6, 2011):

    "Power on Suspend (POS): Processor caches are flushed, and the CPU(s) stops executing instructions. The power to the CPU(s) and RAM is maintained. Devices that do not indicate they must remain on may be powered off" (emphasis by me)

    However, if you meant to run your script when the computer is idle the situation is different. Idle means in this context that the PC is in ACPI state S0 but there are no tasks for the CPU at the moment. The CPU, or rather its cores, will be set to a power management C state between C0 and C6. The governor who controls these states will very quickly switch between C states and P states on demand. When the CPU is idle for longer, it will be sent into the next deeper C state.

    You don't have to do anything about this power state management. Your OS' scheduler and ondemand governor will take care of this. You might want to read on how to write your script in a way it doesn't wake up the CPU when not needed.