Questa è una vecchia versione del documento!
Implementazione delle funzioni "readn" e "writen"
(tratto da “Advanced Programming In the UNIX Environment” 3rd Edition by W. Richard Stevens and Stephen A. Rago, 2013, Addison-Wesley)
readn and writen Functions
Pipes, FIFOs, and some devices—notably terminals and networks—have the following
two properties.
1. A read operation may return less than asked for, even though we have not
encountered the end of file. This is not an error, and we should simply continue
reading from the device.
2. A write operation can return less than we specified. This may be caused by
kernel output buffers becoming full, for example. Again, it’s not an error, and
we should continue writing the remainder of the data. (Normally, this short
return from a write occurs only with a nonblocking descriptor or if a signal is
caught.)
We’ll never see this happen when reading or writing a disk file, except when the file system runs out of space or we hit our quota limit and we can’t write all that we requested.
<code c> ssize_t /* Read “n” bytes from a descriptor */ readn(int fd, void *ptr, size_t n) {
} <\code>
Generally, when we read from or write to a pipe, network device, or terminal, we need to take these characteristics into consideration. We can use the readn and writen functions to read and write N bytes of data, respectively, letting these functions handle a return value that’s possibly less than requested. These two functions simply call read or write as many times as required to read or write the entire N bytes of data.