Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!)

like image 835
Dan Barzilay Avatar asked Oct 24 '25 14:10

Dan Barzilay


2 Answers

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.

like image 179
Mephy Avatar answered Oct 27 '25 06:10

Mephy


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.

like image 20
user3344003 Avatar answered Oct 27 '25 05:10

user3344003



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!