From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: To: 9fans@cse.psu.edu Subject: Re: [9fans] echo -n From: Joel Salomon Date: Mon, 20 Nov 2006 11:37:51 -0500 In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Topicbox-Message-UUID: e2776aac-ead1-11e9-9d60-3106f5b1d025 > No, [a zero-length write] doesn't close the pipe. It sends a > zero-length message, causing the reader to get a zero-length message > returned from read. The pipe is still completely usable; the problem > is that the reader interprets this zero return as EOF, as is the > convention. > > It's too bad that both of these conditions are signaled the same way, > but the pipe is *not* closed. > > There are two options available for dealing with this: change your > writer not to write empty messages (as you have), or change the reader > to ignore them. For the latter, just ignore a return value of 0 and > read again. If the pipe is really closed, then after returning 0 > three times, read will start returning -1. Why is read from a closed pipe not considered an error, though? close(fd[0]); for(int i=0; i<5; i++){ n = read(fd[1], buf, 8); print("%d: read %ld bytes: %r\n", i, n); } produces: 0: read 0 bytes: 1: read 0 bytes: 2: read 0 bytes: 3: read -1 bytes: i/o on hungup channel 4: read -1 bytes: i/o on hungup channel where I expected errstr to be set on the first read after pipe closure. Is there really no way to immediately determine that a pipe has been closed? Why would read be so implemented? --Joel