Temporary program suspension

T

Thomas Carter

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?
 
D

Dave Vandervies

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
 
E

E. Robert Tisdale

Thomas said:
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?

It's not [completely] OS-independent
but POSIX compliant systems will provide a mechanism:
> man 7 signal
SIGNAL(7) Linux Programmer’s Manual SIGNAL(7)



NAME
signal - list of available signals
.
.
.
Signal Value Action Comment
------------------------------------------------------
SIGCONT 19,18,25 Continue if stopped
SIGSTOP 17,19,23 Stop Stop process
.
.
.
> kill -l
HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE
ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ
VTALRM PROF WINCH POLL PWR SYS RTMIN RTMIN+1 RTMIN+2 RTMIN+3
RTMAX-3 RTMAX-2 RTMAX-1 RTMAX
> xclock -update 1 &
> ps ax | grep xclock
7053 pts/1 S 0:00 xclock -update 1
7060 pts/1 R 0:00 grep xclock
> kill -s STOP 7053
[1] + Suspended (signal) xclock -update 1
 
G

Gibby Koldenhof

/*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.
*/

As mentioned there's no real portable way that's safe and does what you
want, yet you probably could do something like:

void ansi_wait(int seconds)
{
volatile clock_t endwait = clock() + seconds * CLOCKS_PER_SEC ;

while(clock() < endwait)
;
}

But again, it's quiet naieve and might fail miserably on some platforms, and
might even take more resources than you want it to.

Cheers,
Gibby Koldenhof
 
K

Keith Thompson

Gibby Koldenhof said:
As mentioned there's no real portable way that's safe and does what you
want, yet you probably could do something like:

void ansi_wait(int seconds)
{
volatile clock_t endwait = clock() + seconds * CLOCKS_PER_SEC ;

while(clock() < endwait)
;
}

The clock() function returns an indication of CPU time, not wall clock
time. Using clock() guarantees that the program will consume CPu
resources.
But again, it's quiet naieve and might fail miserably on some platforms, and
might even take more resources than you want it to.

That too.
 
D

Dave Vandervies

Thomas said:
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?

It's not [completely] OS-independent
but POSIX compliant systems will provide a mechanism:
man 7 signal

....which isn't portable (which the OP specifically requested), and
doesn't correctly answer the OP's question (how would you specify the
amount of time to suspend for?).


dave
(let me go attack the sigmonster with a pointy stick...)
 
K

Keith Thompson

Quick answer: No.
It's not [completely] OS-independent
but POSIX compliant systems will provide a mechanism:

It's not OS-independent at all.

[snip]
kill -s STOP 7053
[1] + Suspended (signal) xclock -update 1
kill -s CONT 7053

And that has nothing to do with C. I suspect the OP was looking for
something that could be invoked from within a C program. You've also
ignored the "for some specified amount of time" requirement.

If you're going to assume POSIX (contrary to the OP's request for an
OS-independent method), there are much better ways to do this.

<OT>The sleep() function is OS-specific but widely available.
Variants with sub-second resolution are not quite as widely available.
For details, try your system documentation or a system-specific
newsgroup.</OT>
 
D

Dave Vandervies

The clock() function returns an indication of CPU time, not wall clock
time. Using clock() guarantees that the program will consume CPu
resources.

Gah. That was my mistake - to time the suspension according to a wall
clock and not according to CPU time taken by the program, you'd want to
use time() and not clock() as I suggested.

A really smart optimizer could even notice that you're doing nothing but
call time() in your loop and insert an appropriate sleep call for you.
(Though I suspect if you had an optimizer that smart you'd also have an
OS API call that you'd want to use instead...)


dave
 
E

E. Robert Tisdale

Keith said:
Quick answer: No.
It's not [completely] OS-independent
but POSIX compliant systems will provide a mechanism:

It's not OS-independent at all.

[snip]
kill -s STOP 7053
[1] + Suspended (signal) xclock -update 1
kill -s CONT 7053

And that has nothing to do with C. I suspect that
the OP was looking for something that could be invoked from within a C program.
You've also ignored the "for some specified amount of time" requirement.

If you're going to assume that
POSIX (contrary to the OP's request for an OS-independent method),
there are much better ways to do this.

<OT>The sleep() function is OS-specific but widely available.
Variants with sub-second resolution are not quite as widely available.
For details,
try your system documentation or a system-specific newsgroup.</OT>

I'm not sure that this will be useful to Thomas Carter.
There is no way to put the program to sleep asynchronously.
He will almost certainly need a way to signal the program
to put it to sleep anyway so he might as well send signals
to suspend then continue after a specified amount of time.
 
R

Richard Tobin

It's not [completely] OS-independent
but POSIX compliant systems will provide a mechanism:

It's not OS-independent at all.[/QUOTE]

You could say the same about C. It's only available on OSes that have
a C compiler.

-- Richard
 
K

Keith Thompson

E. Robert Tisdale said:
Keith Thompson wrote: [...]
<OT>The sleep() function is OS-specific but widely available.
Variants with sub-second resolution are not quite as widely available.
For details,
try your system documentation or a system-specific newsgroup.</OT>

I'm not sure that this will be useful to Thomas Carter.
There is no way to put the program to sleep asynchronously.
He will almost certainly need a way to signal the program
to put it to sleep anyway so he might as well send signals
to suspend then continue after a specified amount of time.

He didn't ask about putting the program to sleep asynchronously.

If the sleep() function were part of ISO C, it would exactly satisfy
what the OP was asking about (re-read his question if you don't
believe me).

Note that he specifically asked for something OS-independent.
 
L

Lawrence Kirby

It's not [completely] OS-independent
but POSIX compliant systems will provide a mechanism:

It's not OS-independent at all.

You could say the same about C. It's only available on OSes that have
a C compiler.[/QUOTE]

That isn't a dependency of the characteristics of the OS. The only issue
there would be if the OS made it impossible to create a C compiler for it.
And remember a C compiler for an OS doesn't have to run under the OS, you
can typically use a cross-compiler for environments too limited in
themselves to run a C compiler.

POSIX on the other hand is essentially an OS level specification so
anything that depends on it is inherently OS-specific, even though it
might be emulated.

Lawrence
 
R

Richard Bos

It's not [completely] OS-independent
but POSIX compliant systems will provide a mechanism:

It's not OS-independent at all.

You could say the same about C. It's only available on OSes that have
a C compiler.[/QUOTE]

But the context of this newsgroup is OSes (or other environments) that
_do_ have a C implementation (not necessarily compiler). On all of
those, C is available (tautologically); not all of them do have POSIX.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top