GCC 如何运行其他程序?

发布于 2024-12-27 04:12:57 字数 319 浏览 2 评论 0原文

也许标题并没有那么准确地表达问题:我知道当我运行 gcc foo.c 时,GCC 会调用其他为其完成所有工作的子程序,使 gcc 主程序只是一个接口。但这到底是如何完成的呢?

它使用 systemexec 或其他函数吗?我之所以想知道这个是因为我想构建一个基于类似系统的网络爬虫,其中会有一个界面程序和其他几个子程序,例如crawldownload.

如果这个问题已经被问过,但我没有使用搜索或“具有类似标题的问题”找到它,我很抱歉。

先感谢您。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见 2025-01-03 04:12:57

虽然您的问题确实更笼统(并且仅使用 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()即可。

sout 的相关输出是:

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

每行的开头是正在运行的进程的 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文