Test to see whether a named process is running - platform independent
I'm trying to find a way of testing whether my process is already running when I try to spin it up. If it is already running, I then need to kill it. The problem is that the application is going to be deployed on both Windows XP and RHEL/CentOS 5 boxes.
I have been able to track down OS-specific ways of testing to see if a named process is running, but is there any way to do it independent of operating system?
We have been making heavy use of the ACE libraries, which allows us to perform a lot of operations which would usually be os-specific in a portable way, but I can't seem to fund a way of getting the process list.
Mutexes are process specific
I did think about putting a giant mutex around the main function, but the problem is that a mutex guarantees mutual exclusion for threads within the same process. Two processes will have separate instances of the mutex.
I have thought of three ways:
1) Write the process id to file when the program spins up. Delete it when the programs stops and write a seg fault handler to delete it when the proram seg faults. If the file exists, another process is already running. This is good, but would require a fault handler, which I don't have a great deal of experience with.
2) Have a batch file/bash script to kick off the application. Obviously the script would be OS specific. It would work, it would be quick to do, but it's not an elegant solution.
3) Implement two versions of the class in one file with massive pre-processor guards, so that one complete implementation is for windows and the other for linux. Again, not so elegant, but it would work. The main issue is working out which OS we are running under at run time.