0
votes

I am trying to run several shell commands in a parallel way without making background processes using "&". Also, I want to assign one job to one CPU (in a fair way) For example, if I have four cores, I want assign four cmd1 to cmd4 as follows:

CPU #1: cmd1

CPU #2: cmd2

CPU #3: cmd3

CPU #4: cmd4

Could you please let me know ways doing that?

I've found "parallel" command, but I could not figure out how to use it. Also, I've tried the following command: ./cmd1 | ./cmd2 | ./cmd3 | ./cmd4 It seems like that four commands (cmd1 to cmd4) are running in parallel, but I am not sure the jobs are assigned to cores as I said in the above.

Thank you!

Sorry. I am running the commands on linux.

1
Why do you care to which core a command is assigned?Johnsyweb
Sorry to confuse you. I just want to assign one job to one core. (Not necessary to assign a job to specific core)freddy

1 Answers

3
votes

First, if you want processes to be executed in parallel, they have to be background jobs. What do have you against using &?

Second, you can use taskset to bind a process to a CPU core, or a set of cores. For example:

taskset -c 0 cmd1 &
taskset -c 1 cmd2 &
taskset -c 2 cmd3 &
taskset -c 3 cmd4 &

This might not be a good idea though; if one process is idle for long periods of time the other 3 cannot use the core it's assigned to.