[comp.lang.c] reading with timeout?

robert@eva.slu.se (Robert Olsson datoravd, EMC tfn 018-672581) (06/03/89)

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

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Swedish University of Agricultural Sciences
Dept. of Statistics, Data Processing and Agricultural Extension
Computer Section EMC, Box 7072, 750 07  UPPSALA,  SWEDEN   Tel +46 18 672407
E-mail robert@eva.slu.se, SUNET eva::robert, FAX +46 18 302876

scs@adam.pika.mit.edu (Steve Summit) (06/05/89)

In article <5125@eva.slu.se> robert@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@adam.pika.mit.edu