Dan Barzilay
Dan Barzilay

Reputation: 4983

Running multiple processes on a single CPU

I wondered how can a single CPU, which I presume has one cpu stack and one registry set (there's only one instance for each register), run multiple processes concurrently?

Does it change the stack and the registers every time it changes the process that it's currently running? For example process X has the value 0x03 in EAX but process Y has the value 0x02 on that register. So how does the CPU handles switching the values of the EAX register when it switches from executing instructions for the X process to executing instructions for the Y process? (Because each process works with the EAX it expected - the one that it stored in there before)

I couldn't find information about this, though I'll accept answers linking to sources with related information.

Sorry if the question is unclear, I tried to clarify it as much as possible, so please ask if anything is still unclear.

Note: I don't mean threads, because as far as I think I understand, those use the same registers and the compiler builds the correct code so that they'll all work well together. (correct me if I'm wrong please!)

Upvotes: 4

Views: 1135

Answers (2)

user3344003
user3344003

Reputation: 21607

A processor defines a context block structure. The name and format are system dependent. Processors generally have a single instruction to save the current process context and load a process context.

The operating system maintains the process context blocks and instructs the processor which to load and save.

The process context contains the values of all the general registers, the indicator flag registers, and the address space definition registers. The amount of data is rather large so loading and saving a process context tends to be relatively lengthy process.

Upvotes: 1

Mephy
Mephy

Reputation: 2986

Concurrency in a single processor is only interrupting a proccess for some time, and letting another execute. Of course, each process have a different registers, including the one point to the stack position its current using - this information is called a context.

Whenever a proccess is entering the suspend mode, its context is saved in memory/stack. The processor then recovers (or creates) the context for the new proccess. When the first proccess is going to execute again, its context is recovered.

This context switch is done by either software (operational system) or hardware.

Upvotes: 6

Related Questions