Sunday, November 25, 2007

Process and Processor

Scott Hanselman: Help me understand when we say multi-core machines, if I have a single process that's running on a machine with four processors, is there agility? If I have multiple threads, are those threads jumping between? I don't quite understand how the hardware works. I always used to think it was one process gets pinned to a processor.
Stephen Toub: Only if you want it to. You'd actually have to express that either as the user in, for example, Task Manager or as a developing setting thread affinity and process affinity for particular processors, but by default, no. There is no affinity. If you have a process that uses only one thread, by default, that process will ever only be running on one processor at a time, but it can certainly move around from processor to processor. Ideally, it won't for a lot
of subtle reasons like making sure that caches aren't invalidated and the memory that the -- well, that
caches aren't invalidated is good enough, but if the process has in it multiple threads and let's say in your case it has four threads, each of those four threads can be running one on each processor at the same time.
Scott Hanselman: Who decides that?
Stephen Toub: For the most part, it's t h e operating system unless as a developer you go ahead and say, "This thread here, I really want to tie to processor 3," in which case you can set an affinity mask that says, "Never let the operating system only allow this thread to run on processor 3 and if processor 4 is available and there's absolutely nothing happening on it, sorry, you can't run this thread until processor 3 is available."
Taken from Parallel Programming with .net Hanselminutes

No comments: