How to check if a process is running from C ++ code?
I am writing a C ++ application that will communicate with another process via boost :: interprocess, however I need to check if another process is actually running since the other process is responsible for creating shared memory between processes.How to check if another process is running?
I specifically have to check other processes
source to share
The managed_shared_memory ctor will throw an interprocess_exception in case it cannot open the given shared memory (assuming you passed open_only to the ctor). You can use the error code in the exception to check if shared memory is available or not.
All means to check if a process is running (looking at the process tree, checking magic log files, or whatever) suffers from a race condition that occurs when a remote process is running but has not yet had time to tune shared memory.
Update: If you only want to check if a process is running by the operating system, you need to go through the list of processes and consider each one. Here you can find an example of how to do it.
A simpler, more portable, but less accurate method is to use lock files. Process A creates a magic "lock file" somewhere on startup and deletes it when it finishes. Process B can then check for the presence of this file to determine if process A is running. A zero-byte file is sufficient for this, but the file may also contain additional information that is useful for process B (such as the PID of process A). However, there is a small time window at the very beginning of process A, in which there is no lock file, but the process is running.
source to share