From: john hall <jhall@netcomuk.co.uk>
Subject: Re: "producer - consumer" problem
Date: 1997/12/13 [thread overview]
Message-ID: <3491E458.B@netcomuk.co.uk> (raw)
In-Reply-To: 348EF272.E3B@cnam.fr
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
prev parent reply other threads:[~1997-12-13 5:00 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-12-10 5:00 "producer - consumer" problem Benoit JACROT
1997-12-13 5:00 ` john hall [this message]
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox