multiple instance on Unix

N

Nigel King

Hi,
I have (my son has!) implemented protection against multiple instances
causing havoc by creating a directory. This fails if it exists and thus
in a single instruction one gets both the acquire and the test.

Windows has it's mutex which solves the problem. Is there any better
version for UNIX.

Thanks

Nigel King
 
J

Jorgen Grahn

Hi,
I have (my son has!) implemented protection against multiple instances
causing havoc by creating a directory. This fails if it exists and thus
in a single instruction one gets both the acquire and the test.

I assume you want a program (when started) to exit immediately if another
instance is running, as the same user, on the machine?
Windows has it's mutex which solves the problem. Is there any better
version for UNIX.

Ok, so Windows has named mutexes which work across processes.

Unix programs normally don't attempt to "protect" themselves in this way. I
can see no harm in two instances of a program running. I can /definitely/
see situations where two programs modifying the same set of files, resulting
in binary garbage, but if these are different programs your approach doesn't
work.

Best is a design which doesn't need locking. Second best is locking which is
tied to the resources (files) which need to be protected. Leaving it up to
the user to be careful is another popular choice ;-)

For reference, have a look at procmail's lockfile(1) and mutt's
mutt_dotlock(1). Both deal with protecting mailbox files.

/Jorgen
 
G

Grant Edwards

Unix programs normally don't attempt to "protect" themselves
in this way.

Some do, some don't. Many system daemons want to make sure
that only a single instance is running. That's one of the
functions of all the *.pid files in /var/run. It's less common
that user programs try to enforce such a restriction, but it
happens. It's usually done by creating a lock file in the
user's home directory if it's a per-user restriction.
I can /definitely/ see situations where two programs modifying
the same set of files, resulting in binary garbage, but if
these are different programs your approach doesn't work.

Best is a design which doesn't need locking. Second best is
locking which is tied to the resources (files) which need to
be protected.

Agreed, but creating a lockfile to make sure only a single
instance of the program is running is the standard third
choice.
Leaving it up to the user to be careful is another popular
choice ;-)

Yup.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top