跳转至

2019

非对齐指令总结

1 大端与小端

  • 大端:数据的高位位于内存的低地址,数据的地位位于内存的高地址。
  • 小端:与大端相反。

事实上大端更符合人的思维,因为在写一个数字时人们更习惯从高位写到低位,且高位是写在左边的。而这可以直接和内存的“图像”对应,而小端则需要把每个字的字节地址颠倒一下才可以。 例如,一个 32 位的整数 0xffeeddcc,在内存里的存储情况如下。(非对齐,起始地址为 0x01)

大端: 0 1 2 3 0x00 00ffeedd 0x01 cc556677 小端: 3 2 1 0 0x00 eeddcc00 0x01 776655ff ps.假设除去该整数存储的空间,字节地址与存储的十六进制数对应,即地址为 0x01 存 11,0x05 存 55,这点用来突出字节地址是多少。

2 非对齐指令

instruction lwl/swl, lwr/swr usage lwl t0, offset(s0)

其中的 left,与 shift left 的“左”都表示数据的高位。right 则表示数据的低位。

lwl 表示将内存对应地址所在的字(align_address,即抹掉地址低两位对应地址)的数据低位复制到寄存器数据的高位。寄存器其它部分不变。

swl 则表示将寄存器的数据高位复制到内存对应数据低位。其它部分不变。 即 lwl/swl 寄存器数据高位↹内存数据低位 lwr/swr 寄存器数据低位↹内存数据高位

1 从#1 中读取该整数实例

大端 lwl s0, 0(1) lwr s0, 3(1) 小端 lwr s0, 0(1) lwl s0, 3(1)

将整数以图中所示存储到内存中只需将 lw*改为 sw*。

可以以此定义非对齐加载、存储伪指令, 如 uld(rd, rb) 表示非对齐 load,加载内存[rb+3:rb]的数据, uld(rd, rb)=> lwl rd, 0(rb) lwr rd, 3(rb) (可能 uld 是加载 8 字节,待确认)

2 关于 load/store 多少字节

n 为地址低 2 位,n=addr[1:0] 大端 lwl/swl 4-n lwr/swr n+1 小端 lwr/swr 4-n lwl/swl n+1

操作系统第 7 章 作业 死锁


1、

Consider the traffic deadlock depicted in follow figure.

deadlock car

a. Show that the four necessary conditions for deadlock indeed hold in this example. b. State a simple rule for avoiding deadlocks in this system.

  • a. 1. mutual exclusion: 每条道路都只能让一个方向的车通行,且车之间不可能穿透而过。 2. hold and wait: 四个方向的车辆都占有一条道路,并等待另一方向的车露出空隙。 3. no preemption: 每个方向的车都必须等待另一个方向的车主动倒车,无法强迫。 4. circular wait:每个方向的车互相等待,形成一个环。
  • b. 安排一位交警选择并指挥一个方向的车让出道路。

2、

Consider a system consisting of four resources of the same type that are shared by three processes, each of which needs at most two resources. Show that the system is deadlock free.

答:反证法,假设形成了死锁。则由每个进程最多需求 2 个资源实例,和死锁必要条件中的 3,4 点可知,唯一的情形是:三个进程各自占有一个资源且等待另一个资源,并形成了一个环。而在该情况下,剩余的一个资源导致死锁不会发生。

3、

Consider a system consisting of m resources of the same type being shared by n processes. Resources can be requested and released by processes only one at a time. Show that the system is deadlock free if the following two conditions hold:

  • a. The maximum need of each process is between 1 and m resources.
  • b. The sum of all maximum needs is less than m + n.

$ a. \sum_{i=1}^n Max_i < m+n $ $ b. Max_i \geq 1 for all i $ $ c. Need_i = Max_i − Allocation_i $

If there exists a deadlock state then: $ d. \sum_{i=1}^n Allocation_i = m $

$ a,b,c,d \Rightarrow \sum_{i=1}^n Need_i < n $

