Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!sgi!jmb@patton.sgi.com From: jmb@patton.sgi.com (Jim Barton) Newsgroups: comp.sys.sgi Subject: Re: HELP: Low level terminal I/O Summary: It can be done! Message-ID: <39240@sgi.SGI.COM> Date: 31 Jul 89 16:48:07 GMT References: <8907201857.aa12017@SMOKE.BRL.MIL> Sender: daemon@sgi.SGI.COM Organization: Silicon Graphics, Inc., Mountain View, CA Lines: 56 In article <8907201857.aa12017@SMOKE.BRL.MIL>, mis@APL.STANFORD.EDU (Misha Pavel) writes: ... > I need to read a tty line and return immediately > even if there is nothing in the system buffer for that tty. > To do that I set the terminal in raw mode with the folowing > settings: > > term.c_iflag = IGNBRK; > term.c_oflag = 0; > term.c_lflag = 0; > term.c_cflag = B9600 | CS8 | CREAD | PARENB | PARODD; > term.c_cc[VMIN] = 1; > term.c_cc[VTIME] = 0; > ... > Misha Pavel Several others have posted valid solutions to this problem, but there is one valid one which is portable (although somewhat ugly) and used by many programs out there. You have the VMIN and VTIME settings backwards. The proper way to do this is to set: term.c_cc[VMIN] = 0; term.c_cc[VTIME] = 1; This tells the terminal driver to return if no characters have arrived in the last 1/10 second, thus your program would sleep for 1/10 of a second each time it polled the variable. The proper way to read a single character is then to perform the read as: if (read(fd, &c, 1) == 1) /* got a character */ ... This style of handling the terminal is oriented towards efficient yet responsive data passing. For instance, let's say I write a program which reads from the tty line and writes to a file (or a pty, for instance). In that case, I could set up a small buffer to take either a maximum number of characters or time out after a certain point: term.c_cc[VMIN] = 0; term.c_cc[VTIME] = 2; ... while ((count = read(ifd, buf, 10) > 0) write(ofd, buf, count); This makes for high throughput while still insuring that single characters make it through quickly. It is useless in real-time applications. As Vernon and Mike pointed out, IRIX is fairly rich in ways to do this operation while still being real-time. -- Jim Barton Silicon Graphics Computer Systems "UNIX: Live Free Or Die!" jmb@sgi.sgi.com, sgi!jmb@decwrl.dec.com, ...{decwrl,sun}!sgi!jmb