HELP: Low level terminal I/O
Jim Barton
jmb at patton.sgi.com
Tue Aug 1 02:48:07 AEST 1989
In article <8907201857.aa12017 at SMOKE.BRL.MIL>, mis at 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 <mis at psych.Stanford.Edu>
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 at sgi.sgi.com, sgi!jmb at decwrl.dec.com, ...{decwrl,sun}!sgi!jmb
More information about the Comp.sys.sgi
mailing list