Search code examples
processoperating-systemprogram-counter

Program counter in processes


I'm having some issue understanding this passage in Tanenbaum Modern Operating system book:

"we see four processes, each with its own flow of control (i.e., its own logical program counter), and each one running independently of the other ones. Of course, there is only one physical program counter, so when each process runs, its logical program counter is loaded into the real program counter". Here is the graph as indicated in the passage enter image description here.

The problem I'm having with is that doesn't each process has its own program counter? And since each program is already its a separate process being executed why does it need to load back into a "real" program counter? I don't understand where this real program counter is coming from?


Solution

  • why does it need to load back into a "real" program counter?

    When Tanenbaum says "real program counter", he is referring to the single program counter associated with a CPU. The program counter is just another CPU register, but it has a specific purpose, namely, pointing to the instruction currently being executed.

    doesn't each process has its own program counter?

    Each process does have its own program counter (PC), but there is only one PC per processor core. So, when a process runs on a CPU, the kernel sets the PC register to that process's saved PC. If the process temporarily stops, then the kernel saves the processes PC from the PC register to some well-defined location in memory.

    Each process has some bookkeeping associated with it, one of which is the process's register state. So, each process that is not currently running has its PC saved in memory.