Program counter of a process

10,292

Solution 1

As you said, the "program counter" (also called "instruction pointer") is part of the hardware; more specifically, it's a processor register. The whole purpose of this register is to point to the current instruction in memory that is being executed by the processor. Once that instruction is executed, the PC is altered to point to the next instruction to be executed.

Most modern operating systems today are multitasking. This essentially means that they can run multiple processes at the same time. However, if you have just one processor, there's no way you can execute more than a process simultaneously, right? To create this illusion that more than one process is executing at the same time on only one processor, multitasking operating systems switch between runnable processes very fast: They advance one process, they pause it, and then they advance some other process, and so on, all this in fractions of a second.

To implement this mechanism, the operating system must have appropriate structures to keep the current status of all running processes. One of the most important values the operating system should keep in those structures is the current PC value for the process, which indicates where, in its program's code, it is currently executing.

Solution 2

Each CPU has a single hardware program counter. Each thread has a program counter VALUE that is only loaded into the hardware program count when the thread is executing.

A process may have multiple hardware program counters if executing on a multiple processing system. Each thread could be running on separate processor and have a program counter on that processor.

Solution 3

That's true. There is one program counter which is a memory address register. However, operating system creates program counters for each process virtually and controls their flows with these PCs. This is scheduling which is the main feature of OSes.

Share:
10,292
mmswe
Author by

mmswe

Updated on June 22, 2022

Comments

  • mmswe
    mmswe almost 2 years

    I thought program counter is a part of hardware. I'm confused after reading the following. Can someone clarfiy the difference?

    A single-threaded process has one program counter specifying the next instruction to execute. (Threads are covered in Chapter 4.) The execution of such a process must be sequential. The CPU executes one instruction of the process after another, until the process completes. Further, at any time, one instruction at most is executed on behalf of the process. Thus, although two processes may be associated with the same program, they are nevertheless considered two separate execution sequences. A multithreaded process has multiple program counters, each pointing to the next instruction to execute for a given thread.