Difference between using fork/execvp and system call

前端 未结 5 1112
小鲜肉
小鲜肉 2021-02-07 12:13

What is the difference between using system() to execute a binary and using the combination of fork/execvp.

Is there any security/portablility/performance difference.

相关标签:
5条回答
  • 2021-02-07 12:32

    system() will fork()/exec() the shell, and then shell will fork()/exec() the program you want to launch.

    So system() is twice as heavy as fork()/exec()

    0 讨论(0)
  • 2021-02-07 12:33

    System also uses a fork/exec... combination. If you do fork/exec yourself you can execute parallel to your running process, while system is blocking (includes the 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).

    0 讨论(0)
  • 2021-02-07 12:44

    Yes, system() runs the command through a shell, while exec() runs the command directly. Of course, introducing a shell opens up for bugs and exploits.

    Edit: of course, the man page provides more detail.

    0 讨论(0)
  • 2021-02-07 12:44

    system() works on Windows but fork() doesn't.

    Unless you use a compatibility layer such as Cygwin, but even then a fork can be very expensive.

    0 讨论(0)
  • 2021-02-07 12:46

    there's also popen(), which is like system(), but allows to read child's output and provide input

    0 讨论(0)
提交回复
热议问题