This implies that there exists a process $ P_i $ such that $ Need_i = 0 $. Since $ Max_i >= 1 $, it follows that $ P_i $ has at least one resource, the deadlock can't maintain.

4、

Consider the following snapshot of a system:

PID AL. MAX
A B C D A B C D
P0 0 0 1 2 0 0 1 2
P1 1 0 0 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6
AV.
A B C D
1 5 2 0

(p.s. AL.= Allocated:, AV.= Available)

Answer the following questions using the banker's algorithm: a. What is the content of the matrix Need? b. Is the system in a safe state? c. If a request from process $ P_1 $ arrives for (0,4,2,0), can the request be granted immediately?

  • a | PID | need | | | | | --- | --- | - | - | - | | | A | B | C | D | | P0 | 0 | 0 | 0 | 0 | | P1 | 0 | 7 | 5 | 0 | | P2 | 1 | 0 | 0 | 2 | | P3 | 0 | 0 | 2 | 0 | | P4 | 0 | 6 | 4 | 2 |
  • b 由银行家算法可知是安全的,序列\(<0,2,1,3,4>\)为安全序列。
  • c 假设满足,则$ Need_{P_1} =(0,11,7,0) \(,序列\) <0,2,3,4,1> $为安全序列,故可立即满足。

5、

Consider a system with four processes$ P_1, P_2, P_3$ and $ P_4 $, and two resources, $ R_1, R_2 $ respectively. Each resource has two instances. Furthermore:

$ P_1 $ allocates an instance of $ R_2 $ , and requests an instance of $ R_1 $ ; $ P_2 $ allocates an instance of $ R_1 $ , and doesn’t need any other resource; $ P_3 $ allocates an instance of $ R_1 $ and requires an instance of $ R_2 $ ; $ P_4 $ allocates an instance of $ R_2 $ , and doesn’t need any other resource. Answer the following questions:

a. Draw the resource allocation graph. b. Is there a cycle in the graph? If yes name it. c. Is the system in deadlock? If yes, explain why. If not, give a possible sequence of executions after which every process completes.

  • a. 略
  • b. 存在
  • c. 不是,$ $

操作系统第六章作业 进程同步

1. What is the meaning of the term busy waiting?

当一个进程位于临界区时,任何其它试图进入临界区的进程都必须在代码中不断地循环。

2. What is the meaning of the term spinlocks?

自旋锁是一种会造成忙等的信号量,在进程等待锁时还需不断运行,占用 cpu 资源。

3. Explain the deadlock and give an example

若干个进程竞争系统资源,最后每个进程都必须得在其它进程释放资源时才能改变状态,因而无法改变状态,造成死锁。 如以下程序在 A,B 同时运行时造成死锁。 A:

wait(S)
wait(Q)
...
signal(S)
signal(Q)

B:

wait(Q)
wait(S)
...
signal(S)
signal(Q)

4

Servers can be designed to limit the number of open connections. For example, a server may wish to have only N socket connections at any point in time. As soon as N connections are made, the server will not accept another incoming connection until an existing connection is released. Explain how semaphores can be used by a server to limit the number of concurrent connections. (6.8)

初始化计数型信号量 connection=N

wait(connection)
//do something
signal(connection)

5. What operations can be performed on a semaphore? List them and explain the means of semaphore values

wait() 和 signal() 操作。 信号量可分为计数信号量与二进制信号量。计数信号量值域不受限制,代表系统受限资源的数量。 二进制信号量的值只能为 0 或 1,可表示对临界区的访问权(只能有一个进程位于临界区)。

6. How does the signal() operation associated with monitors differ from the corresponding operation defined for semaphores? (6.16)

当没有进程等待时,信号量的 signal() 会对信号量的值加一,而管程里的 signal() 则不会进行任何操作。

7

A uniprocessor system concurrently executes 2 processes ($ P_A, P_B $). Two Semaphores $ Mutex_R_a $ and $ Mutex_R_b $ are added in mutual accessing the critical section and synchronizing between $ P_A $ and $ P_B $ . Please read following program segment carefully and answer the questions:

(1) What are initial values for $ Mutex_R_a $ and $ Mutex_R_b $ . (2) Is it possible to cause deadlock? Please state your reason. Semaphore $ Mutex_R_a $, $ Mutex_R_b $ ;

Void P_A ( ){
    while(true){
        Wait(Mutex_Ra );
        ......
        Wait(Mutex_Rb );
        ......
        Signal(Mutex_Ra );
        ......
        Signal(Mutex_Rb );
    }
}
Void P_B ( ){
    while(true){
        Wait(Mutex_Rb );
        ......
        Wait(Mutex_Ra );
        ......
        Signal(Mutex_Rb );
        ......
        Signal(Mutex_Ra );
    }
}

答: (1) $ Mutex_R_a = Mutex_R_b = 1$ (2) 当 P_A 和 P_B 同时到达时,两个进程都会执行第一句,接着便会由于$ Mutex_R_a = Mutex_R_b = 0$而陷入死锁。

8

设有一个可以装 A、B 两种物品的仓库,其容量有限 (为 N),但要求仓库中 A、B 两 种物品的数量满足下述不等式: $$ -M≤A 物品数量-B 物品数量≤N $$ 其中 M 和 N 为正整数。另外,还有一个进程消费 A 和 B,一次取一个 A 与 B 组装成 C。 试用信号量和 P、V 操作描述 A、B 两种物品的入库和出库 (组装成 C) 过程。

答: 设 nA, nB 分别为 A 物品,B 物品数量,二进制信号量 mutex,初始值为 1。

A 入库进程:

wait(mutex)
nA++
if(nA <= N && -M <= nA-nB <= N)
    A入库
else
    nA--
signal(mutex)

B 入库进程:

wait(mutex)
nB++
if(nB <= N && -M <= nA-nB <= N)
    B入库
else
    nB--
signal(mutex)

C 出库进程:

wait(mutex)
if(nA>0 && nB>0)
    nA--;nB--
    C出库
signal(mutex)

changed:

semaphore initiation:
    muxtex = 1
    empty  = N
    delta  = M
    nA = nB = 0
progress A:
do{
    P(empty)
    P(mutex)
        A in
    V(mutex)
    V(nA)
    V(delta)
}while(true)

progress B:
do{
    P(empty)
    P(delta)
    P(mutex)
        B in
    V(mutex)
    V(nB)
}while(true)

progress C:
do{
    P(nA)
    P(nB)
    P(mutex)
        C out
    V(mutex)
    V(empty)
    V(empty)
}while(true)

操作系统第五章作业 CPU 调度


5.1. Why is it important for the scheduler to distinguish I/O-bound programs from CPU-bound programs?

对于长期调度程序,知道进程是 IO 约束的还是 CPU 约束的之后,便可以按照一定比例组合被调度的进程,使得 CPU 和 IO 设备都会一直处于负载状态,从而提高资源的使用率。 对于短期调度(CPU 调度),IO 约束的进程 CPU 区间都比较短,而 CPU 越苏进程则比较长,因而根据最短作业优先准则,可以优先调度 IO 约束进程。

5.2. Discuss how the following pairs of scheduling criteria conflict in certain settings

  • a. CPU utilization and response time CPU 利用率与上下文交换频率成负相关,而响应时间与之成正相关,故冲突。
  • b. Average turnaround time and maximum waiting time 为了减小平均周转时间,通常会采用短作业优先算法,而这会令长作业的等待时间增大。
  • c. I/O device utilization and CPU utilization 为了提高 I/O 设备的利用率,需要优先执行 IO bound 进程,因为 IO 约束的进程 CPU 区间短,因而导致频繁的上下文交换,因而降低 CPU 效率。

5.4 Consider the following set of processes, with the length of the CPU-burst time given in milliseconds

Process Burst Time Priority
$ P_1 $ 10 3
$ P_2 $ 1 1
$ P_3 $ 2 3
$ P_4 $ 1 4
$ P_5 $ 5 2

