[net.unix-wizards] Checking for waiting characters

KEN%NJITCCCC.BITNET@wiscvm.wisc.edu (Kenneth Ng) (05/01/86)

On Unix System V version 2 on an AT&T 3b5, has anyone been able to
successfully test if characters are pending?  I've got an application
that needs to time out after a certain amount of time if no characters
come through.  Every read I've done seems to wait for characters.
To date, I've tried setting -icanon on standard in, I've set the EOL
value to 50, which according to the documentation should give me a time
out of 5 seconds, and I've set the O_NDELAY on a fcntl() to standard
input.  Any help or suggestions would be greatly appreciated.

root@ptsfa.UUCP (Admin) (05/06/86)

In article <470@brl-smoke.ARPA> KEN%NJITCCCC.BITNET@wiscvm.wisc.edu (Kenneth Ng) writes:
>On Unix System V version 2 on an AT&T 3b5, has anyone been able to
>successfully test if characters are pending?  I've got an application
>that needs to time out after a certain amount of time if no characters
>come through.  Every read I've done seems to wait for characters.
>To date, I've tried setting -icanon on standard in, I've set the EOL
>value to 50, which according to the documentation should give me a time
>out of 5 seconds, and I've set the O_NDELAY on a fcntl() to standard
>input.  Any help or suggestions would be greatly appreciated.


When in raw (c_lflag & ~ICANON) mode the MIN and TIME (EOF and EOL, respectively)
you can set both the minimum number of characters to wait for AND the
maximum time to wait AFTER the first character arrives.

There were problems with this prior to SVR2 which once were described as
a documentation bug, but they work now.

Couple these with O_NDELAY and you can do what you want.

ejbjr@ihuxy.UUCP (Branagon) (05/07/86)

*** REPLACE THIS MESSAGE WITH YOUR LINE ***

> In article <470@brl-smoke.ARPA> KEN%NJITCCCC.BITNET@wiscvm.wisc.edu (Kenneth Ng) writes:
> >On Unix System V version 2 on an AT&T 3b5, has anyone been able to
> >successfully test if characters are pending?  I've got an application
> >that needs to time out after a certain amount of time if no characters
> >come through.

The easiest way I've found to do this is to set an alarm, and use a signal
trapping function (see alarm(2) and signal(2)).  The alarm can be set once
at the beginning, or reset after each character.  I'm not sure if alarm()
is sysV specific or not.

-----------------

Ed Branagan
ihnp4!ihuxy!ejbjr

gwyn@brl-smoke.ARPA (Doug Gwyn ) (05/10/86)

In article <1364@ihuxy.UUCP> ejbjr@ihuxy.UUCP (Branagon) writes:
>> In article <470@brl-smoke.ARPA> KEN%NJITCCCC.BITNET@wiscvm.wisc.edu (Kenneth Ng) writes:
>> >On Unix System V version 2 on an AT&T 3b5, has anyone been able to
>> >successfully test if characters are pending?  I've got an application
>> >that needs to time out after a certain amount of time if no characters
>> >come through.
>
>The easiest way I've found to do this is to set an alarm, and use a signal
>trapping function (see alarm(2) and signal(2)).  The alarm can be set once
>at the beginning, or reset after each character.  I'm not sure if alarm()
>is sysV specific or not.

No, alarm() has been in all UNIXes since 6th Edition.
However, the alarm signal can mangle I/O involving "slow"
devices (typically terminal ports), so it is not suitable
for some applications like this.  Other approaches such
as using MIN,TIME are preferable.

brent@poseidon.UUCP (Brent P. Callaghan) (05/12/86)

Try this:

   jmp_buf env ;

   ringgg () { longjmp(env,1) ; }
     :
     :
   signal(SIGALRM, ringgg) ;
   alarm(timeout) ;

   nchars = setjmp(env)==0 ? read(tty, buff, BUFSZ) : 0 ;
   
-- 
				
Made in New Zealand -->		Brent Callaghan
				AT&T Information Systems, Lincroft, NJ
				{ihnp4|mtuxo|pegasus}!poseidon!brent
				(201) 576-3475