Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
665 views
in Technique[技术] by (71.8m points)

process - In a C++ application in Mac or Windows, is there a way to determine if a given ProcessId still exists?

I have a QT application that runs on both Windows and Mac. Is there a way to provide a PID, and get back:

  1. If the PID exists
  2. The name of the process under that PID

For Windows I thing I can use EnumProcesses() to find out what I need, but how can this be done on Mac?

question from:https://stackoverflow.com/questions/65909336/in-a-c-application-in-mac-or-windows-is-there-a-way-to-determine-if-a-given-p

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So it's a QT question. That constrains the space enough that I can provide an answer for the most reasonable case.

A good portable program probably does not monitor arbitrary processes but only its own; and for that everything is well in hand. Child processes are spawned using the QProcess class, and it provides of itself what you need in the form of the stateChanged signal. You don't ever ask if the process ID is good; you get told when it's no longer good.

If you insist on checking if the PID is good; you're in luck. If /proc/self exists but /proc/${pid} doesn't than the PID isn't good anymore. This works about as well as EnumProcesses on windows. You can get the process name from /proc/${pid}/exe (it's a symbolic link, so call readlink() on it). If /proc/self doesn't exist, you probably can't enum processes because the system is in a degenerate state.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...