How are instructions assigned to CPU cores?
In a multi-core processor, how are instructions assigned to CPU cores? That is, on what basis is a program assigned or executed by a particular core and who makes this decision and how it is assigned?
Is is possible for the user or admin to instruct the system to run a specific program on a specific core?
How can this be performed?
How do these cores coordinate?
Even if the OS manages scheduling, how does the OS instruct to run a program on a specific core? How does it look when translated to a code in C or assembly?
Very approximate answer:
In a multi-core processor, how are instructions assigned to CPU cores? That is, on what basis is a program assigned or executed by a particular core and who makes this decision and how it is assigned?
Generally the OS kernel â specifically its scheduler â makes this decision per-process or per-thread, depending on various things like the current utilization of each core, power consumption of each core (now that mixed E/P cores exist), possibly others.
Keep in mind that symmetric multiprocessing existed well before multi-core CPUs â you could get a PC with two CPU sockets even in the Pentium era â and still exists to this day on some server platforms. So you'll find many articles talking about multiprocessing in terms of individual "CPUs" instead of cores.
Is is possible for the user or admin to instruct the system to run a specific program on a specific core?
Depends on the OS; many have a 'CPU affinity' process setting. On Linux you could use taskset (or the more extensive numactl).
For Windows, open its Task Manager (Win2000 or later), go to "Details" and right-click a process to find a "Set affinity" option. The start /affinity command in Cmd or the PowerShell equivalent can be used to start a program with specific affinity pre-set.
how does the OS instruct to run a program on a specific core?
As mentioned before, I think it's better to look at it the other way around: the OS instructs what each CPU core will run.
That is, whenever a CPU returns back from executing a program to executing the OS kernel â either by interrupt in an 'preemptive multitasking' OS or voluntarily in a 'cooperative' OS like Win3.11 â the kernel will first save the CPU's state corresponding to the previous process in RAM (including things like its 'instruction pointer', etc); then it will look for an eligible process; then it will restore the CPU registers for that process and tell the CPU to jump to a specific location in memory, at which point the CPU continues running code belonging to that process.
How does it look when translated to a code in C or assembly?
See for example the relevant bits of Linux kernel:
An exhaustive answer to your questions would easily fill a book, so let's try direct answers.
In a multi-core processor, how are instructions assigned to CPU cores?
Binary code is organized in processes and sometimes threads (depending on OS). The OS's scheduler organizes running those processes/threads on the available CPU cores.
That is, on what basis is a program assigned or executed by a particular core and who makes this decision and how it is assigned?
Largely OS dependent, the scheduler may distribute load based on least loaded core, process/thread priority, core affinity, or something else.
Is is possible for the user or admin to instruct the system to run a specific program on a specific core?
Yes, that's called affinity.
How can this be performed?
Depending on the OS and its API, a process can set its core affinity by itself or a (possibly privileged) user can use tools to set the affinity of a given process.
How do these cores coordinate?
That's at the discretion of the OS and the scheduler.
Even if the OS manages scheduling, how does the OS instruct to run a program on a specific core?
Basically, the scheduler directly or indirectly sets the instruction pointer to the process/thread and lets the core run.
How does it look when translated to a code in C or assembly?
Scheduling works on the machine level. Any code needs to be translated to machine code first.