Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Temporary program suspension

Reply
Thread Tools

Temporary program suspension

 
 
Thomas Carter
Guest
Posts: n/a
 
      02-21-2005
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?


 
Reply With Quote
 
 
 
 
Dave Vandervies
Guest
Posts: n/a
 
      02-21-2005
In article <(E-Mail Removed)>,
Thomas Carter <(E-Mail Removed)> wrote:
> 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 http://www.velocityreviews.com/forums/(E-Mail 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
 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      02-21-2005
Thomas Carter wrote:

> 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
> kill -s CONT 7053

 
Reply With Quote
 
Gibby Koldenhof
Guest
Posts: n/a
 
      02-21-2005
> /*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


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-21-2005
"Gibby Koldenhof" <flux@(E-Mail Removed)> writes:
>> /*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)
> ;
> }


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.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Dave Vandervies
Guest
Posts: n/a
 
      02-21-2005
In article <cvddb8$mnt$(E-Mail Removed)>,
E. Robert Tisdale <(E-Mail Removed)> wrote:
>Thomas Carter wrote:
>
>> 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...)

--
Dave Vandervies (E-Mail Removed)
This is an excellent answer. It's off-topic, it's wrong, and it doesn't
answer the question.
--Richard Heathfield in comp.lang.c
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-21-2005
"E. Robert Tisdale" <(E-Mail Removed)> writes:
> Thomas Carter wrote:
>> 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?


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>

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Dave Vandervies
Guest
Posts: n/a
 
      02-21-2005
In article <(E-Mail Removed)>,
Keith Thompson <(E-Mail Removed)> wrote:
>"Gibby Koldenhof" <flux@(E-Mail Removed)> writes:

[quoting me:]
>>> /*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:


>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

--
Dave Vandervies (E-Mail Removed)
Are we supposed to trust a page that states, amongst others, "Microsoft
absolutely respects the privacy of its customers"? 'Cause I don't, no matter
how pleasantly they smirk. --Richard Bos in comp.lang.c
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      02-21-2005
Keith Thompson wrote:

> E. Robert Tisdale writes:
>
>>Thomas Carter wrote:
>>
>>>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?

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

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      02-21-2005
In article <(E-Mail Removed)>,
Keith Thompson <(E-Mail Removed)> wrote:

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

-- Richard
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
possibly ot: thread suspension Chris Forone C++ 7 05-15-2008 02:49 PM
anonymous array of strings // ("taking address of temporary"- how long is temporary valid?) anon.asdf@gmail.com C++ 7 02-12-2008 10:58 AM
temporary desktop shortcut Lou Firefox 3 08-03-2005 02:30 PM
ISP account suspension virus...... steve NZ Computing 25 06-06-2005 10:57 AM
T'Bird + Temporary groups. Gavern Firefox 1 11-29-2004 01:41 PM



Advertisments