[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 705
  • Last Modified:

Context switching and multicore machines

How does context switching work in multicore machines? Is each new processes assigned to a idle processor? If so, does context switching only occur if none of the processors are idle? I image that this depends on the operating system.

I ask only because I am curious and no other reason.

Thanks in advance.
0
computer_help
Asked:
computer_help
  • 2
  • 2
2 Solutions
 
gtworekCommented:
This depends on OS...
In Windows context is switched if time quantum ends. Then the thread is re-scheduled on optimal CPU. Determining "optimal" core bases mainly on last core used for thread, preferred core for thread and CPU queue length.
Each CPU has own queue but there are some special system-wide mechanisms re-assigning threads waiting in queues for particular CPU and swapping ques between cores.
After being processed thread is assigned to wait queue. Each priority (0-31) has own queue. CPU takes the highest thread first and if there are high priority threads - lower ones are not processed at all.
There is also "anti-starvation" thread, looking for threads not processed for 3 seconds. If such thread is found, his priority is raised dramatically. During next context switch it will be processed because has high priority and after processing his priority lowers. Maybe it will be processed in normal way, maybe after 3s will be raised again.

Did this help you? :)
0
 
computer_helpAuthor Commented:
Wow! Thank you gtworek for the speedy reply!

This definitely helped me out. Let me see if I understand you correctly.

On a Windows OS:

- A process can consist of one or more threads

- Queuing occurs at the thread level.

- Each CPU has its own queue. When you say each priority has its own queue, you really mean that each CPU has its own priority queue where the next thread to be executed is selected based on its position in the queue and its priority.

- A context switch always occurs at the end of a time quantum

- There are system wide mechanisms for balancing queues between CPUs, and making sure that all threads are selected eventually

So from the point of view of the CPU in a multicore computer, context switching occurs exactly the same as in a machine with a single CPU. The advantage of having a multicore computer is that more than one thread can be executed at once.

Am I correct so far?

I have only one last question. How does the system determine what CPU will be assigned to a newly created thread?
0
 
LMiller7Commented:
A context switch doesn't normally require that a processor or core is idle. The scheduler will usually assign a core that is deemed best under the circumstances. This isn't a permanent assignment. A thread may be assigned any available core or CPU at any given time. This can be influenced by system configuration and other factors as well.

As you may have gathered from the above comment, scheduling and context switching is very complex and the details depend on the OS.

The Microsoft publication "Windows Internals" devotes many pages to this and many of the details are omitted.
0
 
gtworekCommented:
On a Windows OS:
> - A process can consist of one or more threads
YES. Always think about threads not processes if you are digging into scheduling.

> - Queuing occurs at the thread level.
YES. Always. Different thread of same process can have different priorities :)

> - Each CPU has its own queue. When you say each priority has its own queue, you really mean that each CPU has its own priority queue where the next thread to be executed is selected based on its position in the queue and its priority.
YES. Exactly as you wrote, but you shold remember about system-wide kernel mechanisms that can reassign waiting thread from one CPU to another one basing on queue length.

> - A context switch always occurs at the end of a time quantum
NO! If thread finishes his job and enters idle state - context is switched earlier. Thread processing can be interrupted i.e. by hardware interrupt when you move the mouse and system updates cursor position or if network packet arrives etc. Hardware interrupts are usually (until you change this in boot.ini / bcd) processed by all CPUs.

> - There are system wide mechanisms for balancing queues between CPUs, and making sure that all threads are selected eventually
YES. Of course if system is not powerful enough to process all threads there are some trouble... ;)

> So from the point of view of the CPU in a multicore computer, context switching occurs exactly the same as in a machine with a single CPU. The advantage of having a multicore computer is that more than one thread can be executed at once.
YES and NO... multicore scheduling is much more complicated than single core. But switching occurs exactly same way. The only difference is in determining core to run the thread next time.


> Am I correct so far?
I can say yes... :)

> I have only one last question. How does the system determine what CPU will be assigned to a newly created thread?
It bases on the current CPU queue lenght.

And last thing: remember that threads are assigned only to allowed cores (affinity mask determines allowed cores). By default it means "all cores". But managing this mask is really simple even for end users.
0
 
computer_helpAuthor Commented:
Thanks gtworek I really appreciate it.

Also thanks to LMiller7.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now