进程的程序计数器

Program counter of a process

我以为程序计数器是硬件的一部分。阅读以下内容后,我感到困惑。有人可以澄清区别吗?

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.

没错。有一个程序计数器,它是一个内存地址寄存器。然而,操作系统虚拟地为每个进程创建程序计数器,并控制它们与这些 PC 的流程。这是操作系统的主要功能调度。

如您所说,"program counter"(也称为"instruction pointer")是硬件的一部分;更具体地说,它是一个处理器寄存器。该寄存器的全部目的是指向处理器正在执行的内存中的当前指令。执行该指令后,PC 将更改为指向下一条要执行的指令。

当今大多数现代操作系统都是多任务处理的。这实质上意味着他们可以同时 运行 多个进程。但是,如果您只有一个处理器,就不可能同时执行多个进程,对吧?为了营造一种错觉,即多个进程同时在一个处理器上执行,多任务操作系统在 运行 可用进程之间切换得非常快:它们推进一个进程,暂停它,然后推进其他进程处理等等,所有这一切都在几分之一秒内完成。

要实现此机制,操作系统必须具有适当的结构来保持所有 运行ning 进程的当前状态。操作系统应该在这些结构中保留的最重要的值之一是进程的当前 PC 值,它指示当前在其程序代码中执行的位置。

每个 CPU 都有一个硬件程序计数器。每个线程都有一个程序计数器 VALUE,只有在线程执行时才加载到硬件程序计数中。

如果在多处理系统上执行,一个进程可能有多个硬件程序计数器。每个线程可以 运行 在单独的处理器上,并在该处理器上有一个程序计数器。