如何制作"-j"?选项实际起作用吗? [英] How does the make "-j" option actually work?

查看:199
本文介绍了如何制作"-j"?选项实际起作用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从手册页:

-j [职位],-职位[=职位] 指定要同时运行的作业(命令)数. 如果有多个-j选项, 最后一个有效.如果-j 选项是 不带参数的情况下,make不会限制作业的数量 可以同时运行.

-j [jobs], --jobs[=jobs] Specifies the number of jobs (commands) to run simultaneously. If there is more than one -j option, the last one is effective. If the -j option is given without an argument, make will not limit the number of jobs that can run simultaneously.

我知道它使用图依赖来知道哪些规则是独立的.

I know it uses a graph dependency to know which rules are independent.

我想知道此图的构建方式,并了解使用的标准是什么.

I would like to know how this graph is built and understand what are the criteria used.

谢谢.

推荐答案

正如人们所期望的那样,依赖关系图基于为每个Makefile目标列出的先决条件. make将构建一个图形,其中目标和先决条件为顶点,并且从先决条件到其目标都有一个有向边.通过这种方式,输入边的数量可以告诉您目标具有多少个先决条件.如果没有入局边缘,那么就没有先决条件.

The dependency graph is based, as one would expect, on the prerequisites listed for each Makefile target. make will build a graph where the targets and prerequisites are vertices and there is a directed edge from the prereqs to their targets. In this way the number of incoming edges tells you how many prereqs a target has. If it has no incoming edges, then it has no prerequisites.

例如,.c.h文件的顶点将没有输入边.这些文件是您的源文件,不需要构建.

The vertices for the .c and .h files, for example, will have no incoming edges. Those files are your source files and do not need to be built.

然后在图形上执行拓扑排序以确定执行顺序.来自维基百科:

It then performs a topological sort on the graph to determine the order of execution. From Wikipedia:

拓扑排序(拓扑顺序)的规范应用是安排一系列作业或任务;拓扑排序算法最早是在1960年代初期在项目管理中使用PERT调度技术进行研究的(Jarnagin 1960).作业用顶点表示,如果必须在开始作业y之前完成作业x,则x到y之间存在一条边(例如,在洗衣服时,必须先烘干洗衣机,然后再将衣服晾干).然后,拓扑排序给出执行作业的顺序.

The canonical application of topological sorting (topological order) is in scheduling a sequence of jobs or tasks; topological sorting algorithms were first studied in the early 1960s in the context of the PERT technique for scheduling in project management (Jarnagin 1960). The jobs are represented by vertices, and there is an edge from x to y if job x must be completed before job y can be started (for example, when washing clothes, the washing machine must finish before we put the clothes to dry). Then, a topological sort gives an order in which to perform the jobs.

拓扑排序的要点是找到没有传入边(没有依存关系)的顶点,并将其放在第一位.然后从图中删除它们.现在,您将获得一组新的顶点,这些顶点没有传入的边(没有依存关系).那些是下一个.依此类推,直到完成. (如果您曾经到达没有此类顶点的点,则依赖图将包含一个循环,这是一种错误情况.)

The gist of a topological sort is to find the vertices with no incoming edges (no dependencies) and put those first. Then remove them from the graph. Now you'll have a new set of vertices with no incoming edges (no dependencies). Those are next. And so on until finished. (If you ever reach a point when there are no such vertices then the dependency graph contains a cycle, which is an error condition.)

在典型的Makefile中,这意味着您将首先构建源文件(无需执行任何操作).然后是依赖于那些源文件的目标文件.然后从这些目标文件构建库和可执行文件.

In a typical Makefile this means you'll first build the source files (nothing needs to be done). Then the object files that depend on those source files. Then the libraries and executables built from those object files.

在正常的非并行操作下,make将在每次迭代中简单地选择一个目标并进行构建.并行时,它将捕获尽可能多的无依赖目标,并以允许的并发作业数为上限并行构建它们.

Under normal non-parallel operation make will simply pick a single target each iteration and build it. When it is parallel it will grab as many dependency-less targets as it can and build them in parallel, up to the number of permitted simultaneous jobs.

因此,当make到达目标文件步骤时,它将在图形中具有大量顶点,并且这些顶点都没有传入边.它知道它可以并行构建目标文件,因此它分叉 n的 n 个副本来构建目标文件.

So when make gets to, say, the object file step, it will have a large number of vertices in the graph that all have no incoming edges. It knows it can build the object files in parallel and so it forks off n copies of gcc to build the object files.

这篇关于如何制作"-j"?选项实际起作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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