C++:如何检查外部程序是否正在运行?
我用 C++ 运行一个外部程序:
_wsystem(exec);
如果它运行超过 n 秒,我想终止该进程。我可以在 Python 中这样做:
p = subprocess.Popen(self.temp_exec, shell=True)
cur_time = 0.0
while cur_time < self.time_limit:
if p.poll() != None:
# Kill the process
p.terminate()
break
time.sleep(0.1)
cur_time += 0.1
What's the replacement of p.poll() and p.terminate() in C++?
你
谢谢 涉及 WinAPI 的解决方案也受到欢迎。
I run an external program with C++:
_wsystem(exec);
I want to kill the process if it runs for more than n seconds. I can do it in Python like this:
p = subprocess.Popen(self.temp_exec, shell=True)
cur_time = 0.0
while cur_time < self.time_limit:
if p.poll() != None:
# Kill the process
p.terminate()
break
time.sleep(0.1)
cur_time += 0.1
What's the alternative of p.poll() and p.terminate() in C++?
Thank you
P.S. Solutions involving WinAPI are welcome too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个 MS 知识库条目 描述如何干净地终止应用程序。本质上,如果您只想终止进程而不关心潜在的副作用,那么您可以使用
TerminateProcess
。检查进程是否仍在运行的 Windows API 方法是
GetExitCodeProcess
。There is a MS knowledge base entry describing how to cleanly terminate applications. Essentially if you just want to kill the process and don't care about potential side effects then you can just use
TerminateProcess
.The Windows API way to check if a process is still running is
GetExitCodeProcess
.如果您可以在操作系统级别解决此问题而不是使用 Python。例如,可能会查看
http://devel.ringlet.net/sysutils/timelimit/
或者您man 查看Python的资源模块:
http://docs.python.org/library/resource.html
If you can resolve this issue on the OS level and not using Python. E.g. might look into
http://devel.ringlet.net/sysutils/timelimit/
Or you man check the resource module of Python:
http://docs.python.org/library/resource.html
我只能评论 Unix,因为那是我最了解的平台。
p.poll()
变为kill(pid, 0)
p.terminate()
变为kill(pid, SIGTERM)
代码>I can only comment on Unix, since that's the platform I know best.
p.poll()
becomeskill(pid, 0)
p.terminate()
becomeskill(pid, SIGTERM)