c++ system() 孤儿 &僵尸进程
我有一个小应用程序,我们称之为“launch.exe”。它是一个 C++ 应用程序。
我在其中所做的是调用 system() 3 次来启动其他 3 个应用程序。我们将它们称为 A、B 和 C。
问题 #1
A、B 和 C 是 GUI 应用程序,“launch.exe”在 A 退出之前无法继续运行。然后再次卡住,直到B退出。然后再次卡住,直到C退出。我希望 lauch.exe 能够在我打开的应用程序保持打开状态时继续运行。
问题#2
假设我能够找到问题#1的解决方案,在启动A、B和C之后,我不希望“launch.exe”保持打开状态。我希望 launch.exe 关闭,而 A、B 和 C 继续运行。
这是一个适合您的场景。假设“launch.exe”仅启动一个应用程序(我们称之为 A)。然后,在 A 启动后,如果我关闭“launch.exe”,A 仍保持打开状态。
好吧...这就是我想要的,但是刚刚发生了什么? A现在是孤儿了吗?如果是这样,这是一个问题吗?
如果我在退出 launch.exe 之前关闭 A 会怎样?表面上看起来没问题,但是返回什么呢?如果我在 cmd shell 中启动了一个 exe,它会返回到那个,但由于我是通过 c++ appl 中的 system() 调用完成的,它会返回到我的 lauch.exe 还是会变成僵尸?
注意:
为什么我要使用 system()?
--因为我需要一些兼容 Windows/Linux 的东西。
--因为我需要将某些正在启动的应用程序的权限提升到管理员级别。
--我应该补充一点,A、B 和 C 完全独立至关重要(出于安全原因,它们不应共享相同的内存空间或其他任何内容)。
--最后,一些应用程序 B 和 C 是多线程的(我声明这一点是因为我读到某些函数不能正确生成多线程应用程序。我不清楚原因。)。
I have a little application, let's call it "launch.exe". It is a c++ appl.
What I do in it is I call system() 3 times to launch 3 other applications. let's call these A, B, and C.
problem #1
A, B, and C are GUI apps and "launch.exe" is not able to progress until A exits. Then it is stuck again until B exits. Then stuck again until C exits. I would like lauch.exe to be able to progress while the applications I have opened remain open.
Problem #2
Assuming that I am able to figure out a solution to problem #1, after A, B, and C are launched, I don't want "launch.exe" to stay open. I want launch.exe to close and I want A, B, and C to remain running.
Here is a scenario for you. Lets us say "launch.exe" only starts one application (let us call it A). Then, after A is started, if i close "launch.exe", A remains open.
OK...this is what I want but what just happened? Is A an orphan now? And if so, is this a problem?
And what if I closed A before I exited launch.exe? On the surface it seems OK, but what does it return to? If I launched an exe in cmd shell, it would return to that, but since I did it from a system() call in a c++ appl, does it return to my lauch.exe or does it become a zombie?
NOTES:
Why am I useing system()?
--Cause I need something that is Windows/Linux compatible.
--Cause I need to elevate privileges to admin level for some of the applications being launched.
--I should add that it is vital that A, B, and C be totally independent (for security reasons they should not share the same memory space or anything else).
--Last, some of the apps, B, and C are multi-threaded (I state this because I have read that some functions do not spawn multi-threaded applications properly. I'm not clear on reasons why.).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
spawn
代替,这不会阻止启动器直到子进程退出。或者,由于您已经在使用 Qt,因此请使用
QProcess
。没有可移植的方法来以不同的用户身份生成子进程,但 Windows 特定的方法是
CreateProcessWithLogonW
。Use
spawn
instead, this won't block the launcher until the child exits.Or, since you're already using Qt, use
QProcess
.There is no portable way to spawn a subprocess as a different user, but the Windows-specific way is
CreateProcessWithLogonW
.为什么不使用 & 符号“&”来启动 ABC 流程呢?附加到命令参数
这样您的启动器就不会等待这些进程退出。
然后使用
QApplication::exit
或QApplication::quit
退出启动器阅读此SO 问题 查看 fork/execvp 和 system() 之间的区别。
Why don't you start your A B C processes with ampersand "&" appended to command parameters
This way your launcher will not wait for these processes to exit.
Then exit your launcher with
QApplication::exit
orQApplication::quit
Read this SO question to see the difference between fork/execvp and system().