从服务 Linux c++ 打开应用程序
Opening an application from service linux c++
我想从另一个应用程序 B 打开应用程序 A。通过打开它,我不想在 B 中打开它。我发现了许多从另一个应用程序中调用应用程序的方法。但是,我想做的是同时打开另一个(A(。我怎样才能做到这一点?fork(( 和 exec(( 似乎在 B 中打开了 A。我正在为 Linux 和 Mac 开发代码。建议将不胜感激。谢谢。
在进程 A(父进程(中调用fork()
以创建进程 A 和进程 B(子进程(
在进程 A 中等待 B 退出...
在进程 B 中,再次调用 fork(((创建 B 和 C(并退出
这会导致 A 收集 B 的状态代码并防止它成为僵尸。
B的退出导致C成为孤儿,成为init
的所有者,因此它不再是A的后代。
在进程 C 中,调用 exec
以将当前进程中的程序替换为所需的程序。
像这样:
#include <iostream>
#include <unistd.h>
#include <cassert>
using namespace std;
void forkBtoC()
{
cout << "B forking" << endl;
auto child = fork();
if (0 == child) {
cout << "C execing" << endl;
execl("your-program-here", "arg1", "arg2", nullptr);
}
else {
}
}
int main()
{
// I am parent A
cout << "A forking" << endl;
auto result = fork();
if (result == 0) {
setsid();
forkBtoC();
// I am child B
}
else if (result > 0) {
// I am parent A. result is the pid of B
int B_code = 0;
waitpid(result, &B_code, 0);
cout << "B returned " << B_code << endl;
}
else {
assert(!"failed to fork A");
}
return 0;
}
相关文章:
- 试图在visual studio上用C++创建一个桌面应用程序
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 在C应用程序中运行C++(带有STL)函数
- 使用VerQueryValue检索应用程序的文件描述
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 使用调试/崩溃报告将应用程序部署到客户端
- C++控制台应用程序阻止退出
- 码头化的C++应用程序是否向后兼容早期的内核版本
- 将应用程序从32位移植到64位时出现问题
- 如何改变c++应用程序的视觉效果
- WM_CTLCOLORSTATIC从未在WIN32应用程序中触发
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- phytec phyBOARD iMX-6在从闪存而不是SD卡运行qt5 opengles应用程序时表现不佳(FPS减半
- 为什么导入Mixed native/CLR lib.dll的本机C++应用程序没有在Mixed lib.dll中的外部变
- 如何从Windows应用程序输出到标准?
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 如何在 64 位 vb.net Windows 应用程序中引用 32 位 dll
- 如果整个应用程序是虚拟映射的,为什么 new 会进行系统调用?