By “single instance application”, I assume that you mean a program that can only be run once, and if run again a new instance will not start?
The traditional way to do this is to have the program write out its PID (process ID) to a lock file as soon as it starts up, unless the lockfile already exists.
If the lockfile already exists, then the process should read the PID from it instead. It should then check the file /proc/pid/cmdline, and extract the name of the running process. If it is the same as the name of the current process, then the lockfile should be deleted, and it should try again. Otherwise, it should exit.
There's probably a library that does this for you.
If the program could be run by a different user (and you want to prevent that), then you would normally put the lock file in /tmp, using the various routines to make a temporary file.
Hope that helps,
—Robert J. Lee
|