The processes are assumed to have arrived in the order $ P_1,P_2,P_3,P_4,P_5 $, all at time 0.

  • a. Draw four Gantt charts illustrating the execution of these processes using FCFS , SJF , a nonpreemptive priority (a smaller priority number implies a higher priority), and RR (quantum = 1) scheduling. 1. FCFS $ P_1 $ (10)       | $ P_2 $(1)   | $ P_3 $(2)   | $ P_4 \((1)   |\) P_5 $ (5)     --- | --- | --- | --- | --- 2. SJF $ P_2 $(1)   | $ P_4 $(1)   | $ P_3 $(2)   | $ P_5 $ (5)     | $ P_1 $ (10)       --- | --- | --- | --- | --- 3. nopreemptive priority $ P_2 $(1)   | $ P_5 $ (5)     | $ P_1 $ (10)       | $ P_3 $(2)   | $ P_4 $(1)   --- | --- | --- | --- | --- 4. Round Robin $ P_1 $| $ P_2 $ | $ P_3 $ | $ P_4 $ |$ P_5 $ | $ P_1 $| $ P_3 $ | $ P_5 $ | $ P_1 $ |$ P_5 $ | $ P_1 $ | $ P_5 $ | $ P_1 $ | $ P_5 $ | $ P_1 $ | $ P_1 $ | $ P_1 $ | $ P_1 $ | $ P_1 $ | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
  • b. What is the turnaround time of each process for each of the scheduling algorithms in part a?
    FCFS SJF nopreemptive priority RR
    $ P_1 $ 10 19 16 19
    $ P_2 $ 11 1 1 2
    $ P_3 $ 13 4 18 7
    $ P_4 $ 14 2 19 4
    $ P_5 $ 19 9 6 14
  • c. What is the waiting time of each process for each of the scheduling algorithms in part a?
    FCFS SJF nopreemptive priority RR
    $ P_1 $ 0 9 6 9
    $ P_2 $ 10 0 0 1
    $ P_3 $ 11 2 16 5
    $ P_4 $ 13 1 18 3
    $ P_5 $ 14 4 1 9
    sum 48 16 41 27
    - d. Which of the schedules in part a results in the minimal average waiting time (over all processes)?

    答:SJF with 3.2 ms

5.10 Explain the differences in the degree to which the following scheduling algorithms discriminate in favor of short processes

  • a. FCFS
  • b. RR
  • c. Multilevel feedback queues

答:

  • FCFS 哪个先到哪个优先,因此对待短进程和长进程是平等的。
  • RR 给每个进程分配相同的时间片,因此也是平等的。
  • 对于一种典型的多级反馈队列算法(有三个队列,前两个使用 RR 算法,时间片分别为 8ms,16ms,第三个使用 FCFS 算法,队列间使用抢占式优先级算法),短作业都会处于前面的队列,以此短作业的优先级更高。

5. Please prove: SJF gives the minimum average waiting time for a given set of processes to arrive at the same time

假设 CPU 按照一定顺序执行这些进程,每个进程 CPU 区间分别为$ t_1,t_2,\dots,t_n $。(非抢占) 则平均等待时间为: $$ \overline t = \frac{1}{n}\sum_{i=1}^n{(i-1)t_i} $$ 由排序不等式知$ t_1,t_2,\dots,t_n $降序时最短,即 SJF 算法。

6. What is Processor Affinity? What is load balancing? What is the relationship between the two?

处理器亲和性指在多处理器调度算法中,由于 cache miss 的代价比较高,应该努力让一个进程在一个处理器上运行。 而负载平衡指将工作负载平均分配到每个处理器上,从而保持每个处理器使用率都比较高。 因此当一个处理器负载较高时,负载平平衡策略会倾向让负载分配到多个处理器上,而处理器亲和则倾向保持该状态。 因此这两种策略有矛盾的关系。

罗小黑战记

今天去看小黑电影了,发生了许多有趣的事情。

