views:

107

answers:

6

So I've been getting into a bit of assembly lately and I'm a beginner so i was wondering if someone could clarify something. I take it every process has it's own set of registers, and each thread can modify these registers right?. How then do multiple threads use the same registers without causing clashes? Or does each thread have its own set of registers?

+7  A: 

A thread context switch involves saving the registers of the current execution context, and loading the registers with saved values from execution context begin switched to. (among other things). So each thread effectively has its own set of registers. Also its own stack, since ESP is one of the registers.

One way of thinking about this is that you get threads by saving off the current register state, and loading the registers with a new state. If that isn't happening, then Its not a thread switch. If you are also switching to a different set of virtual address tables, then what you have is a process switch rather than a thread switch.

you say:

I take it every process has it's own set of registers, and each thread can modify these registers right?

But this isn't quite right. Each CPU core has a single set of registers. These registers are changed whenever the OS switches to a different thread. But there is only one thread executing in a CPU core at any one time. Processes don't really have their own registers, processes own threads (or at least one thread), and threads have registers, or rather a place to keep the values for the registers while the thread is waiting for a CPU core to be available to run on.

John Knoeller
nice info thanks! so pretty much before a context switch the current thread would pushfd and pushad? Then after pop it back and away it goes?
Dnaiel
@Dnaiel: I don't know if they actually use those instructions, but that's the basic idea, yes.
John Knoeller
A: 

Each thread has its own context, which includes the set of registers, CPU flags, stack, etc.

code ex machina
+1  A: 

Depending on the processor, there is only one set of registers. Not one set per thread.

There are ways to save the state of all registers, so that a thread can take up where it left off.

Some processors facilitate this.

Peter K.
A: 

The thread is done by a kernel or an OS, so the program should not care about it. If no kernel or OS is available, then you need to implement it yourself. for that you will need:

  • a function which will save the sate of all register in your CPU (SP:Stack pointer, internal register value, PC:Program counter etc...) in an other memory space to switch to a new thread.
  • a function to load a thread environment to your CPU environment, restore the previously saved internal register value to your CPU register.
Phong
A: 

In hardware, there is only one set of registers for each processor core. Because of this, only one thread at a time may use the registers. Multiple threads are run at the same time on a single core by rapidly switching from one thread to another. Scheduling which thread runs when is the job the operating system.

When switching from one thread to another, the contents of the registers are saved to a special area of memory, and the registers for the next thread are copied back into the processor. This includes the instruction pointer, so the thread knows where to continue executing when it gets control back. This process is called context switching.

Since the operating system's scheduler is in yet another thread, it can only schedule processes when it is running. This means that a special hardware feature--an interrupt--is necessary to control context switches. Only the operating system can schedule context switch interrupts.

Jeffrey L Whitledge
+1  A: 

you have

  • a set of processes that is the one of your operating system,
  • every process has a memory space that contains dynamic allocated memory, static data and code assembly,
  • every process has a list of threads
  • every thread has its own set of registers, program counter and stack

with context switch your scheduler swaps thread data to pass the execution to another one.

Usually a process is heavier than a thread and various scheduling approaches exist:

  • doing context switches just internally (green threads) to your program (your OS will just consider it a single process so: hard multi-core)
  • you can assign a number of real processes to have an hybrid approach allowing easy multi-core optimization.
Jack