GCC 如何运行其他程序?
也许标题并没有那么准确地表达问题:我知道当我运行 gcc foo.c 时,GCC 会调用其他为其完成所有工作的子程序,使 gcc 主程序只是一个接口。但这到底是如何完成的呢?
它使用 system
或 exec
或其他函数吗?我之所以想知道这个是因为我想构建一个基于类似系统的网络爬虫,其中会有一个界面程序和其他几个子程序,例如crawl
和download.
如果这个问题已经被问过,但我没有使用搜索或“具有类似标题的问题”找到它,我很抱歉。
先感谢您。
Maybe the title does not word the question so precisely: I know that when I run gcc foo.c
GCC calls other sub-programs that do all the work for it, making the main gcc program just an interface. But how exactly is this done?
Does it use system
or exec
or some other function? The reason I want to know this because I want to build a web crawler based on a similar system, where there would be a interface program and several other sub-programs like crawl
and download
.
I'm sorry if this question has already been asked but I didn't find it using search or the "Questions with similar titles".
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然您的问题确实更笼统(并且仅使用
gcc
作为示例),但我的第一个想法是使用strace
来弄清楚它在做什么。在我的系统 (Ubuntu 11.10/x64) 上,我刚刚运行了strace
,如下所示:这显示了
gcc
进程的系统调用,同时跟踪分叉 (-F
)并将跟踪的输出发送到sout
。这样做,我可以看到这里的gcc
调用vfork()
,然后在子进程中调用execve()
,尽管实际程序的源代码可能只需执行一个简单的fork()
/exec()
即可。sout
的相关输出是:每行的开头是正在运行的进程的 pid。因此,主进程调用
stat()
来查找cc1
,然后分叉,子进程执行它。话虽这么说,我本来可以在没有引用的情况下回答你的问题; fork/exec 是从程序中调用子进程的常用方法。
While your question is really more general (and only using
gcc
as an example), my first idea would be to usestrace
to figure out what it's doing. On my system (Ubuntu 11.10/x64), I just ranstrace
, like so:This shows system calls for the
gcc
process, while following forks (-F
) and sending the output of the trace tosout
. Doing this, I can see thatgcc
here callsvfork()
, and thenexecve()
in the child, though the actual program's source might just do a simplefork()
/exec()
.The relevant output from
sout
is:At the begnning of each line is the pid of the process running. So the primary process calls
stat()
to findcc1
, then forks, and the child executes it.That being said, I could have answered your question without the citation; fork/exec is a common way to call sub-processes from your program.