reading with timeout?

Steve Summit scs at adam.pika.mit.edu
Mon Jun 5 04:53:26 AEST 1989


In article <5125 at eva.slu.se> robert at eva.slu.se (Robert Olsson) writes:
>	Is there a way of reading with timeout in C, without using the 
>	vendor-specific routines? I would like something like this:
>
>	timeout = settimeout( 5 );
>	fscanf (.......
>	if ( timeout ) {

I usually do it by setting an alarm and then catching SIGALRM.
In the old days, your SIGALRM handler didn't really have to do
anything, and your read appeared to fail with errno = EINTR.
Berkeley broke this with "restartable system calls" in 4.2, so
you either have to use longjmp out of the signal handler, or turn
off system call restarting, which I understand 4.3 lets you do.

A drawback is that the granularity of alarm() is poor, but many
systems provide some other way of setting alarms with finer than
one second resolution.

Under a System V style terminal driver, there are timeout
parameters you can set (as long as you are reading from a
terminal), but those are probably the "vendor-specific routines"
you already knew about.

                                            Steve Summit
                                            scs at adam.pika.mit.edu



More information about the Comp.lang.c mailing list