Sai Krishna
Sai Krishna

Reputation: 221

Can I program to chose a free CPU, I have multiple, to run my shell script?

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?

Upvotes: 5

Views: 248

Answers (2)

hek2mgl
hek2mgl

Reputation: 158160

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.

Upvotes: 1

user1907906
user1907906

Reputation:

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.

Upvotes: 2

Related Questions