4

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?

12
  • 2
    For starters: en.wikipedia.org/wiki/Scheduling_(computing)
    – Zac67
    Commented Jan 3 at 12:25
  • 1
    I've editing this question to improve the readability, but I think it will benefit to this question to simply it. At the least, I recommend removing the very last question, as IMO, it's a bit much to answer along with the other related questions. Commented Jan 3 at 12:29
  • @AmazonDiesInDarkness thanks for improving my question. About the last question, I think that is where the essence of my question lies, so I think it would be good to retain it. Commented Jan 3 at 12:40
  • @Zac67 the link you've provided covers what scheduling is but doesn't answer my question. Thanks btw. Commented Jan 3 at 12:44
  • 1
    Given that you've already attracted a couple good answers, my recommendation now is to leave your question as is. If it gets closed, you can always edit it and make it into multiple posts later. Commented Jan 3 at 13:38

2 Answers 2

8

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:

3
  • Thanks for your answer and sharing some resources. I'll come back if i have any doubts. Commented Jan 3 at 14:04
  • I think the answer is a bit vague on "how does the OS instruct to run a program on a specific core?": Actually it's not the context saving in the TCB (Task control Block) that decides where the task will run, but the restoring of the saved context to a CPU (that will continue task execution). So the scheduler selects. On "how does the OS instruct to run a program on a specific core?": The tricky part is to save all CPU state without changing that state before it is saved (and the opposite, restoring the state). The actual selection of the CPU is "just normal code".
    – U. Windl
    Commented Jan 3 at 19:21
  • @U.Windl: Indeed, that needs to be done in assembly language in the kernel's interrupt and system-call entry-points. Often the ISA is designed to make this efficiently possible, e.g. by already pushing the user-space stack pointer onto the kernel stack, and setting the stack pointer register to point there, all before any kernel instructions run, or register bank-switching and a way to access the stashed user-space regs, or various other things like x86-64's swapgs to stash a piece of user-space state and get a pointer, in a syscall entry point where RSP is still the user-space value. Commented Jan 3 at 22:06
5

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.

2
  • Thanks for your answer. How does the os keep track of which core is utilized Commented Jan 3 at 14:06
  • 1
    @vimalathithan17 The same way you manage any data structure: The OS assigns tasks to cores, so it should know what it did, meaning: The cores don't select a task and the OS has to find out which, but the opposite: The OS assigns a task to a core.
    – U. Windl
    Commented Jan 3 at 19:24

Not the answer you're looking for? Browse other questions tagged .