0

在进程执行期间,调用 fork() ids 时,会创建一个具有单独内存空间但内存内容相同的新进程。因此,由于这些是不同的进程,它们将具有不同的进程描述块,因此它们将根据调度算法给出的机会执行(PCB 将保存它们的程序计数器值)。

但是当一个进程典当另一个线程时,该线程共享它的地址空间。我的问题是关于线程的执行:- 线程是否给定了具有不同程序计数器值的单独 PCB,并且调度算法调度了接下来将执行哪个线程。如果是那么线程如何在分配给它的函数完成执行后立即停止执行。这是因为该函数是子线程堆栈上唯一的函数,并且当它返回时没有其他函数可以使用?

4

1 回答 1

1

Typically, the scheduler/dispatcher handles threads. Threads are the system objects that have execution and the Thread Control Blocks, (or whatever it's called on whatever OS), will have their own stack, register save, (especially stack pointer, ie where the PC is pushed upon interrupt), thread priority, other thread-specific data and a PCB pointer to the process it belongs to. The PCB has memory management data, access-control data, permissions etc, ie. data specific to a process. Processes do not have any execution except insofar as every process must own at least one thread, (usually, but not exclusively, the one raised by the loader when the process was created).

If the thread code exits by returning from the top-level function that was used in its creation, (by no means the most common means for a thread to be terminated), it will pop off a return address that was placed on its stack at creation time and so make a 'TerminateThread', (or whatever), system call, resulting it its own suicide.

Obviously, a very broad overview of a 'typical' OS. Details are OS dependent, (and, indeed, vary with releases).

于 2012-08-25T18:43:24.510 回答