简体   繁体   中英

What exactly does that mean to allocate the CPU to another process in a Context Switch?

I'm studying both Computer Organization and Operating System. One confusing me a lot is What exactly does that mean to allocate a CPU to a process in a Context Switch?

The term ' allocate CPU ' is not according to what I imagine about a Context Switch.

So I want to make sure If I'm right, according to my view:

Stored-program architecture stores instructions into the main memory, those instructions are as in the text section of a Process.

From computer organization's perspective, only Program Counter decides which instruction to execute.

If the kernel decides to switch the allocation from Process 1 to Process 2, namely context switch, it does:

  1. Store Process 1 's data into PCB 1
  2. Load Process 2 's data, namely PCB 2 into the kernel
  3. Update all register values from PCB 2 ( Includes PC )
  4. So then PC's address is updated to another text section. The diagram below shows my thought.

上下文切换

So, can I say that what kernel actually does to execute another Process, is it does a context switch and basically just changes the Program Counter's address to another process's text section address?

"Changing the program counter" is the low-level hardware view of what's going on.

"Allocating the CPU" is the operating-system view of whatever it needs to do to keep track of system resources, which of course involves loading the program counter for the code now to be executed.

A process is defined by its register values. Some of those will be general registers that any application can change and other will be system registers that can only be changed in kernel mode.

Most processors define a structure that is often called a Process Context Block. The PBC defines where each register defining the process state is stored. Some processor require multiple PCB-like structures as the my save floating point and other types of registers separately.

Most processors have Load Process Context and Save Process Context instructions (or their equivalent. There is usually just a single operand to these instructions: the address of the PCB.

A process change then looks like:

 SAVEPCTX    address_of_pcb_for_current_process
 LOADPCT     address_of_pcb_for_next_process ; after this, in a new process.
 REI ; Return to user mode.

On some poorly design processors, these steps are more complicated.

The term 'allocate CPU' is not according to what I imagine about a Context Switch.

It all depends upon how you think about it. The processor always needs to be running something (unless you have some processor designed for mobile that can easily be turned off). So is a context switch allocating a processor to a process or is it a process being assigned to a CPU?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM