5

I have a shell script. In that script I am starting 6 new processes. My system has 4 CPUs. If I run the shell script the new processes spawned are automatically allocated to the one of the CPUs by default by the operating system. Now, I want to reduce the total time of running of my script. Is there a way that I can check a processor's free utilization and then chose one to run my process?

I do not want to run a process on a CPU which is >75% utilized. I would wait instead and run on a CPU which is <75% utilized.

I need to program my script in such a way that it should check the 4 CPUs' utilization and then run the process on the chosen CPU.

Can someone please help me with an example?

1
  • 1
    You don't need to do anything. The scheduler will do a much better job than you of selecting a core to run on. If your jobs can be run in parallel, then do so. Which core to run on is a detail that you should not worry about. Commented Jul 30, 2013 at 14:11

2 Answers 2

2

I recommend GNU Parallel:

GNU parallel is a shell tool for executing jobs in parallel using one or more computers. A job can be a single command or a small script that has to be run for each of the lines in the input. The typical input is a list of files, a list of hosts, a list of users, a list of URLs, or a list of tables. A job can also be a command that reads from a pipe. GNU parallel can then split the input and pipe it into commands in parallel.

In addition, use nice.

Sign up to request clarification or add additional context in comments.

Comments

1

You can tell the scheduler that a certain CPU should be used, using the taskset command:

taskset -c 1 process

will tell the scheduler that process should run on CPU1.

However, I think in most cases the built-in Linux scheduler should work well.

1 Comment

Btw, on BSD there is a similar command called cpuset with slight different option names. It would be: cpuset -l 1 process on BSD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.