I have a开发者_如何学Python console application, written on C. Inside this application, I want to determine how many copies of the console application are already launched. Thank you.
I found this googling. It involves some knowledge (not much) of IPC. This works only for Windows. In unix this can be done in a similar manner by using the corresponding unix IPC.
LATER EDIT: Found also this. It counts the number of processes having a given process name (Windows only).
One way is to access your operating system's process list and count how many occurrences of the executable appear. But that could be fooled by multiple copies of the executable with different names.
To make sure you count all occurrences regardless of executable name, you could create a global shared memory object (protected with synchronized access, of course) where each instance registers its process ID. Then you can parse that list and validate against the OS process ID list in case any of the instances died before unregistering themselves.
精彩评论