[英]Difference between using fork/execvp and system call
What is the difference between using system() to execute a binary and using the combination of fork/execvp. 使用system()执行二进制文件和使用fork / execvp的组合有什么区别。
Is there any security/portablility/performance difference. 是否有任何安全性/可移植性/性能差异。
System also uses a fork
/ exec
... combination. 系统还使用
fork
/ exec
...组合。 If you do fork
/ exec
yourself you can execute parallel to your running process, while system
is blocking (includes the wait
). 如果您自己执行
fork
/ exec
,则可以在运行进程并行执行,而system
阻塞(包括wait
)。 Also system
executes the command not direct, but via a shell (which makes problems with setuid bit) and system
blocks/ignores certain signals (SIGINT, SIGCHILD, SIGQUIT). system
执行命令不是直接的,而是通过shell(这会产生setuid位问题)和system
块/忽略某些信号(SIGINT,SIGCHILD,SIGQUIT)。
Yes, system()
runs the command through a shell, while exec()
runs the command directly. 是的,
system()
通过shell运行命令,而exec()
直接运行命令。 Of course, introducing a shell opens up for bugs and exploits. 当然,引入shell会出现bug和漏洞。
Edit: of course, the man page provides more detail. 编辑:当然, 手册页提供了更多细节。
system()
will fork()
/ exec()
the shell, and then shell will fork()
/ exec()
the program you want to launch. system()
将fork()
/ exec()
shell,然后shell将fork()
/ exec()
你想要启动的程序。
So system()
is twice as heavy as fork()
/ exec()
所以
system()
重量是fork()
/ exec()
两倍
system() works on Windows but fork() doesn't. system()适用于Windows,但fork()不适用。
Unless you use a compatibility layer such as Cygwin, but even then a fork can be very expensive. 除非你使用兼容层如Cygwin,但即使这样,fork也可能非常昂贵。
还有popen(),它类似于system(),但允许读取子的输出并提供输入
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.