在 Ksh 和 Perl 中并行执行任务的最佳方式是什么?
我有一个大型 C++ 项目,需要在没有并行 make 的平台上构建(例如 Linux 上的 make -j)。 服务器有 6 个 CPU,我想手动进行并行构建。
我可以为大约 300 个目标文件生成这样的任务列表。 我使用 Makefile 进行依赖性检查和增量构建:
make -f Makefile obj1.o
make -f Makefile obj2.o
make -f Makefile obj3.o ...
我如何使用 Ksh 和 Perl 并行执行这些任务,同时运行的任务不超过 6 个? (Java 或 Python 不可用:-( )
I have this large C++ project that I need to build on a platform that does not have a parallel make (like make -j on Linux). The server has 6 CPU's and I want to do a parallel build manually.
I can generate a task list like this for about 300 object files. I use the Makefile for the dependency checks and incremental build:
make -f Makefile obj1.o
make -f Makefile obj2.o
make -f Makefile obj3.o
...
How would I execute these tasks in parallel with no more then 6 tasks running at a time using Ksh and Perl? (Java or Python are not available :-( )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 Perl 中,您应该查看 Parallel::ForkManager。 你可以这样做:
In Perl the you should look at Parallel::ForkManager. You could do something like this:
分叉的另一种方法是在自己的线程中运行每个 make。
不幸的是,我的手在两位左右挥舞。 首先是让循环静态等待,直到线程完成。 我相信这是使用threads::shared 的 cond_wait() 和信号量变量来完成的。
第二个是从 make 获取返回值,这样您就可以知道是否出现故障并停止构建。 为此,您必须 join() 每个线程才能获取 system() 的结果,即进程的退出代码。
抱歉仓促回复。 希望社区能够填补剩下的部分。
An alternative to forking is to run each make in its own thread.
I am unfortunately hand waving around two bits. First is making the loop wait quiescently until a thread finishes. I believe this is accomplished using threads::shared's cond_wait() and a semaphore variable.
The second is getting the return value from make, so you know if something failed and to halt the build. To do that you'd have to join() each thread to get the result of system(), the process' exit code.
Sorry for the hasty reply. Hopefully the community will fill in the rest.
HPUX 上的 gnu make 没有 -j 标志吗?
http://hpux.connect.org.uk/hppd/hpux /Gnu/make-3.81/
Does gnu make on HPUX not have the -j flag?
http://hpux.connect.org.uk/hppd/hpux/Gnu/make-3.81/
使用 GNU Parallel 您可以编写:
10 秒安装:
了解更多: http://www.gnu .org/software/parallel/parallel_tutorial.html https://www.youtube.com /playlist?list=PL284C9FF2488BC6D1
Using GNU Parallel you can write:
10 second installation:
Learn more: http://www.gnu.org/software/parallel/parallel_tutorial.html https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1
如果操作系统正确处理处理器间通信和调度,那么只要不存在相互依赖关系,您就应该能够将所有进程放入后台进程中。
如果它们都是独立的,我会发出一系列如下命令:
对于依赖项,用双与号 (
&&
) 将它们串起来,以便依赖项不会启动,直到它的《父母》已经完结了。我意识到这并不完全是您要求的解决方案,但这就是我解决问题的方式:)
If the OS is properly handling inter-processor communication and scheduling, you should just be able to throw all the makes into backgrounded processes, as long as there are no interdependencies.
If they're all independent, I would issues a series of commands like this:
With dependencies, string them with the double ampersand (
&&
) so that the dependent item doesn't start until its 'parent' has finished.I realize that's not quite the solution you requested, but it's how I would attack the problem :)