Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
I/O Subsystem<br />
Raw Mode<br />
Raw mode is important for screen<br />
oriented applications.<br />
Figure 39. Raw Mode - Reading 5 character<br />
Bursts<br />
#include <br />
#include <br />
struct termio savetty;<br />
main()<br />
{<br />
extern void sigcatch();<br />
struct termio newtty;<br />
int nrd;<br />
char buf[32];<br />
signal(SIGINT, sigcatch);<br />
if (ioctl(0, TCGETA, &savetty) == -1)<br />
{<br />
printf("ioctl failed: not a tty\n");<br />
exit(0);<br />
}<br />
newtty = savetty;<br />
newtty.c_lflag &= ~ICANON; /* turn off canonical mode */<br />
newtty.c_lflag &= ~ECHO;<br />
/* turn off character echo */<br />
newtty.c_cc[VMIN] = 5; /* minimum 5 chars */<br />
newtty.c_cc[VTIME] = 100; /* 10 see interval */<br />
if (ioctl(0, TCSETAF, &newtty) == -1)<br />
{<br />
printf("cannot put tty into raw mode\n");<br />
exit(0);<br />
}<br />
for (;;)<br />
{<br />
nrd = read(0, buf, sizeof(buf));<br />
buf[nrd] = 0;<br />
printf("read %d chars '%s'\n", nrd, bur);<br />
}<br />
}<br />
void sigcatch();<br />
{<br />
ioctl(0, TCSETAF, &savetty);<br />
exit(0);<br />
}<br />
#include <br />
main()<br />
{<br />
register int i, n; int fd;<br />
char buf[256];<br />
/* open terminal read-only <strong>with</strong> no-delay option */<br />
if ((fd=open("/dev/tty", O_RDONLY | O_NDELAY)) == -1)<br />
exit(0);<br />
n = 1;<br />
for (;;) /* for ever */<br />
{<br />
for (i = 0; i < n; i++)<br />
;<br />
if (read (fd, bur, sizeof(buf)) > 0)<br />
{<br />
printf("read at n %d\n", n);<br />
n--;<br />
}<br />
else /* no data read; returns due to no-delay */<br />
n++;<br />
}<br />
}<br />
Polling<br />
- Can "poll" terminals by opening <strong>with</strong> nodelay,<br />
etc. but processing intensive.<br />
- BSD system has a select system call<br />
select (nfds, rfds, wfds, efds,<br />
timeout)<br />
nfds = number of file descriptors<br />
rfds, wfds, efds = bit masks (read,<br />
write, exceptions)<br />
timeout = how long to wait<br />
Figure 40. Polling a Terminal<br />
150