Implementing a Timeout in C/C++
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Implementing a Timeout in C/C++




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-15-06 10: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






[ Post a follow-up to this message ]



    Re: Implementing a Timeout in C/C++  
Roger Leigh


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-16-06 01:48 AM

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





[ Post a follow-up to this message ]



    Re: Implementing a Timeout in C/C++  
Micah Cowan


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-16-06 01:48 AM

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;
}





[ Post a follow-up to this message ]



    Re: Implementing a Timeout in C/C++  
Ian Collins


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-16-06 12:50 PM

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.





[ Post a follow-up to this message ]



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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-16-06 10: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






[ Post a follow-up to this message ]



    Re: Implementing a Timeout in C/C++  
Micah Cowan


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-16-06 10: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





[ Post a follow-up to this message ]



    Re: Implementing a Timeout in C/C++  
Brian C


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-17-06 07: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.





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 03:50 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register