popen creates an extra sh process

旧街凉风 提交于 2019-12-06 11:50:42

You should listen to the good folks who are advising you not to use popen - it's bad. But there is a simple fix for the issue you've encountered - add exec to the beginning of the command line you pass to popen. That is, instead of:

popen("my_command /home/war", ...

use:

popen("exec my_command /home/war", ...

popen uses sh to spawn the child process, like system does on POSIX systems.

If you want to avoid it, just use fork, close, mkpipe and exec (which is more or less what popen does internally). If you don't need the pipe you can just fork and exec.

As far as popen is concerned, it is supposed to invoke the shell (read the manpage)

To skip the shell process, you can do a fork/exec

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!