操作系统作业4

本文讨论了多线程在提高系统性能方面的优势,如在Web服务器、远程过程调用(RPC)系统和字处理软件中的应用。区分了用户级线程和内核级线程的特点,并阐述了在不同场景下选择其中之一的理由。同时,介绍了操作系统进行内核级线程上下文切换的过程。最后,给出了多线程在CPU密集型应用和统计计算中的具体实现案例。
摘要由CSDN通过智能技术生成

第四章作业

4.1

题目:Provide three programming examples in which multithreading provides better performance than a single-threaded solution.

 

回答:

  1. 一个Web服务器接收到对网页、图片、声音等的客户端请求。流量比较高的服务器可能同时有很多客户端访问它。如果Web服务器作为一个传统的单线程进程运行,那么它同时只能够响应一个客户端。客户端等待服务的时间可能会非常。通常比较高效的方法是创建一个包含了多个线程的进程来服务同样的请求。这种方法将使web服务进程多线程化。服务器将创建一个独立的线程来监听客户端请求;当一个请求产生时,它就创建另一个线程来服务这个请求,而不是创建一个进程。
  2. 线程在远程过程调用(RPC)系统中也起着至关重要的作用。RPC通过提供类似于普通函数或过程调用的通信机制来允许进程间通信。通常,RPC服务器是多线程的。当服务器接收到消息时,它使用单独的线程来服务消息。这允许服务器为多个并发请求服务
  3. 字处理软件:如果只有一个进程,当你去编辑,去输入的时候,那么排版的工作就做不了,也不能去存盘。因此,通常会在录入输入一些文档的过程中再派生出其他的线程来完成其他的任务。也就是在一个字处理软件的进程当中派生出其他的任务。一个任务完成的是管理键盘的输入,如果删除某一行,那么文档就需要重新做排版,那么专门有一个任务是用来做排版的。如果为了工作不丢失,会启动定时保存,到点之后这个任务就开始工作,把相应的内容保存在磁盘上。

 

 

4.2

题目:What are two differences between user-level threads and kernel-level threads? Under what circumstances is one type better than the other?

 

回答:

用户线程:

内核线程:

区别:

用户级线程

内核级线程

内核不知道用户级线程的存在

内核已知内核级线程的存在

在没有内核支持的情况下管理用户级线程

内核级线程由操作系统管理

用户级线程由线程库调度

内核级线程由内核调度

用户级线程创建的速度更快

 

 

内核级线程优于用户级线程的情况

如果内核是单线程的,那么内核级线程优于用户级线程,因为执行阻塞系统调用的任何用户级线程都将导致整个进程阻塞,即使其他线程在应用程序中可以运行。

多处理器环境中,内核级线程优于用户级线程,因为内核级线程可以同时在不同的处理器上运行,而进程的用户级线程只在一个处理器上运行,即使多个处理器可用。

 

用户级线程优于内核级线程的情况

如果内核是时间共享的,那么用户级线程比内核级线程更好,因为在时间共享系统中,上下文切换频繁发生。内核级线程之间的上下文切换开销很高,几乎与进程相同,而用户级线程之间的上下文切换与内核级线程相比几乎没有开销。

 

 

4.3

题目:Describe the actions taken by a kernel to context-switch between kernel- level threads.

 

回答:

首先,从正在切换的线程中保存CPU寄存器的值

其次,恢复正在调度的新线程的CPU寄存器。

 

4.5

题目:Assume that an operating system maps user-level threads to the kernel using the many-to-many model and that the mapping is done through LWPs. Furthermore, the system allows developers to create real-time threads for use in real-time systems. Is it necessary to bind a real-time thread to an LWP? Explain.

 

回答:需要——如果一个real-time thread没有绑定到LWP,那么该线程在运行之前必须先连接到LWP。

当LWP绑定到某个线程之后,这个线程优先级将会变得更高。在死锁情况下,如果一个线程有LWP,这个线程将以最小延迟运行来节省时间。

所以,通过将LWP绑定到实时线程来确保线程一旦调度就能够以最小延迟运行。

 

 

4.8

题目: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

 

回答:bc

a. Register values

b. Heap memory

