code_junkie

What's the best way of executing tasks in parallel in Ksh and Perl?

Nov 12th, 2011
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. my @make_obj = qw(
  2. obj1.o
  3. obj2.o
  4. obj3.o
  5. ...
  6. );
  7.  
  8. my $fm = $pm = new Parallel::ForkManager(6);
  9. foreach my $obj (@make_obj) {
  10. $fm->start and next;
  11. system("make -f Makefile $make_obj");
  12. $fm->finish();
  13. }
  14.  
  15. use threads;
  16.  
  17. my $max_threads = 5;
  18.  
  19. my @targets = qw(obj1.o obj2.o obj3.o ...);
  20. while(@targets) {
  21. my $num_threads = threads->list(threads::running);
  22. if( $num_threads < $max_threads ) {
  23. my $target = shift @targets;
  24. threads->create(sub { return system "make $target" });
  25. }
  26. }
  27.  
  28. make -f Makefile obj1.o &
  29. make -f Makefile obj2.o &
  30. ...
  31. make -f Makefile objn.o &
Add Comment
Please, Sign In to add comment