14:20 出门时遇到 lfy 了,他什么都不带要去朝天门玩。我们刚好顺路,都要去三峡广场(sb 地图,推荐我坐半个小时公交车绕路去,幸好 fy 提醒了我走路去,只走了不到 10 分钟就到了)。

到了电影院后,发现电影院其实挺小的,大厅就是等候室,几排火车站的那种椅子。由于我早了 40 多分钟,便坐在那儿等。

入场了,果然来的人很少。我前边只有一对情侣,我右边没人,左边是三个妹纸(怎么感觉有点幸运,“自动变色”),后面我只注意到了一对男生,以及一位年纪好像还挺大的阿姨(难道是真爱?感觉大人看动画电影的只会是一家人的情况呀)。

电影开始了(之前都是黑屏,在开始前 2 分钟开始放广告,如果黑屏的时间里放和小黑有关的宣传片多好呀,不知道首映的时候会不会放)。

播放没几分钟,突然黑屏了。台下开始只言片语起来,只听见左边的三个女生中两个外向点女生调侃第一次出来就运气这么不好,还说还不如去吃东西,接着说要不要去退票。不过中间的女生说她还想看看,她们可以去问问能不能退票(这才是真爱呀,推断应该是她安利她们两个过来看的)。然后最左边的女生便主动出去叫工作人员了。几分钟后设备弄好了,电影继续。

观感

只要你吹罗小黑,我们就是好朋友。 剧情十分紧凑,基本没有什么废话。剧情衔接流畅自然。电影从中间开始,一路炫酷打斗,高潮不断。结尾也不拖沓,风息化作森林,小黑认无限为师父继续流浪,一切都释然了。结尾的也有彩蛋,山新对小白说(山新)说:“早晚有一天你会遇到你生命中的那只猫的”,衔接了 TV 版。

总结

我喜欢小黑的原因就是因为他里面一切都很纯粹,没有坏人,没有阴谋诡计。所以看得十分舒服。电影有丰富有趣的笑点。意境也很美——茂密的森林,鸡犬相闻的乡村,农田,夜晚,星星,大海,繁华的都市,一切都是那么的平衡,都让人感叹世界真美。人物性格鲜活分明。

p.s. 今天晚上 b 站刷小黑的各种周边视频,发现原来这种风格的动画用“治愈”来描述比较合适。

操作系统第三章 作业

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 的情况则弥补了这一点,当一个用户线程阻塞时可以被其它用户线程替换,因此处理器效率最高。

操作系统第三章 作业

3.1

Describe the differences among short-term, medium-term, and long-term scheduling.

  • 短期调度 从内存中的就绪队列里选择一个进程执行。执行最为频繁,为保证执行时间短,因而调度算法不能太复杂。
  • 长期调度 也称为工作调度,决定从磁盘中将哪些进程放入内存。因为执行很不频繁,所以可以使用更复杂的调度算法,从而使内存中进程的 CPU 使用和 I/O 使用更均衡,资源利用率更高。
  • 中期调度 有交换的机制,即将进程从内存中移除,并在适当时间再次将其调入内存。这有助于动态改善进程组合,使 CPU 和 I/O 使用更均衡。

3.2

Describe the actions taken by a kernel to context-switch between processes.

进程间的上下文切换,即切换到另一个进程时需要保留当前进程的状态并恢复另一进程的状态。这些状态可以用 PCB 即进程控制块表示,包含进程状态,程序计数器,CPU 寄存器等信息。上下文切换时,主要执行一个状态保存和状态恢复过程,可能还要执行一些体系结构有关的操作比如清除 cache 等。

3.A

Consider a computer with N processors in a multiprocessor configuration.

a. How many processes can be in each of the Ready, Running, and Waiting states at one time? 答:若干个就绪阶段,不超过 N 个执行阶段,若干个等待阶段。 b. What is the minimum number of processes that can be in each of the Ready, Running, and Waiting states at one time? 答:最少当然都是 0 个?

3.B

