GCC如何运行其他程序? [英] How does GCC run its other programs?

查看:147
本文介绍了GCC如何运行其他程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许标题并没有如此精确地提出问题:我知道当我运行 gcc foo.c 时,GCC会调用其他所有为其工作的子程序,使主要的gcc程序只是一个界面。但是到底做得如何?



是否使用系统 exec 或其他一些功能?我想知道这个的原因是因为我想构建一个基于类似系统的网络爬虫,其中有一个接口程序和其他几个子程序,比如 crawl 下载



对不起,如果这个问题已被问到,但我没有使用搜索或标题类似的问题。



预先感谢您。

解决方案

更一般的(并且只使用 gcc 作为例子),我的第一个想法是使用 strace 来找出什么它在做。在我的系统(Ubuntu 11.10 / x64)上,我只运行了 strace ,如下所示:

  strace -F -o sout gcc -Wall -o test test.c 

显示系统对 gcc 进程的调用,同时跟随分支( -F )并将跟踪的输出发送到 SOUT 。这样做,我可以看到 gcc 在这里调用 vfork(),然后 execve( ),尽管实际程序的源代码可能只是做一个简单的 fork() / exec() code>。



code> sout 的相关输出为:
$ (/ usr / lib / gcc / x86_64-linux-gnu / 4.6.1 / cc1,{st_mode = S_IFREG | 0755,st_size = 11248824, ...})= 0
26264 access(/ usr / lib / gcc / x86_64-linux-gnu / 4.6.1 / cc1,X_OK)= 0
26264 vfork()= 26265
26264 wait4(26265,<未完成...>
26265 execve(/ usr / lib / gcc / x86_64-linux-gnu / 4.6.1 / cc1,[/ usr / lib / gcc / x86_64-linux-gnu / 4...,-quiet,-imultilib,。,-imultiarch,x86_64-linux-gnu,test.c, -qua tbase,test.c,-mtune = generic,-march = x86-64,-auxbase,test,-Wall,-fstack-protector ,...],[/ * 46 vars * /])= 0

每行的pid是进程的PID运行。因此,主进程调用 stat()来查找 cc1 ,然后分叉,子进程执行它。



这就是说,我可以在没有引用的情况下回答你的问题; fork / exec是从程序中调用子进程的常用方式。


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.

解决方案

While your question is really more general (and only using gcc as an example), my first idea would be to use strace to figure out what it's doing. On my system (Ubuntu 11.10/x64), I just ran strace, like so:

strace -F -o sout gcc -Wall -o test test.c

This shows system calls for the gcc process, while following forks (-F) and sending the output of the trace to sout. Doing this, I can see that gcc here calls vfork(), and then execve() in the child, though the actual program's source might just do a simple fork()/exec().

The relevant output from sout is:

26264 stat("/usr/lib/gcc/x86_64-linux-gnu/4.6.1/cc1", {st_mode=S_IFREG|0755, st_size=11248824, ...}) = 0
26264 access("/usr/lib/gcc/x86_64-linux-gnu/4.6.1/cc1", X_OK) = 0
26264 vfork()                           = 26265
26264 wait4(26265,  <unfinished ...>
26265 execve("/usr/lib/gcc/x86_64-linux-gnu/4.6.1/cc1", ["/usr/lib/gcc/x86_64-linux-gnu/4."..., "-quiet", "-imultilib", ".", "-imultiarch", "x86_64-linux-gnu", "test.c", "-quiet", "-dumpbase", "test.c", "-mtune=generic", "-march=x86-64", "-auxbase", "test", "-Wall", "-fstack-protector", ...], [/* 46 vars */]) = 0

At the begnning of each line is the pid of the process running. So the primary process calls stat() to find cc1, 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.

这篇关于GCC如何运行其他程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