Subj : Re: Threading and Timeouts To : comp.programming.threads From : Giancarlo Niccolai Date : Thu Jul 21 2005 02:00 am Giancarlo Niccolai wrote: > Marcin 'Qrczak' Kowalczyk wrote: > >> Giancarlo Niccolai writes: >> >> >> POSIX specification for write() says that for pipes and fifos a >> partial write is only possible with O_NONBLOCK set. It implies that >> this is true also for other descriptors which are not files, e.g.: >> Sorry, I forgot to cite: """ Write requests to a pipe or FIFO shall be handled in the same way as a regular file with the following exceptions: [ ... interesting but not concerning select() ... ] * If the O_NONBLOCK flag is clear, a write request may cause the thread to block, but on normal completion it shall return nbyte. """ As you can see, this explicitly says that the same behavior must not be implied for regular files. And in this special case (pipes/fifo writes), it is possible that a blocking FD may block after a successfull select reporting it ready, as this is the only case where writing exactly nbyte bytes "takes precedence". Again, we are talking about blocking in wait for the system to be ready to accept data. I.E. In case of sync i/o write() is forced to wait untill all data are phisically gone on the media. What select() GUARANTEES is that the fd is in a status that, if it were O_NONBLOCK, then it may not return EAGAIN, that is to say, that the system will immediately process *at least* part of the data that can be sent on the fd. In case on sockets write(), which are equivalent to send(), we have also the guarantees for send() that ensure us that if the data can be immediately accepted, then the function will return immediately: """ The send() function shall initiate transmission of a message from the specified socket to its peer. """ and """ The length of the message to be sent is specified by the length argument. If the message is too long to pass through the underlying protocol, send() shall fail and no data shall be transmitted. Successful completion of a call to send() does not guarantee delivery of the message. A return value of -1 indicates only locally-detected errors. """ Long story short: with select on SOCKETS, we'll have send/recv & c. (and write/read on sockets) surely not to block on ready sockets. With write on files may block on ready fd but after the system has accepted data, that is, due to I/O ops and settings(*). With pipes, it may even block even before accepting data; O_NOBLOCK has a special semantic for pipes and FIFO. (*) Actually, write() can block on I/O also with O_NOBLOCK, as """ [talking of fd] Before successful return from write(), the file offset shall be incremented by the number of bytes actually written. On a regular file, if this incremented file offset is greater than the length of the file, the length of the file shall be set to this file offset. """ This won't block as op won't necessarily cause media write, but combined with: """ If the O_SYNC bit has been set, write I/O operations on the file descriptor shall complete as defined by synchronized I/O file integrity completion. """ This may well block. Bests, Giancarlo. .