Please explain the five states of a process.

  • new: 进程刚被创建时,处于此阶段。如 linux 中 fork 系统调用创建新进程时。
  • ready: 进程执行等待被分配到 CPU 中执行。
  • run: 进程正在被执行。
  • waiting: 进程由于需要等待 I/O 等设备的数据,而不能执行,被放置对应设备的等待队列中。
  • terminated: 进程执行结束,正在释放进程的 PCB。

3.C

what is the role of PCB in OS? What information is contained in PCB? 操作系统对进程的创建、删除、调度都是通过对 PCB 的操作来完成的。 PCB 包含:

  • 进程状态
  • 程序计数器
  • cpu 寄存器
  • cpu 调度信息:如进程优先级,调度队列指针等。
  • 内存管理信息:基址,页表或段表等。
  • accounting info: cpu 时间时间,时间界限,进程数等。
  • I/O 状态信息:分配的 I/O 列表,打开的文件列表等待。

3.D

When a process is created, the operating system needs to be done?

  • 给新进程分配物理和逻辑资源(CPU 时间,内存,文件,I/O 设备,可来自操作系统或继承父进程资源)
  • 传递初始化数据

3.E

Please list the differences between message passing system and shared memory system.

  • 共享内存系统基本只能在同一台计算机(可多核)两进程传递信息,而信息传递系统既可以是同一台计算机也可以是通过网络连接的不同计算机间进程传递信息。
  • 共享内存系统只需为进程分配共享内存,通信由进程负责,而信息传递系统需要操作系统参与进行进程间的信息传递。

操作系统第二章 作业

2.2

List five services provided by an operating system that are designed to make it more convenient for users to use the computer system. In what cases it would be impossible for user-level programs to provide these services? Explain.

1 程序执行 服务:操作系统将程序装载入内存并执行程序。 若使用用户程序提供该服务,则很有可能带来安全隐患,因为应用程序可能会装载非法程序。

2 I/O 操作 服务:系统提供方便操作 I/O 设备的方法。 若使用用户程序提供该服务,在多用户系统中,应用程序可能会窃取其它用户存储在硬盘里的数据。

3 文件系统操作 服务:提供文件的,创建、删除、搜索、获取信息方法,并提供访问权限管理。 同样,应用程序可能会非法访问文件。

4 通信 服务:提供进程间的相互通信。(同一计算机内的不同进程或通过网络链接的不同计算机的进程) 同样,应用程序可能会非法窃听其它应用的信息。

5 错误检测 服务:对于发生在硬件上或用户程序中的错误,操作系统采取适当的处理措施。 若使用应用程序提供该服务,则应用程序在编写时需要考虑所有可能的错误,这是不现实的。

2.5

What are the five major activities of an operating system in regard to file management?

  • 文件的创建和删除
  • 文件的打开和关闭
  • 文件的读、写、重定位
  • 读取文件属性和设置文件属性
  • 文件的权限管理

2.8

What are the two models of interprocess communication? What are the strengths and weaknesses of the two approaches?

1 共享内存 (shared-memory model) 优点:

  • 速度快
  • 访问数据方便

缺点:

  • 不易进行数据的保护和同步

2 消息 (message-passing model) 优点:

  • 不必避免冲突
  • 计算机间通信时,更容易实现

缺点:

  • 大量数据传输时,速度慢

操作系统第一章 作业

1.3

Under what circumstances would a user be better off using a timesharing system rather than a PC or single-user workstation? 答:当计算机性能足够强大的时候,采用分时系统可以让计算机同时地,快速地,为许多用户解决复杂的问题,并使得计算资源得到充分的利用。此时若让每个用户使用 pc 机去解决问题会需要很久,而给使用工作站成本又太高。

1.5

Describe the differences between symmetric and asymmetric multiprocessing. What are three advantages and one disadvantage of multiprocessor systems? 答:

  1. 区别:
  • 对称多处理器中的每个核对于程序来讲是完全相同的,而非对称多处理的每个核都有自己擅长处理的特定的计算任务,是不同的。
  • 对称多处理器中的每个核都可以进行 I/O 操作,而非堆成中多处理中,有一个主核控制其它从核,通常只有主核执行 I/O 操作。
  1. 优缺点:
  • 优点:

    1. 利用了并行性,增加了系统的吞吐量。
    2. 因为不同 cpu 共享外设,大容量存储和电源等资源,因此更加经济。
    3. 因为即使一个 cpu 出了问题,整个系统仍可以正常工作,因此更加可靠。
  • 缺点:

    1. 需要编程人员编写并行性的程序才能发挥效果,增加编程工作量。

