empty() for USG UNIX ?

Kenneth Almquist ka at june.cs.washington.edu
Wed Nov 9 11:28:56 AEST 1988


Dan Ts'o <dan at rna.rockefeller.edu> asks for a  System V routine to test
whether data is available to be read on a file descriptor referring to a
slow device (such as a tty).  I doubt that such a routine can be written.
Depending on what you are doing, you may be able to use FNDELAY mode to
get the same effect.  Code like:

	ioctl(fd, FIONREAD, (char *)&nchars);
	if (nchars) {
		count = read(fd, buf, sizeof(buf));
		Code to process input.
	} else {
		Do something else.
	}

becomes:

	fcntl(fd, F_SETFL, FNDELAY);
	if ((count = read(fd, buf, sizeof(buf))) != 0) {
		Code to process input.
	} else {
		Do something else.
	}

These two pieces of code are approximately equivalent, but the former
contains a race condition.  (The fact that characters are available to
be read when the FIONREAD ioctl is performed doesn't mean that these
characters will still be around by the time the code gets around to
performing the read.)  So the latter version of the code is preferable
on systems that support both.
				Kenneth Almquist



More information about the Comp.bugs.sys5 mailing list