Detecting Closed Sockets

Chris Torek torek at elf.ee.lbl.gov
Sun Mar 10 21:06:25 AEST 1991


In article <39989 at cup.portal.com> LordBah at cup.portal.com
(Jeffrey J Vanepps) writes:
>Program R is a communications router.  It accepts connections from data
>producers and data consumers.  Normally, it waits until data is available
>from some producer, reads the data, and then writes the data to each
>consumer who requested the type of data being generated by that producer.
>However, data is not allowed to be lost or thrown away, so if a producer
>produces data for which there is not yet a consumer, then R does not
>read that data.  It is left in the socket buffer, eventually blocking
>the producer.  
>
>Now, after this has happened, and there is a buffer full of data waiting 
>to be read from a producer, R has no way to tell that the producer process 
>has exited.  Normally when one side of a socket connection exits, the
>other side is told via select(2) that there is data to be read, but the
>recv(2) call returns zero bytes.  But in the case described above, R can't
>try to recv because it has nowhere to put the data.

There is something missing from the above problem specification, because
as given, there is nothing wrong, nothing to fix.

If data may never be discarded, then:

	producer P runs, outputs data of type T
	producer P exits
	several hours later, consumer C requests data of type T

There also appears to be a potential race condition:

	producer P runs, outputs data of type T
	consumer C1 runs, requests data of type T, gets first two items
		(which thus disappear from the queue)
	consumer C2 runs, requests data of type T; now both C1 and C2
		get the remaining items.  C2 has lost out due to a race.

If the missing goal above is to have router R spawn new producers, then:

   - R can be the parent of *all* producers
     (and thus use wait3() or wait() to detect vanished ones)

or

   - Each producer P can also have a second socket to router R, one
     which exists only to identify P to R initially and then has no
     further data written to it.  When P exits, this socket will see
     an end-of-file condition.

If producers and consumers run on different machines than R, some sort
of keep-alive mechanism may be required as well, since a broken
connection and an idle one are otherwise completely identical.
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab EE div (+1 415 486 5427)
Berkeley, CA		Domain:	torek at ee.lbl.gov



More information about the Comp.unix.programmer mailing list