1.10

What is the purpose of interrupts? What are the differences between a trap and an interrupt? Can traps be generated intentionally by a user program? If so, for what purpose? 答:

  1. 中断的目的:
  • 使用中断访问 I/O 机制,可以消除 cpu 对 I/O 设备的轮询,减少 cpu 的等待时间。
  • 通过中断更容易对外设进行响应,如按下键盘会产生一个中断,计算机处理这个中断便会跳转到响应处理程序。
  1. 中断是硬件产生的,陷阱(trap)是软件产生的。
  2. 可以,如软件需要进行系统调用时。

计算机组成原理 第一章作业

教材:计算机组成原理:软硬件接口 第 5 版 (mips 版) 时间:2019/09/08

Exercise 1.2

  1. a: "Assembly lines in automobile manufacturing" -> "Performance via Pipelining"
  2. b: "Suspension bridge cables" -> “Dependability via Redundancy”
  3. c: "Aircraft and marine navigation systems that incorporate wind information" -> “Performance via Prediction”
  4. d: "Express elevators in buildings" -> “Performance via Parallelism”
  5. e: "Library reserve desk" -> “Hierarchy of Memories”
  6. f: "Increasing the gate area on a CMOS transistor to decrease its switching time" -> “Make the Common Case Fast”
  7. g: "Adding electromagnetic aircraft catapults (which are electrically-powered as opposed to current steam-powered models), allowed by the increased power generation off ered by the new reactor technology" -> “Design for Moore’s Law”
  8. h: "Building self-driving cars whose control systems partially rely on existing sensor systems already installed into the base veh icle, such as lane departure systems and smart cruise control systems" -> “Use Abstraction to Simplify Design”

Exercise 1.3

以 C 语言为例:     编译     汇编      链接 $ 源代码 \Longrightarrow 汇编语言 \Longrightarrow 目标代码 \Longrightarrow 机器代码$

  • 编译:通过编译器,读取源程序(字符流),对之进行词法和语法的分析,将高级语言指令转换为功能等效的汇编代码的过程。
  • 汇编:通过汇编器将汇编语言代码翻译成目标机器指令的过程。
  • 链接:将有关的目标文件彼此相连接,也即将在一个文件中引用的符号同该符号在另外一个文件中的定义连接起来,使得所有的这些目标文件成为一个能够被操作系统装入执行的统一整体。

Exercise 1.4

a) $ frame size = 1280 \times 1024 \times 3 \times 8bit = 3.75MB $ b) $ time = \frac{ 1280 \times 1024 \times 3 \times 8bit }{100 \times 10^{6} bit} \approx 0.31s $

Exercise 1.5

1.5.1

\(I\)为指令数,\(t\)为 cpu 执行时间,\(f\)为时钟频率,则: $$ I = \frac{t \cdot f}{CPI} $$ $ I_{p1} = {1 \times 3 \over 1.5} = 2G 条$ $ I_{p2} = {1 \times 2.5 \over 1} = 2.5G 条$ $ I_{p3} = {1 \times 4 \over 2.2} \approx 1.82G 条$

1.5.2

设$ C $为时钟数,则:

$ C_{p1} = 10 \times 3 = 30G $ $ I_{p1} = {30 \over 1.5} = 20G $

$ C_{p2} = 10 \times 2.5 = 25G $ $ I_{p2} = {25 \over 1} = 25G $

$ C_{p3} = 10 \times 4 = 40G $ $ I_{p3} = {40 \over 2.2} \approx 18.2G $

1.5.3

