C++ 到 system() 或 fork()/exec()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14830611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
to system() or fork()/exec()?
提问by Sparky
There appear to be two common ways of running an external executable from C in unix, the
在 unix 中从 C 运行外部可执行文件似乎有两种常用方法,即
system()
call and
打电话和
pid = fork()
switch(pid)
//switch statement based on return value of pid,
//one branch of which will include and exec() command
Is there any reason to prefer a fork/exec over system in the case where they are functionally equivalent (parent process waits for child to finish, no complex information is returned from child)?.
在功能等效的情况下(父进程等待子进程完成,子进程没有返回复杂信息),是否有任何理由更喜欢 fork/exec 而不是系统?
回答by Fred Foo
system
executes a command-interpreter, i.e. a shell, which (a) is slower than a direct fork/exec, (b) may behave differently on different systems and (c) is a potential security hazard if you pass it a string from an untrusted source. Also, system
waits for the child process to exit, while you might want it to run concurrently with the parent process.
system
执行一个命令解释器,即一个 shell,它 (a) 比直接 fork/exec 慢,(b) 在不同的系统上可能表现不同,(c) 如果你从不受信任的人那里传递一个字符串,这是一个潜在的安全隐患来源。此外,system
等待子进程退出,而您可能希望它与父进程同时运行。
More in general, the low-level fork/exec gives you additional control: before or in between the two operations, you might want to chdir
, open pipes, close file descriptors, set up shared memory, etc.
更一般地说,低级 fork/exec 为您提供了额外的控制:在两个操作之前或之间,您可能想要chdir
打开管道、关闭文件描述符、设置共享内存等。
(By different systems, I don't mean Windows vs. Unix (as Windows doesn't even have fork): I'm talking Red Hat Linux vs. Ubuntu. The former uses Bash to execute what is passed to system
, the latter a lightweight POSIX-compatible shell.)
(对于不同的系统,我不是指 Windows 与 Unix(因为 Windows 甚至没有 fork):我说的是 Red Hat Linux 与 Ubuntu。前者使用 Bash 执行传递给 的内容system
,后者是轻量级的 POSIX 兼容外壳。)
回答by John Zwinck
fork()
creates a new process. If you don't need to do that, just use system()
(or popen()
). You might want a second process to achieve parallelism, or for finer-grained control over the job, but often you just don't care for that if the job is meant to be synchronous.
fork()
创建一个新进程。如果您不需要这样做,只需使用system()
(或popen()
)。您可能需要第二个进程来实现并行性,或者对作业进行更细粒度的控制,但如果作业是同步的,您通常并不关心这一点。
On the other hand, I find that 95% of uses of system()
are unnecessary or would somehow be better off done another way (e.g. using zlib instead of system("gzip")
). So maybe the best answer is to use neither!
另一方面,我发现 95% 的使用system()
是不必要的,或者以某种方式做得更好(例如使用 zlib 而不是system("gzip")
)。所以也许最好的答案是两者都不使用!
回答by alk
Going via system()
additionally invokes a shell process, which might not be what you want.
去通过system()
另外调用一个shell进程,这可能不是你想要的。
Also the calling process is notified only when such shell dies not when the actual process run by the shell died.
此外,调用进程仅在此类 shell 终止时才会收到通知,而不是在 shell 运行的实际进程终止时通知。
回答by Shark
system() will type out the command and execute it like a user would have typed out.
i mostly saw it like system("pause"); system("cls");
system() 将输入命令并像用户输入一样执行它。我大多是这样看的system("pause"); system("cls");
But if you need to control the child process, you want to fork.
但是如果你需要控制子进程,你要fork。