跳转至

操作系统第三章 作业

4.1

Provide two programming examples in which multithreading does not provide better performance than a single-threaded solution.

1) 任何“线性”执行的程序(后面的程序严重依赖前面执行的结果),这类程序即使使用多线程,每个线程都会阻塞其它线程的执行,因此并不会有更好的性能。

2) 只执行单一任务的程序。多线程对于浏览器,字处理软件等多任务软件是有用的。如浏览器可以在接收网络数据时同时刷新页面。但对于只执行单个任务的程序,便没有必要。

4.2

Describe the actions taken by a thread library to context switch between user-level threads.

用户级线程库的代码都存在于用户空间,因此调用库中的函数会导致一个本地调用而不是系统调用。因为线程主要包括寄存器的值和栈,因此上下文切换会涉到寄存器和栈状态的保存和恢复。

4.3

Under what circumstances does a multithreaded solution using multiple kernel threads provide better performance than a single-threaded solution on a single-processor system?

1)一个应用程序分为许多不同的部分。如网页浏览器有一个线程用于显示图像和文本,另一个用于从网络接收数据。 2)一个应用程序需要执行多个相似的任务。如网页服务器可能要处理上千个客户并发请求网页。

4.4

Which of the following components of program state are shared across threads in a multithreaded process?

  • a. Register values
  • b. Heap memory
  • c. Global variables
  • d. Stack memory

答:b,c

4.8

Consider a multiprocessor system and a multithreaded program written using the many-to-many threading model. Let the number of user-level threads in the program be more than the number of processors in the system. Discuss the performance implications of the following scenarios.

a. The number of kernel threads allocated to the program is less than the number of processors.

b. The number of kernel threads allocated to the program is equal to the number of processors.

c. The number of kernel threads allocated to the program is greater than the number of processors but less than the number of user-level threads.

答:当内核线程小于处理器数目时,因为用户线程只能通过内核线程访问处理器,因此会有一些处理器处于“围观”状态。当内核线程刚好等于处理器时,处理器的效率相对于 a 会提高,然而当一个线程执行了阻塞系统调用时,其它用户线程就无法使用该内核进程,c 的情况则弥补了这一点,当一个用户线程阻塞时可以被其它用户线程替换,因此处理器效率最高。