Unix Programming - Implementing a Timeout in C/C++

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2006 > Implementing a Timeout in C/C++





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Implementing a Timeout in C/C++
wade.lindsey@gmail.com

2006-03-15, 5:55 pm

Hello all -

I am attempting to implement a timeout condition in C/C++ and am
finding very little resources on doing such a thing. In ADA, I would
do something like this:

select
accept An_Entry do
end An_Entry;
or
delay 5.0;
Put("An_Entry: timeout");
end select;

but don't confuse ADA's select() with C's...

Anyway, what I'm trying to do is:

while (ERROR_CONDITION)
{
<Wait for either 10 Seconds to expire or ERROR_CONDITION == 0>
}

basically, a straitforward timeout...

Is there anyway to achieve this without using sleep? I have other
actions going on in the background that I don't want to suspend, I just
want a max-timed while loop - is that possible?? Trivial??

Thanks,
Wade

Roger Leigh

2006-03-15, 8:48 pm

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

wade.lindsey@gmail.com writes:

> basically, a straitforward timeout...
>
> Is there anyway to achieve this without using sleep? I have other
> actions going on in the background that I don't want to suspend, I just
> want a max-timed while loop - is that possible?? Trivial??


setitimer(2) or alarm(2) are good choices.

Install an SIGALRM signal handler, and use that to break out of the
loop by setting a condition variable of type sig_atomic_t.

Example:

http://cvs.alioth.debian.org/cgi-bi...ot=buildd-tools
http://cvs.alioth.debian.org/cgi-bi...ot=buildd-tools

This is a password entry for TTYs, to hook into PAM. It uses a timer
to implement two timeouts: a warning timeout, and a fatal timeout.

In this case, there isn't a loop, it interrupts a read(2) system call,
but it could easily have been

while (!timer_expired)
{
...
}


Regards,
Roger

- --
Roger Leigh
Printing on GNU/Linux? http://gutenprint.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8+ <http://mailcrypt.sourceforge.net/>

iD8DBQFEGKsGVcFcaSW/ uEgRAv4LAKDSXA1joJ4MU6ozUpWfQ7UrJWIGCwCg
xv9e
SeXC1NiXUUFui8cEU9xwklw=
=Oj7c
-----END PGP SIGNATURE-----
Micah Cowan

2006-03-15, 8:48 pm

wade.lindsey@gmail.com writes:

> Anyway, what I'm trying to do is:
>
> while (ERROR_CONDITION)
> {
> <Wait for either 10 Seconds to expire or ERROR_CONDITION == 0>
> }
>
> basically, a straitforward timeout...
>
> Is there anyway to achieve this without using sleep? I have other
> actions going on in the background that I don't want to suspend, I just


What sort of error condition do you have going? For certain types of
situations select() (C's :-)) can be most beneficial, as it will yield
processor time to others for use.

You could certainly check the value of time() [for real seconds] or
clock() [for process seconds]; or gettimeofday() [if you want finer
precision].

Your best bet may be to use alarm() with a handler. For instance:

#include <signal.h>
#include <stdlib.h>
#include <unistd.h>

volatile sig_atomic_t got_alarm = 0;

void handle_alarm(int signum)
{
got_alarm = 1;
}

int main(void)
{
int errcond = 1;
void (*sigrslt)(int);

sigrslt = signal(SIGALRM, handle_alarm);
if (sigrslt == SIG_ERR)
return EXIT_FAILURE;

alarm(5);
while (errcond && !got_alarm)
;
return EXIT_SUCCESS;
}
Ian Collins

2006-03-16, 7:50 am

wade.lindsey@gmail.com wrote:
> Hello all -
>
> I am attempting to implement a timeout condition in C/C++ and am
> finding very little resources on doing such a thing. In ADA, I would
> do something like this:
>
> select
> accept An_Entry do
> end An_Entry;
> or
> delay 5.0;
> Put("An_Entry: timeout");
> end select;
>
> but don't confuse ADA's select() with C's...
>
> Anyway, what I'm trying to do is:
>
> while (ERROR_CONDITION)
> {
> <Wait for either 10 Seconds to expire or ERROR_CONDITION == 0>
> }
>
> basically, a straitforward timeout...
>
> Is there anyway to achieve this without using sleep? I have other
> actions going on in the background that I don't want to suspend, I just
> want a max-timed while loop - is that possible?? Trivial??
>

Are the other actions other threads?

If so, you might consider using a condition variable and
pthread_cond_timedwait. All depends what your error condition is and
how it is cleared.

--
Ian Collins.
wade.lindsey@gmail.com

2006-03-16, 5:53 pm

Thanks -

It sounds like select() might work best for me - it's checking for a
local condition, (foo =< 0). All info I can find about select(..)
is for use with sockets and tcp/ip - how can it be used in standard
algorithms?

Thanks again,
Wade

Micah Cowan

2006-03-16, 5:53 pm

wade.lindsey@gmail.com writes:

> Thanks -
>
> It sounds like select() might work best for me - it's checking for a
> local condition, (foo =< 0). All info I can find about select(..)
> is for use with sockets and tcp/ip - how can it be used in standard
> algorithms?


Er... it can't. It's great for testing conditions on file
descriptors... it can also be used to wait until either a specified
timeout has expired, or an early signal is received.

You /could/ use it with communicating threads/processes, where the
thread/process doing the actual work (and updating foo) writes a byte
(or something) to a pipe that the waiting thread/process is
select()ing... but if you're doing everything in a single
thread/process (that is, foo gets updated somewhere in the body of the
loop) then select() won't do anything for you; and you might as well
call time() repeatedly and compare it with a saved value, as alarm()
won't really save you anything and could be less precise (it will wait
/at least/ as long as you specified, but it might be slightly longer).

HTH,
Micah
Brian C

2006-03-17, 2:57 am

Micah Cowan wrote:
> wade.lindsey@gmail.com writes:
>
>
>
>
> Er... it can't. It's great for testing conditions on file
> descriptors... it can also be used to wait until either a specified
> timeout has expired, or an early signal is received.
>
> You /could/ use it with communicating threads/processes, where the
> thread/process doing the actual work (and updating foo) writes a byte
> (or something) to a pipe that the waiting thread/process is
> select()ing... but if you're doing everything in a single
> thread/process (that is, foo gets updated somewhere in the body of the
> loop) then select() won't do anything for you; and you might as well
> call time() repeatedly and compare it with a saved value, as alarm()
> won't really save you anything and could be less precise (it will wait
> /at least/ as long as you specified, but it might be slightly longer).
>
> HTH,
> Micah

Also, alarm() is not MT-safe. I had to write my own MT-safe alarm.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com