C How to suspend and wake up the computer in C/C++

Just to save the power and make less noise when it has no task to do.
For example:
schedule a job to be started at 3:00 am, and suspend the computer for other time.
 
Why do you need to do it in C/C++. Job and delayed suspension can be scheduled using cron or at .
As for waking up I'm not sure if it can be done easily programmatically, some info here .
 
Why do you need to do it in C/C++.
1. The time to suspend or wakeup may be dynamic. For example, if the web crawler founds a web site is busy, it'll suspend the computer and try later.
2. The result of depending on libs is better than depending executables any way. For example, more controllable, more effective.
 
The basic problem you're facing is: how do you actually suspend or wake the computer? Suspending is usually done by issuing a command, which is in and itself an executable (for example apm(8) or zzz(8). How do you intend to do this from your own C/C++ code? You could find the source code for these commands, and copy it into your program. From a software engineering perspective, that's an awful idea, it's duplication of effort. You could find a library that has this functionality (good luck!). Or you could invoke that other program from your C/C++ program (using system(3) or fork/exec), but at that point your program has de-facto become a script that runs other programs.

1. The time to suspend or wakeup may be dynamic. For example, if the web crawler founds a web site is busy, it'll suspend the computer and try later.
You can do dynamic stuff in many languages. In C and C++, in assembly, in Python or Perl, and in shell scripts. For shell scripts, you often do that by using canned executables that are de-facto part of the shell environment, like sed, expr, sort, uniq, join and awk.

You need to get away from the wrong impression that programming happens in C/C++, and that programming is better than scripting. There are zillions of programming languages, and some are better suited to some problems, while others are better suited to other problems. For this problem, C/C++ seems needlessly complex.

2. The result of depending on libs is better than depending executables any way. For example, more controllable, more effective.
Libs are not standardized, are not portable, and can change with version upgrades. If you want "controllable", you should go to a standards-controlled way. For example, look at the POSIX documents, and use only POSIX standard interfaces. Now that could mean using a POSIX-standard shell to write a shell script, which at the end invokes apm(8).
 
schedule a job to be started at 3:00 am, and suspend the computer for other time.
How you will wake it up?
The only way I envision is Wake-on-LAN (besides BIOS options), so you'll have to program another computer to send a magic packet to wake this one up.

Does anybody know another option?..
 
On some machines, you can program the BIOS (through ACPI) to wake up at a predetermined time.

I played with Wake-on-LAN many decades ago, when motherboards didn't have built-in ethernet yet. Required getting a special ethernet card, and running funny jumper wires from the ethernet card to some pin on the motherboard (NMI or something like that). Then configuring the BIOS correctly, and then creating the super-special Ethernet packet on another node. Wasted two or three weekends on it, and never got it to work. Hopefully it is easier now.
 
It would depend on the state you suspend to how you go about doing it. Probably have better luck getting this answered via the development mailing lists.
 
You could do that with auto-throttling using sysctl variables. You could also use cron and cron from a gateway or router as well with magic packets but that still doesn't really give the OP what he was looking for. I'm sure scouring powerd's source could be beneficial for him, though.
 
Back
Top