buffer i/o using read(2) on BSD sockets

amos shapira amoss at batata.huji.ac.il
Sat Mar 24 23:04:47 AEST 1990


ka at cs.washington.edu (Kenneth Almquist) writes:

>I presume that the idea of having the read system call return a short
>count originally appeared in UNIX to deal with terminal input.

This isn't the only possible reason, what about reading to the end of a
file? When a process tryes to read more bytes than avilable in the file then
the same event would apear (i.e. the amount of bytes returned is less than
the amounts requested).

[ stuff deleted ]

>Berkeley sockets intentionally copied the pipe semantics, so that
>pipes could be implemented as a special case of sockets.

I doubt this, the Berkeley sockets were devised solely for the purpose of
letting processes talk to the network devices (that's why they were financed
by DARPA) and they have nothing to do with the way data is transfered through
them. ONE derivation of the socket mechanisem is the socketpair, which was
copyed also into the implementaion of the pipe() system call. The read()
and write() system calls were changed to support sockets mainly to let
"naive" processes use sockets without knowing about them, this also seems
to me as a good example of sticking to the UNIX dicipline that processes
shouldn't care too much were their input/output comes-from/goes-to.
For more info read "The Design and Implementation of the 4.3BSD UNIX(tm)
Operating System" by Laffler, McKusick, Karels and Quarterman, chapters 10
to 12, note that the description of the sockets mechanism is completly
separate from the other realated layers.

>If you have to read a specific number of characters under UNIX, there
>are two ways to do it.  One is to place a loop around the read system
>call.  The other is to use the fread routine and let the standard I/O
>library take care of the buffering.
>     Kenneth Almquist

There is an ioctl() functions called FIONREAD which will return the number
of immidietly avilable bytes to read (if you read the book mentioned above,
then this is the so_rcv.sb_cc field). Note that this is implemented in the
socket layer and not in the protocols layer (sys/sys_socket.c line 75 in
4.3BSD).

Cheers,
- Amos Shapira

amoss at batata.bitnet
amoss at batata.huji.ac.il



More information about the Comp.unix.wizards mailing list