How to control which core a process runs on?

后端 未结 9 654
挽巷
挽巷 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:28

    Normally the decision about which core an app will run on is made by the system. However, you can set the "affinity" for an application to a specific core to tell the OS to only run the app on that core. Normally this isn't a good idea, but there are some rare cases where it might make sense.

    To do this in windows, use task manager, right click on the process, and choose "Set Affinity". You can do it programmatically in Windows using functions like SetThreadAffinityMask, SetProcessAffinityMask or SetThreadIdealProcessor.

    ETA:

    If you are interested in how the OS actually does the scheduling, you might want to check out these links:

    Wikipedia article on context switching

    Wikipedia article on scheduling

    Scheduling in the linux kernel

    With most modern OS's, the OS schedules a thread to execute on a core for a short slice of time. When the time slice expires, or the thread does an IO operation that causes it to voluntarily yield the core, the OS will schedule another thread to run on the core (if there are any threads ready to run). Exactly which thread is scheduled depends on the OS's scheduling algorithm.

    The implementation details of exactly how the context switch occurs are CPU & OS dependent. It generally will involve a switch to kernel mode, the OS saving the state of the previous thread, loading the state of the new thread, then switching back to user mode and resuming the newly loaded thread. The context switching article I linked to above has a bit more detail about this.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-04 10:36

    As others have mentioned, it's controlled by the operating system. Depending on the OS, it may or may not provide you with system calls that allow you to affect what core a given process executes on. However, you should usually just let the OS do the default behavior. If you have a 4-core system with 37 processes running, and 34 of those processes are sleeping, it's going to schedule the remaining 3 active processes onto separate cores.

    You'll likely only see a speed boost on playing with core affinities in very specialized multithreaded applications. For example, suppose you have a system with 2 dual-core processors. Suppose you have an application with 3 threads, and two of threads operate heavily on the same set of data, whereas the third thread uses a different set of data. In this case, you would benefit the most by having the two threads which interact on the same processor and the third thread on the other processor, since then they can share a cache. The OS has no idea what memory each thread needs to access, so it may not allocate threads to cores appropriately.

    If you're interested in how the operating system, read up on scheduling. The nitty gritty details of multiprocessing on x86 can be found in the Intel 64 and IA-32 Architectures Software Developer's Manuals. Volume 3A, Chapters 7 and 8 contain relevant information, but bear in mind these manuals are extremely technical.

    0 讨论(0)
提交回复
热议问题