c. Global variables

d. Stack memory

堆内存和全局变量在多线程进程中共享。在多线程过程中,每个线程都有自己的堆栈和寄存器值。

 

 

4.12

题目:Using Amdahl’s Law, calculate the speedup gain of an application that has a 60 percent parallel component for (a) two processing cores and (b) four processing cores.

 

回答:

  1. two processing cores 

1/(0.4 + (1-0.4)/2=1.429

  1. four processing cores.

1/(0.4 + (1-0.4)/4)=1.818

 

 

4.14

题目:A system with two dual-core processors has four processors available for scheduling. A CPU-intensive application is running on this system. All input is performed at program start-up, when a single file must be opened. Similarly, all output is performed just before the program terminates, when the program results must be written to a single file. Between startup and termination, the program is entirely CPU- bound. Your task is to improve the performance of this application by multithreading it. The application runs on a system that uses the one-to-one threading model (each user thread maps to a kernel thread).

• How many threads will you create to perform the input and output? Explain.

• How many threads will you create for the CPU-intensive portion of the application? Explain.

/*一个具有两个双核处理器的系统有四个处理器可用于调度。CPU密集型应用程序正在这个系统上运行。所有输入都是在程序启动时执行的,这时必须打开一个文件。类似地,所有输出都在程序终止之前执行,当程序结果必须写入单个文件时。在启动和终止之间,程序完全是CPU绑定的。您的任务是通过多线程来提高这个应用程序的性能。应用程序运行在使用一对一线程模型(每个用户线程映射到内核线程)的系统上。

你将创建多少线程来执行输入和输出?解释。

你将为应用程序的CPU密集型部分创建多少线程?解释。*/

 

回答:

线程数取决于应用程序的优先级和要求。

因此,对于这种应用程序,只有线程足够了,这个线程将同时处理输入和输出操作——并发,关键点是创建与阻塞系统调用数相同的线程,因为线程将被用来阻塞。

而创建额外的线程也没啥益处。

因此,创建一个线程来执行输入,一个进程来执行输出。

 

创建四个线程来执行应用程序的CPU密集型部分。

因为:线程数应该和处理核数相同。

如果线程数比4小,会浪费处理资源。

而大于4个线程就不能运行。

 

 

4.15

题目:Consider the following code segment: 

a. How many unique processes are created?

b. How many unique threads are created?

 

回答:

a:6个进程:P父进程,pid = fork()创建的子进程P1,if语句中fork()创建的P2,以及P,P1和P2在最后一个fork()中分别创建的P3,P4和P5——共6进程,不算父进程的话,unique processes5

 

b:线程创建是在if中完成的。if块中执行独子进程P1。因此,进程P1将被创建一个线程。进程P2使用fork()创建。因此,进程P2也将创建一个线程。因此,将创建2unique线程。

 

 

4.17

题目:The program shown in Figure 4.16 uses the Pthreads API. What would be the output from the program at LINE C and LINE P?

 

回答:

LINE C值=5

线程中的子进程由父进程fork,子进程每个都有自己的内存空间。

在fork之后,父进程等待子进程的完成。

为子进程创建新线程,调用Runnor()函数,将全局ValLable的值设置为5。

因此,在执行此行之后,所显示的值将为5。

LINE P值=0

在完成子进程之后,父进程中存在的全局变量的值仍然为0。

因此,在执行此行之后,所显示的值将为0。

 

 

4.21

题目:Write a multithreaded program that calculates various statistical values for a list of numbers. This program will be passed a series of numbers on the command line and will then create three separate worker threads. 

One thread will determine the average of the numbers, 

the second will determine the maximum value, 

and the third will determine the minimum value. 

For example, suppose your program is passed the integers

The variables representing the average, minimum, and maximum values will be storedglobally.

 The worker threads will set these values, and the parent thread will output the values once the workers have exited. (We could obviously expand this program by creating additional threads that determine other statistical values, such as median and standard deviation.)

 

回答:

//

//  main.c

//  操作系统作业第四章4.21

//

//  Created by zsy on 2018/10/14.

//  Copyright © 2018zsy. All rights reserved.

//

#include 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值