[comp.unix.questions] process sync - can it be portable?

davidsen@steinmetz.ge.com (William E. Davidsen Jr) (01/20/89)

I have a program which will eventually run a number of copies all
sharing a number of files. My problem comes with locking... I want this
to run on as many types of system as possible, without excessive #if's.
I need to do a little access syncing with the following requirements:
	1) lock with wait - if already locked pause until free
	2) self unlocking if the process with the lock dies.

Now before I get 20 messages telling me what I already know, YES I can
do it on BSD, on SysV, Xenix, Ultrix, and SunOS. What I want is a single
portable method which will work from the same source code in each. In
the past I've used semiphores, simple locks, fancy locks (just parts of
a file), etc, messages, pipes, and shared memory for IPC. The problem is
that I want this to be portable.

Anyone got a good idea. Note that preserving order on multiple waiting
processes is not required. Long before locks become a bottleneck on this
program the system will run out of something else.

I will summarize mail, but would prefer to have replies posted to
simulate other ideas. I'll even take help from one of the "any fool
plainly knows that..." types at this point.
-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me

gwyn@smoke.BRL.MIL (Doug Gwyn ) (01/21/89)

In article <12992@steinmetz.ge.com> davidsen@crdos1.UUCP (bill davidsen) writes:
>I have a program which will eventually run a number of copies all
>sharing a number of files. My problem comes with locking...
>The problem is that I want this to be portable.

The general approach to such requirements is to define one's own portable
interface to a necessarily system-dependent implementation of the facility.

The BRL CAD package defines a "resource" structure and a set of macros/
functions for acquiring/releasing resources.  Something of the sort is
probably your best bet.