简体   繁体   中英

(LINX-IPC protocol) How to find out if a process with a PID is running in C++?

I have a PID of a LINX process (obtained through 'linxstat' command) that I want to send a LINX signal ( http://linx.sourceforge.net/linxdoc/doc/html/linx.7.html ) to:

Cmn::Ipc<ASig> sig(A_SIG);
sig->a = 10;
sig->b = 20;
sig.send(PID);

Now, before I send the LINX signal to the PID, I want to make sure that the PID exists and is still running (making sure the process hasn't crashed). If the PID does not exist, then I'll send it to another common process which has a PID of 0 and this process can handle any type of signal. Is there a way to check if the PID exists in C++? If yes, what is the LINX API for it?

I want to be able to do this without having to know the process name and with just the PID of the process.

Anywhere between the code that gets the PID, performs the PID check and sends a signal target process can exit and the new unrelated process with the same PID can spawn as range of PID numbers is limited and they are reused over time. So I think it is not possible to achieve what you want with just PID.

You can send signal 0 first:

kill(2):

If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for the existence of a process ID or process group ID.

But it's probably pointless. Moreover, this kind of pid-based access is rather race-condition prone unless you're the parent of the process (in which case you always know whether or not it ended and whether or not you've reaped it).

No matter what you do to check that it is currently running, there's no guarantee that it won't crash between the time you check and the time you send the signal - or, between the time you send the signal and the time it arrives. So, just send the signal and find a way to deal with the fact that the process may not be there when it arrives. In extreme cases the process may even die and its PID be re-used before the signal arrives - have fun dealing with that..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM