当前位置:网站首页>CSAPP家庭作業答案7 8 9章
CSAPP家庭作業答案7 8 9章
2022-07-06 14:58:00 【蕪湖韓金輪】
答案僅供參考
7.12
解題思路是模仿鏈接器的行為:使用重定比特記錄來識別比特置然後使用圖7.9中的算法計算重新定比特的絕對地址,或者簡單地從圖7.10中的重定比特指令中提取它們。有兩點需要注意的是第8行的movl指令包含兩個需要重定比特的引用。第5行和第8行的指令包含對buf[1]的引用,初始值為Ox4,重定比特的地址計算為ADDR(buf) +4。
- 第一次重定比特是mov 0x0,%edx,對應於圖7-10中的15行,將swap.o中偏移量為3-6的比特置上的0x0重定比特於運行時的真實地址0x80483c8+3=0x80483cb,其值為0x804945c
- 第二次重定比特是mov 0x4,%eax,在圖中16行,將swap.o中偏移量為8-b比特置上的0x4重定比特與運行時的真實地址0x80483c8+8=0x80483d0,其值為0x8049458
- 第三次和第四次重定比特在movl $0x4,0x0,對應於圖7-10的18行,將swap.o中偏移量為10-13的0x0和偏移量為14-17的0x4重定比特於運行時的地址0x80483c8+10=0x80483d8和0x80483c8+14=0x80483dc,並將movl $0x4,0x0修改為movl $0x8049458,0x8049548
- 第五次重定比特是mov 0x0,%eax,在圖7-10的23行,將偏移量為1f-22的0x0重定比特於地址0x80483c8+1f=0x80483e7上的值0x8049548
結果如下錶所示:
| 圖7-10中的行號 | 地址 | 值 |
|---|---|---|
| 15 | 0x80483cb | 0x004945c |
| 16 | 0x80483d0 | 0x0049458 |
| 18 | 0x80483d8 | 0x0049548 |
| 18 | 0x80483dc | 0x0049458 |
| 23 | 0x80483e7 | 0x0049548 |
7.13
A
在.text的節的節偏移12處調用了函數,在節偏移為19處發生了值的傳遞,並且值為0x0,則要被重定比特。結合c代碼可知在此處將調用p3()的返回值放在edx寄存器中,再和*xp相加,然後再在偏移為21的比特置發生函數p2()的調用。所以三次重定比特的節偏移分別為12,19,21,分別對指令call 12<p1+0xa>、指令mov 0x0,%eax和指令call 21<p1+0x19>進行重定比特。 或者直接使用objdump反匯編也可以得到相同結果。
結果如下錶所示:
| 節偏移 | 重定比特類型 | 符號名字 |
|---|---|---|
| 0x12 | R_386_PC32(相對引用) | p3 |
| 0x19 | R_386_32(絕對引用) | xp |
| 0x21 | R_386_PC32(相對引用) | p2 |
B
.data 節中修改了xp的值,重定比特於x的地址。所以 .data 節只有一次重定比特, 節偏移為0x4,重定比特類型是重定比特絕對引用,符號名字是xp。
| 節偏移 | 重定比特類型 | 符號名字 |
|---|---|---|
| 0x4 | R_386_32(絕對引用) | xp |
8.23
父進程接收並捕獲第一個信號,當處理程序還在處理第一個信號時,第二個信號傳來被添加到待處理集合中,此時被處理程序阻塞了就還不會被接收,當第三個信號傳來,由於類型和第二個信號相同就會被丟弃,第4、5個信號也是如此。當第一個信號處理完畢後,內核注意到還有一個待處理信號(信號2)就會强迫父進程接收,然後執行處理程序,第二次處理完畢後,沒有待處理的信號了,就結束。所以只會接收處理兩個信號,counter值只會是2,而不是5。
8.24
題目的要求是在子進程中對只讀文本段進行寫操作,這會導致段錯誤,所以第二個要求是要輸出段錯誤。而判斷异常終止可以使用WIFSIGNALED來判斷,若為非0則發生异常終止,修改後的代碼如下:
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#define N 2
int main()
{
int status,i;
pid_t pid;
for(i = 0;i < N;i++)
if((pid = fork()) == 0)
{
int *pt = 0x0;
*pt = 15213;
exit(100+i);
}
while((pid = waitpid(-1,&status,0)) > 0)
{
if(WIFEXITED(status))
printf("child %d terminated normally with exit status = %d\n",pid,WEXITSTATUS(status));
else if(WIFSIGNALED(status))
{
fprintf(stderr,"child %d terminated by signal %d",pid,WTERMSIG(status));
psignal(WTERMSIG(status)," ");
}
else
printf("child %d terminated abnormally\n",pid);
}
if(errno != ECHILD)
printf("waitpid error\n");
exit(0);
}
輸出如下:

9.11
A. 虛擬地址為0x027c,轉換成二進制:00 0010 0111 1100
| 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
B .按照9.6.4節的假設,VPO為地址的0比特到5比特,本題為111100,VPN為6比特到13比特,本題為00001001,即0x9,TLBI是6-7比特為0x1,TLBT為8-13比特為0x2。查錶可知索引比特0x1、標記比特為0x2,不命中,MMU需要從頁錶中的PTE中取出PPN,得到有效的PPN為0x17,沒有缺頁。
完整錶格為:
| 參數 | 值 |
|---|---|
| VPN | 0x9 |
| TLB索引 | 0x1 |
| TLB標記 | 0x2 |
| TLB命中?(是/否) | 否 |
| 缺頁?(是/省) | 否 |
| PPN | 0x17 |
C. 將得到的物理頁幀號0x17( 01 0111 )作為實際物理地址的前6比特,偏移量不變,仍為VPO( 11 1100 ),得到最終的物理地址: 0101 1111 1100
| 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
D. 將物理地址的6-11比特的010111作為CT,其值為0x17,2-5比特的1111作為CI其值為0xf,0-1比特作為塊偏移CO值為0x0。在高速緩存中找索引比特0xf的組、標記比特0x17的行、偏移為0x0的字節,由高速緩存錶可知0xf的組無0x17標記的行,故不命中,無返回字節。 結果如下錶:
| 參數 | 值 |
|---|---|
| 字節偏移 | 0x0 |
| 緩存索引 | 0xf |
| 緩存標記 | 0x17 |
| 緩存命中?(是/否) | 否 |
| 返回的緩存字節 | - |
9.12
A.虛擬地址為0x03a9,其二進制為00 0011 1010 1001
| 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 1 | 0 | 1 | 0 | 0 | 1 |
B. 按照9.6.4節的假設,VPO為地址的0比特到5比特,本題為101001,VPN為6比特到13比特,本題為0000 1110,即0xe,TLBI是6-7比特為0x2,TLBT為8-13比特為0x3。查錶可知索引比特0x2、標記比特為0x3,由於有效比特為0不命中,MMU需要從頁錶中的PTE中取出PPN,得到有效的PPN為0x11,沒有缺頁。
完整錶格為:
| 參數 | 值 |
|---|---|
| VPN | 0xe |
| TLB索引 | 0x2 |
| TLB標記 | 0x3 |
| TLB命中?(是/否) | 否 |
| 缺頁?(是/省) | 否 |
| PPN | 0x11 |
C. 將得到的物理頁幀號0x11( 01 0001)作為實際物理地址的前6比特,偏移量不變,仍為VPO( 10 1001 ),得到最終的物理地址: 0100 0110 1001
| 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 1 |
D:將物理地址的6-11比特的010001作為CT,其值為0x11,2-5比特的1010作為CI其值為0xa,0-1比特作為塊偏移CO值為0x1。在高速緩存中找索引比特0xa的組、標記比特0x11的行、偏移為0x1的字節,由高速緩存錶可知0xa的組無0x11標記的行,故不命中,無返回字節。
完整錶格:
| 參數 | 值 |
|---|---|
| 字節偏移 | 0x1 |
| 緩存索引 | 0xa |
| 緩存標記 | 0x11 |
| 緩存命中?(是/否) | 否 |
| 返回的緩存字節 | - |
9.13
A.虛擬地址為0x0040,其二進制為00 0000 0100 0000
| 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
B. 按照9.6.4節的假設,VPO為地址的0比特到5比特,本題為000000,,VPN為6比特到13比特,本題為0000 0001,即0x1,TLBI是6-7比特為0x1,TLBT為8-13比特為0x0。 當VPN=1時,有效比特為0,PPN無。所以就沒有物理地址。有缺頁。
| 參數 | 值 |
|---|---|
| VPN | 0x1 |
| TLB索引 | 0x1 |
| TLB標記 | 0x0 |
| TLB命中?(是/否) | 否 |
| 缺頁?(是/省) | 是 |
| PPN | 0x11 |
C.缺頁
D.缺頁
9.14
代碼如下
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
int main(int argc, char const *argv[])
{
int fd;
if ((fd = open("hello.txt", O_RDWR)) == -1) {
perror("Failed to open hello.txt");
exit(EXIT_FAILURE);
}
struct stat stat_of_file;
if (fstat(fd, &stat_of_file) == -1) {
perror("Failed to get stat of hello.txt");
exit(EXIT_FAILURE);
}
char *p;
if ((p = mmap(NULL, stat_of_file.st_size, PROT_WRITE, MAP_SHARED, fd, 0)) == (void *)-1) {
perror("Failed to mmap");
exit(EXIT_FAILURE);
}
p[0] = 'J';
munmap(p, stat_of_file.st_size);
close(fd);
return 0;
}
運行結果:
边栏推荐
- [oiclass] maximum formula
- How to transform functional testing into automated testing?
- 使用 flask_whooshalchemyplus jieba实现flask的全局搜索
- Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
- Global and Chinese market of maleic acid modified rosin esters 2022-2028: Research Report on technology, participants, trends, market size and share
- “人生若只如初见”——RISC-V
- 函数:计算字符串中大写字母的个数
- Cc36 different subsequences
- 浙大版《C语言程序设计实验与习题指导(第3版)》题目集
- servlet中 servlet context与 session与 request三个对象的常用方法和存放数据的作用域。
猜你喜欢

Fundamentals of digital circuit (V) arithmetic operation circuit

Soft exam information system project manager_ Project set project portfolio management --- Senior Information System Project Manager of soft exam 025

About the garbled code problem of superstar script

Cc36 different subsequences

"If life is just like the first sight" -- risc-v

Wang Shuang's detailed learning notes of assembly language II: registers

Don't you even look at such a detailed and comprehensive written software test question?

Database monitoring SQL execution

四元数---基本概念(转载)

CSAPP家庭作业答案7 8 9章
随机推荐
Function: find the root of the equation by Newton iterative method
How to learn automated testing in 2022? This article tells you
Es full text index
王爽汇编语言详细学习笔记二:寄存器
Global and Chinese markets of PIM analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
ByteDance ten years of experience, old bird, took more than half a year to sort out the software test interview questions
关于交换a和b的值的四种方法
STC-B学习板蜂鸣器播放音乐
1. Payment system
5分钟掌握机器学习鸢尾花逻辑回归分类
【指针】统计一字符串在另一个字符串中出现的次数
Fundamentals of digital circuits (II) logic algebra
Zhejiang University Edition "C language programming experiment and exercise guide (3rd Edition)" topic set
[pointer] counts the number of times one string appears in another string
函数:字符串反序存放
About the garbled code problem of superstar script
Login the system in the background, connect the database with JDBC, and do small case exercises
"If life is just like the first sight" -- risc-v
Global and Chinese market of DVD recorders 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets of electronic grade hexafluorobutadiene (C4F6) 2022-2028: Research Report on technology, participants, trends, market size and share