One of the daemons I wrote opened a UNIX domain socket for normal communication with the daemon client. Other instances then checked to see if they could connect to this socket. If they could, another instance is currently working. Edit: As @psmears pointed out, there is a race condition. Other instances should simply try to create the same listening socket. This will fail if it is already in use.
Lock files work more often than this special case. You can create a (empty) file in a well-known place, and then use file locks, say with fcntl(2) and F_SETLK and F_GETLK to lock this file or determine if the lock is locked. May not work on NFS. Locks are cleared when your process dies, so this should work and carry over (at least to HP-UX). Some daemons like to upload pid to this file if they determine that another instance is not running.
source share