5

I want to run 3 jobs (A, B, C) on 2 cores of a machine with >2 cores. I know that:

runtime(A)>runtime(C)

runtime(B)>runtime(C)

It is unknown in advance if runtime(A)>runtime(B) or runtime(A)<runtime(B).

What I want to do is:

  • launch A on core 1
  • launch B on core 2
  • after either one is finished, launch C on the one free core

How can this be achieved (in bash, if possible)?

ddrichel
  • 53
  • 3
  • 1
    Welcome to Stack Overflow. This platform is intended for software developers. You forgot to add in your question what you have tried to solve the problem yourself. – Cyrus Jul 26 '20 at 09:09
  • 1
    @Cyrus An attempt is encouraged, but is not required. – TylerH Jul 26 '20 at 21:38

2 Answers2

6

Just tell GNU Parallel it can use 2 cores:

parallel -j 2 ::: jobA jobB jobC

Note that the jobs will run in the order you specified, but the output may come in a different order. If that is an issue, add -k parameter to keep output in order.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Brilliant, this works and it makes sense for gnu parallel to respect the order. I previously dismissed parallel because I was under the impression that the order of the output is the same as the order of the execution. But, I guess this is the case only when jobs on different threads finish at about the same time. – ddrichel Jul 26 '20 at 11:06
3

It's easy in Bash 4.3 (released in 2014) or later:

A &
B &
wait -n # wait for any child to finish
C &
wait # for everyone to finish
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • `sleep 5; echo "A" & sleep 10; echo "B" & wait -n & sleep 5; echo "C" & wait` runs in 20 seconds, I expected 10. And A and B are executed sequentially, aren't they? – ddrichel Jul 26 '20 at 10:50
  • 1
    `sleep 5;` executes that and then waits, your example is therefore buggy. Try writing a script or function called `A` and putting `sleep 5` in it then use the code I gave. – John Zwinck Jul 27 '20 at 08:58
  • 2
    Thank you, you are right. Although I am going with the GNU parallel solution (somewhat easier to extend and maintain), yours is a nifty one and I love it! – ddrichel Jul 27 '20 at 10:45