放弃 CPU 控制权的线程 - 教科书中似乎自相矛盾
Threads giving up CPU control - Seeming contradiction in textbook
我正在学习操作系统课程中的线程和进程,我在我的教科书(现代操作系统,第 4 版,Tanenbaum 和 Bos)中发现了一个明显的矛盾。我敢肯定我在这里误解了什么,如果有人能澄清一下就太好了。
第 106 页:
Another common thread call is thread_yield, which allows a thread to voluntarily give up the CPU to let another thread run. Such a call is important because there is no clock interrupt to actually enforce multiprogramming as there is with processes
好的 - 所以我的解释是线程永远不会放弃控制权,除非它们自愿放弃控制权。有道理。
然后在第 116 页,在线程错误处理共享信息的示例中:
As an example, consider the errno variable maintained by UNIX. When a process (or a thread) makes a system call that fails, the error code is put into errno. In Fig. 2-19, thread 1 executes the system call access to find out if it has permission to access a certain file. The operating system returns the answer in the global variable errno. After control has returned to thread 1, but before it has a chance to read errno, the scheduler decides that thread 1 has had enough CPU time for the moment and decides to switch to thread 2.
但是线程 1 不是不由自主地从 CPU 中拉出来的吗?我认为没有办法像进程切换那样强制执行线程切换?
如果我们要处理进程级线程而不是 OS 级线程,这就有意义了。 CPU可以中断一个进程(不管运行ning是什么线程),但是因为OS不知道进程级线程,它不能打断他们。如果进程中的一个线程想要允许另一个线程运行,它必须专门屈服于另一个线程。
然而,如今大多数语言都使用 OS 级线程,OS 知道并可以抢占。
令人困惑的是线程有两种不同的实现方式。在过去,根本没有线程支持。 DoD 对 Ada 编程语言的授权(其中任务(也称为线程)是不可或缺的一部分)强制采用线程。
运行 创建库的时间(主要是为了支持 Ada)。这在一个过程中起作用。该进程维护一个计时器,该计时器会中断线程,并且库会在线程之间切换,就像操作系统切换进程一样。
请注意,此系统一次只允许一个进程的一个线程执行,即使在多处理器系统上也是如此。
您的第一个示例描述的是这样一个库,但它描述的是一个非常原始的线程库,其中线程调度基于进程的各个线程之间的协作。
后来操作系统开始发展对线程的支持。操作系统不是调度进程,而是调度线程来执行。一个进程就是一个地址 space 和一个线程的集合。你的第二个例子是在谈论这种线程。
我正在学习操作系统课程中的线程和进程,我在我的教科书(现代操作系统,第 4 版,Tanenbaum 和 Bos)中发现了一个明显的矛盾。我敢肯定我在这里误解了什么,如果有人能澄清一下就太好了。
第 106 页:
Another common thread call is thread_yield, which allows a thread to voluntarily give up the CPU to let another thread run. Such a call is important because there is no clock interrupt to actually enforce multiprogramming as there is with processes
好的 - 所以我的解释是线程永远不会放弃控制权,除非它们自愿放弃控制权。有道理。
然后在第 116 页,在线程错误处理共享信息的示例中:
As an example, consider the errno variable maintained by UNIX. When a process (or a thread) makes a system call that fails, the error code is put into errno. In Fig. 2-19, thread 1 executes the system call access to find out if it has permission to access a certain file. The operating system returns the answer in the global variable errno. After control has returned to thread 1, but before it has a chance to read errno, the scheduler decides that thread 1 has had enough CPU time for the moment and decides to switch to thread 2.
但是线程 1 不是不由自主地从 CPU 中拉出来的吗?我认为没有办法像进程切换那样强制执行线程切换?
如果我们要处理进程级线程而不是 OS 级线程,这就有意义了。 CPU可以中断一个进程(不管运行ning是什么线程),但是因为OS不知道进程级线程,它不能打断他们。如果进程中的一个线程想要允许另一个线程运行,它必须专门屈服于另一个线程。
然而,如今大多数语言都使用 OS 级线程,OS 知道并可以抢占。
令人困惑的是线程有两种不同的实现方式。在过去,根本没有线程支持。 DoD 对 Ada 编程语言的授权(其中任务(也称为线程)是不可或缺的一部分)强制采用线程。
运行 创建库的时间(主要是为了支持 Ada)。这在一个过程中起作用。该进程维护一个计时器,该计时器会中断线程,并且库会在线程之间切换,就像操作系统切换进程一样。
请注意,此系统一次只允许一个进程的一个线程执行,即使在多处理器系统上也是如此。
您的第一个示例描述的是这样一个库,但它描述的是一个非常原始的线程库,其中线程调度基于进程的各个线程之间的协作。
后来操作系统开始发展对线程的支持。操作系统不是调度进程,而是调度线程来执行。一个进程就是一个地址 space 和一个线程的集合。你的第二个例子是在谈论这种线程。