Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!bloom-beacon!adam.pika.mit.edu!scs From: scs@adam.pika.mit.edu (Steve Summit) Newsgroups: comp.lang.c Subject: Re: reading with timeout? Message-ID: <11833@bloom-beacon.MIT.EDU> Date: 4 Jun 89 18:53:26 GMT References: <5125@eva.slu.se> Sender: daemon@bloom-beacon.MIT.EDU Reply-To: scs@adam.pika.mit.edu (Steve Summit) Lines: 26 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