我正在从Android Linux中的另一个可执行文件调用一个可执行文件。以下是相关代码:
...
int status = system("/system/bin/executable");
...
Run Code Online (Sandbox Code Playgroud)
我的要求是不要等到executable完成执行。意味着我想executable独立于调用它的可执行文件运行。
我已经在互联网上进行搜索,但是没有找到如何使该系统调用不受阻碍的方式。请帮我解决。
system()没有错误处理的函数如下所示:
int system(char const *cmdline)
{
pid_t pid = fork();
if(pid == 0)
{
char const *argv[] = { "sh", "-c", cmdline, NULL };
execve("/bin/sh", argv, NULL);
_exit(1);
}
else
{
int status;
waitpid(pid, &status, 0);
return status;
}
}
Run Code Online (Sandbox Code Playgroud)
该命令本身由外壳程序解析,因此您可以使用常规&后缀将命令发送到后台。然后,外壳程序立即终止,将后台程序重新设置为PID 1(因此您的程序不负责收集僵尸),然后system()返回。