questions about read()

Joel Coltoff coltoff at PRC.Unisys.COM
Thu Jul 7 04:59:27 AEST 1988


I have some questions about the subtle workings of read() under 4.3
BSD. I'm talking through sockets to a server that forks and execs an
interactive application (shown below). The real application will be
more substanstial after the remote end is finished.

#include <stdio.h>

main()
{
	char buf[100];
	setlinebuf(stdout);
	while (gets(buf)){
		if ( strncmp(buf, "bye", 3) == 0 )
			break;
		printf( "%s\n", buf );
/*
 * Write out garbage data
*/
		printf( "************\n", buf );
	}
	exit(0);
}

The remote end loops doing select() and read(). What I'm noticing is
that each read() seems to get a single line. I can drastically change
the behavior by changing the buffering that the application uses. What
I really want is to send a block of data and have the read() get it all
in one pass. I seem to be able to do this with a fflush(stdout) at the
bottom of the loop.

How does read() decide when to return? The manual page says that for
"normal" files read() guaranteed to read the requested number of bytes
if that many bytes are there. Is normal a disk file? What happens is a
signal interrupts a read()? Does read() return with what it got so far
and set EINTR or isn't a socket considered to be a slow device? What
other questions should I be asking here?

Please respond by email and if there seems to be enough interest I'll
summarize to the net. Thanks in advance.
-- 
	- Joel
		{psuvax1,sdcrdcf}!burdvax!coltoff	(UUCP)
		coltoff at burdvax.prc.unisys.com		(ARPA)
-- 
	- Joel
		{psuvax1,sdcrdcf}!burdvax!coltoff	(UUCP)
		coltoff at burdvax.prc.unisys.com		(ARPA)



More information about the Comp.lang.c mailing list