I need a SysV select()

Charles Green charles at c3engr.UUCP
Sat May 21 12:30:52 AEST 1988


In article <11536 at mimsy.UUCP> jds at mimsy.umd.edu (James da Silva) writes:
>>In article <9738 at sol.ARPA> jpayne at cs.rochester.edu (Jonathan Payne) writes:
>>>I want to get input
>>>from either the keyboard or from some other file description.  I don't
>>>want to sit around polling the two, because that would be ridiculous.

>The natural way to handle this in System V is to use two processes, each of
>which is waiting on one file descriptor.  You can then use shared memory
>and/or signals, semaphores or message queues to get the data around, to taste.

My favorite solution is to have several small "multiplexor" processes, all
identical but exec()ed with a unique numeric argument, reading their respective
stdins and all writing into the same pipe that's read by the main process.
When one of the multiplexor processes completes a read(), he does a single
write() to the pipe containing his unique ID, a byte count, and the data.

The main process sleeps on a read() of the pipe of a size sufficient to pick up
the "header" (ID and byte count).  When he gets one, he issues another read()
to pick up the data.

This scheme works because a write() to a pipe is atomic; data from the other
multiplexor processes won't be interspersed with it (as opposed to TTYs...)
However, you have to be able to accept data from all "channels" simultaneously;
you can't selectively read data from one channel and not another.

	fd >----> [mux] >--
			   \
	fd >----> [mux] >---+--> (pipe) >--> [main process]
			   /
	fd >----> [mux] >--
-- 
#ident	"@(#).signature	1.5 :~charles/s..signature 04/27/88 21:40:43"
charles at c3pe.UUCP				charles_green%c3gate at c3pe



More information about the Comp.unix.wizards mailing list