From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,3d7c99651d0c9cf5 X-Google-Attributes: gidfee84,public From: john hall Subject: Re: "producer - consumer" problem Date: 1997/12/13 Message-ID: <3491E458.B@netcomuk.co.uk>#1/1 X-Deja-AN: 297716029 References: <348EF272.E3B@cnam.fr> X-Complaints-To: abuse@corp.netcom.net.uk X-Trace: taliesin.netcom.net.uk 881976547 14654 194.42.232.137 (13 Dec 1997 01:29:07 GMT) Organization: None. (via NETCOM Internet Ltd. USENET service). Newsgroups: comp.unix.osf.osf1 Benoit JACROT wrote: > I have a "producer - consumer" problem : N processes write to a file and > one process read this file. To stop reading, the consumer wants to know > when all producers are dead. > To solve this problem I see two solutions : > - trying to know how many processes have the file open (not possible > ?), > - reading the process table and look for any producer, but having to > make assumptions about their name. > How can I implement these solutions (under OSF1), are there any other > solutions ? > Thanks. How about using an advisory lock like flock(). The producers each get a _read_ lock (NOT a write lock) on a file - the one that is being written to will do. The consumer attempts (periodically, or via a separate thread or process) to get an exclusive _write_ lock on the file. When it succeeds, it assumes all the producers have left the building. File locks have the attractive property that an exited process releases all locks without specific user program actions. You may need to watch out for race conditions, e.g.: - producers starting just after all others have exited, allowing the consumer to get a file lock in a small time window and thus assuming that all producers have finished. - the consumer starting before any producers, getting an exclusive write lock, and assuming that there will be no more producers. HTH regards john hall