input ready under UNIX ??!!??

Every system needs one terry at wsccs.UUCP
Sat Nov 5 18:14:55 AEST 1988


In article <547 at poseidon.ATT.COM>, psrc at poseidon.ATT.COM (Paul S. R. Chisholm) writes:
> <"He seemed like such a nice man . . . and then he turned out to be a writer!">
> 
> In article <771 at necis.UUCP>, rbono at necis.UUCP (Rich Bono) writes:
> > HELP!!  how can one (if at all) find out (non-destructivly) if there is
> > any input waiting to be read from stdin???   With the Microsoft-C libraries
> > I can use the kbhit() function which returns TRUE if there are any characters
> > waiting to be input.  Clearing ICANON with a ioctl() for stdin does NOT do
> > what I want.....
> >(follows, an example that uses kbhit() and gets(); odd combination)
> 
> First of all, it sounds like a good idea to package this in a single
> routine with a portable interface.  You may have to entirely rewrite
> the routine to get it to run under the UNIX(R) operating system, but
> it would be called the same as under MS-DOS.

[a lot of non-ideal partial examples deleted]

Or you could call the standard routines for it.  Most Xenix and nearly all
AT-architecture UNIX implementations have rdchk() in either the standard
library or some other on the machine.

The ioctl parameters TIOCQCNT (Terminal Input Output Control Queue CouNT)
or FIONREAD also work nicely everywhere but system V.

The alarm across the signal only gives you blocking to a one second resoloutin,
far too slow to really do anything with.  Besides, when reading from some
devices (say the LAT drivers under ULTRIX) the alarm is just queued rather
than being executed, as the I/O blocks in kernel-mode code.  Bletch.

Using two file descriptors, two pipes and a selectio will work on Berkley; you
for a child process to do your reading and write a single character to the pipe
and read the other end of the pipe and the pipe to the child with a select I/O.
If you get the single character, write it to the pipe again.  This will allow
you to poll the 'keyboard':

                selectio() <------------+----------- keyboard reader (child)
					|
		= character?		|
					|
		yes: character-------->pipe
		no: process character.

The other (much cleaner and nicer and recommended) way would be to use a
semaphore and a child process.

Or you could just drag yourself to use the ioctl commands... but that would mean you have to read SIO(7) in your UNIX manual.  Bummer.


			terry at wsccs

PS: There is an advantage to using a semaphore or signal as the notification
method, rather than polling, as it allows you to go off and do something else
while you're waiting for the I/O to complete and not have to worry about
polling the port with an ioctl() and either replacing the idle process (if only
1 program does this) or dragging the system to it's knees if it's stupid enough
to let you (2 or more processes only).



More information about the Comp.lang.c mailing list