Is it possible, in an OS-independent way, to suspend a C program for some
specified amount of time, in such a way that CPU load, due to this
program, while remaining suspended, is non-existant or, failing that,
minimal?
No. The best you can do is create an abstraction layer that will call an
appropriate API function for the platform you're running on. F'rexample:
(Not compiled or tested, standard disclaimers apply)
--------
/*os-abstraction.h*/
#ifndef H_OS_ABSTRACTION
#define H_OS_ABSTRACTION
/*Suspend execution for n seconds, using an OS-specific API call to
avoid using CPU time if possible.
Does not return any error indication is the sleep is interrupted!
*/
void suspend(unsigned n);
#endif /*H_OS_ABSTRACTION #include guard*/
--------
/*os-abstraction.c*/
#include "os-abstraction.h"
#ifdef HAVE_POSIX_SLEEP
#include <unistd.h>
/*POSIX sleep() will suspend execution for the number of seconds you give it*/
void suspend(unsigned n) {sleep(n);}
#endif /*HAVE_POSIX_SLEEP*/
#ifdef HAVE_WIN32_SLEEP
#include <windows.h>
/*Win32 Sleep() will suspend execution for the number of milliseconds
you give it
*/
void suspend(unsigned n) {Sleep(n*1000);}
#endif
/*You may also want to include a fallback to use clock() to spin
until the requested time has elapsed if you can't find any more
multitasking-friendly API calls to do it.
*/
--------
Then in your code:
--------
#include "os-abstraction.h"
/*...and when you want to sleep for a while:*/
suspend(delay);
--------
dave
--
Dave Vandervies (e-mail address removed)
And if we see a post about VisualAda++, we will storm Redmond and shove the
One Boot up the Gates of Hell. --August Derleth and Richard Heathfield
Why wait for a post about VisualAda++? If it feels good, do it. in CLC