How to control which core a process runs on?

后端 未结 9 652
挽巷
挽巷 2020-12-04 09:52

I can understand how one can write a program that uses multiple processes or threads: fork() a new process and use IPC, or create multiple threads and use those sorts of com

9条回答
  •  有刺的猬
    2020-12-04 10:34

    To find out the number of processors instead of using /proc/cpuinfo just run:

    nproc
    

    To run a process on a group of specific processors:

    taskset --cpu-list 1,2 my_command 
    

    will say that my command can only run on cpu 1 or 2.

    To run a program on 4 processors doing 4 different things use parameterization. The argument to the program tells it to do something different:

    for i in `seq 0 1 3`;
    do 
      taskset --cpu-list $i my_command $i;
    done
    

    A good example of this is dealing with 8 million operation in an array so that 0 to (2mil-1) goes to processor 1, 2mil to (4mil-1) to processor 2 and so on.

    You can look at the load on each process by installing htop using apt-get/yum and running at the command line:

     htop
    

提交回复
热议问题