如何在不等待调用返回的情况下进行系统调用并恢复执行? [英] How do I make a system call and resume execution without waiting for the call to return?
问题描述
基本上我想使用 system()、exec()、反引号或其他东西来进行系统调用,但随后立即在调用脚本中恢复执行而不关心调用的结果以及它是否返回,死亡,停止,等等.
Basically I want to use system(), exec(), back-ticks or something to make a system call, but then to immediately resume execution in the calling script without caring about the result of the call and whether or not it returns, dies, stalls, whatever.
是否可以在没有线程/分叉的情况下做到这一点?
Is it possible to do this without threading/forking?
推荐答案
简短的回答,没有.这是fork()
的全部点.唯一的问题是你是直接调用 fork()
还是让其他东西帮你做.
Short answer, no. This is the entire point of fork()
. The only question is whether you call fork()
directly, or get something else to do that for you.
如前所述,您可以使用 shell 的后台操作符 (&
) 来执行此操作,但是您正在使用 shell,这会带来通常的字符串注入攻击问题>
As mentioned, you could use the shell's backgrounding operator (&
) to do that, but then you're using the shell, and that comes with the usual string-injection attack problem
system("some command with args &");
更直接地,您可以只执行通常的 fork()
和 exec()
,然后不执行通常的 waitpid()
发生,这就是阻塞发生的地方:
More directly, you could just do the usual fork()
and exec()
and then not perform the waitpid()
that normally happens, which is where the blocking occurs:
if(fork() == 0) {
exec("some", "command", "with", "args") or die "Cannot exec - $!";
}
# No waitpid here so no waiting
如果您正在这样做,最好还放入一个 SIGCHLD
处理程序以忽略子进程最终退出时的情况,以免让僵尸四处游荡.在代码开头附近的某个位置,放置
If you're doing that, best also to put a SIGCHLD
handler in to ignore when the child does eventually exit, so as not to leave zombies hanging around. At some point near the beginning of the code, put
$SIG{CHLD} = 'IGNORE';
这篇关于如何在不等待调用返回的情况下进行系统调用并恢复执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!