Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!usenet.ukfsn.org!not-for-mail From: Martin Gregorie Newsgroups: comp.lang.java.programmer Subject: Re: single instance Date: Thu, 3 Jan 2013 21:05:02 +0000 (UTC) Organization: UK Free Software Network Lines: 29 Message-ID: References: NNTP-Posting-Host: 84.45.235.129 Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: localhost.localdomain 1357247102 4445 84.45.235.129 (3 Jan 2013 21:05:02 GMT) X-Complaints-To: usenet@localhost.localdomain NNTP-Posting-Date: Thu, 3 Jan 2013 21:05:02 +0000 (UTC) User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2) Xref: csiph.com comp.lang.java.programmer:20927 On Thu, 03 Jan 2013 09:56:27 -0800, Peter Duniho wrote: > I would guess that Unix and Linux have similar OS-supported techniques > (and may even be the same for those two). So writing two, maybe three > at the most, platform-specific solutions would give you cross-platform > support for the vast majority of clients. > A common approach for *NIXen is to use a file with a known absolute pathname that contains the process PID. The clever trick is that the program that creates the file immediately closes and reopens it for reading and then deletes the file without closing it: this works because file deletion is deferred until there are no longer any processes that have the file open. When a process dies or is killed, all the files it had open are closed. If the file exists and has a different pid to the checker, then another copy is running. There is no clean-up needed: if the user kills the program the file vanishes because it only remains in existence as long as at least one process has the file open. As well as Linux and Unices, this works for a number of other OSen: its certainly good for BSB (and hence OS X), Stratus VOX and, IIRC, Microware's OS-9. -- martin@ | Martin Gregorie gregorie. | Essex, UK org |