Path: utzoo!attcan!uunet!ingr!madhat!alvitar From: alvitar@madhat.UUCP (Phil Harbison) Newsgroups: comp.unix.wizards Subject: Re: How can I read keyboard without stopping Message-ID: <282@madhat.UUCP> Date: 23 Aug 88 00:48:47 GMT References: <813@ms3.UUCP> <1246@mcgill-vision.UUCP> <12251@ncoast.UUCP> Organization: DataVision, Huntsville AL Lines: 39 In article <813@ms3.UUCP>, isns02@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@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.