(1) $ t = \frac{I \cdot CPI}{f} $ (2) $ (1-0.3)t = \frac{I \cdot (1+0.2)CPI}{f'} $

$ 由\frac{(1)}{(2)} \Rightarrow \frac{clock rate'}{clock rate} = {(1+0.2) \over (1-0.3)} = 1.71$

Exercise 1.6

1.6.1

令$ p_i $为一条指令占总指令的比例,则对于平均 CPI,有: $$ \overline {CPI} = \sum_{i=1}^nCPI_i \cdot p_i $$ 对于 P1:$ \overline {CPI} = 0.1\times 1 + 0.2\times 2 + 0.5\times 3 + 0.2\times 3 = 2.5 $ 对于 P2:$ \overline {CPI} = 2 $

1.6.2

对于 P1,时钟数:$ C = \frac{1.0 \times 10^6}{2.5} = 4\times 10^5 $ 对于 P2,时钟数:$ C = \frac{1.0 \times 10^6}{2} = 5\times 10^5 $

Exercise 1.7

a)

设$ T \(为时钟周期,\) t \(为程序执行时间,\) I $为指令数,则: $$ CPI = \frac{t}{I \cdot T} $$ $ \overline {CPI_A} = \frac{1.1s}{1.0 \times 10^9 \times 1 ns} = 1.1$ $ \overline {CPI_B} = \frac{1.5s}{1.2 \times 10^9 \times 1 ns} = 1.25$

b)

设$ f = \frac{1}{T} $,利用上述公式,得: $$ \frac{CPI_A}{CPI_B} = \frac{I_B \cdot T_{PB}}{I_A \cdot T_{PA}}\ \Rightarrow \frac{1.1}{1.25} = \frac{1.2 f_{PA}}{1.0 f_{PB}}\ \Rightarrow \frac{f_{PA}}{f_{PB}} = 0.733 $$ 答:处理器 A 的时钟频率为处理器 B 的 0.733 倍

c)

仍假设 cpu 时钟周期为 1ns,$ t_{new} = 6.0 \times 10^8 \times 1.1 \times 1ns = 0.66s $

对于 A: $ 加速比 = (1.1 - 0.66)/1.1 = 40\% $ 对于 B:$ 加速比 = (1.5 - 0.66)/1.5 = 29.3\% $

Exercise 1.8

1.8.1

设 P 为功耗,C 为负载电容,V 为电压,f 为开关频率,动态功耗公式: $$ P = C \cdot U^2 \cdot f $$ $ C_{Pentium 4} = \frac{90}{1.25^2 \times 3.6 \times 10^6} = 16 \mu F$ $ C_{Core i5} = \frac{40}{0.9^2 \times 3.4 \times 10^6} \approx 14.5 \mu F$

1.8.2

$ 设 p1 为静态功耗占总功耗的比例,p2 为静态功耗相对于动态功耗的比例。 $

$ Pentium 4: p1 = 10/100 = 10\%, p2 = 10/90 = 11.1\%$ $ Core i5: p1 = 30/70 = 42.9\%, p2 = 30/40 = 75\%$

1.8.3

\[ P_总 = P_动 + P_静 = C \cdot U^2 \cdot f + I \cdot U$$ $ 因为 I 不变,设变化后的电压为 U',t = \frac{U'}{U},则: $ $$ P_总' = P_动 \cdot \left( \frac{U'}{U} \right)^2 + P_静 \cdot \left( \frac{U'}{U} \right) \\ \Rightarrow P_总' = P_动 \cdot t^2 + P_静 \cdot t \]

对于 P4: $ 90 = 90t^2 + 10t \Rightarrow t \approx 0.946 $ 对于 i5: $ 63 = 40t^2 + 30t \Rightarrow t \approx 0.935$ 答:对于 P4,电压降为原来的$ 94.6\% \(,对于 i5,电压降为原来的\) 93.5\% $

序章

写博客在我看来就犹如创建一个属于自己的秘密基地, 那是一个属于自己内心的场所。

偶尔期待着有人误入这里, 通过文字向他们表达真实的自己。