How can I read keyboard without stopping
Phil Harbison
alvitar at madhat.UUCP
Tue Aug 23 10:48:47 AEST 1988
In article <813 at ms3.UUCP>, isns02 at ms3.UUCP (Harris Reavin) writes:
> I would like to know if it is possible to get input data from the
> keyboard while my program is constantly looping and displaying output
> to the screen. The input comes from a call to "popen()".
The best way I've found to do this is to spawn a separate process to
handle the keyboard and communicate using pipes. I have a program that
performs an I/O multiplexing function. It must simultaneously monitor
the keyboard and the output of a pipe, sending data from the pipe to the
display, and data from the keyboard to a subprocess. I have fork a
couple of "handler" processes, one for the keyboard and one for the
subprocess. The handlers collect their data using blocking reads, then
send the data through a pipe to the main process using a simple packet
protocol. The main process does a blocking read on the pipe waiting for
the next packet, and can tell by the packet ID whether it came from the
keyboard or the subprocess. The packets look like this:
+-----------+------------+------+
| source-id | byte count | data |
+-----------+------------+------+
There are two nice side effects of this implementation. The same code
runs on v7, BSD, and USG flavors of UNIX, and probably any version of
UNIX or UNIX-derivatives which support atomic writes to pipes and fstat.
Previous versions of this code used select for BSD, non-blocking reads
for USG, and punted on v7. Checking for type-ahead is now easy, since
the main process can perform an fstat to find out if there is data in
the pipe.
In article <12251 at ncoast.UUCP>, Brandon S. Allbery writes:
> +---------------
> | 3) FIONREAD. BSD systems have an ioctl you can apply to a terminal
> | line to get the number of characters queued ...
>
> System V does not. Xenix has rdchk(fd). (I wish AT&T would pick it up!)
Unisoft's version of System V has FIONREAD, but unfortunately it only
works when the tty is in cooked mode! In most cases where I want to use
FIONREAD, the tty is in raw mode.
More information about the Comp.unix.wizards
mailing list