当前位置:网站首页>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;
}
運行結果:
边栏推荐
- What is the transaction of MySQL? What is dirty reading and what is unreal reading? Not repeatable?
- Global and Chinese markets of MPV ACC ECU 2022-2028: Research Report on technology, participants, trends, market size and share
- Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
- [Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases
- 指针:最大值、最小值和平均值
- ucore lab2 物理内存管理 实验报告
- Public key box
- [pointer] octal to decimal
- Interview Essentials: what is the mysterious framework asking?
- Cc36 different subsequences
猜你喜欢

ES全文索引

Summary of thread implementation

“Hello IC World”

数字电路基础(五)算术运算电路

线程的实现方式总结

5分钟掌握机器学习鸢尾花逻辑回归分类

Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?

Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers

MySQL中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
![Cadence physical library lef file syntax learning [continuous update]](/img/0b/75a4ac2649508857468d9b37703a27.jpg)
Cadence physical library lef file syntax learning [continuous update]
随机推荐
[pointer] delete all spaces in the string s
To brush the video, it's better to see if you have mastered these interview questions. Slowly accumulating a monthly income of more than 10000 is not a dream.
四元数---基本概念(转载)
ucore lab2 物理内存管理 实验报告
5 minutes to master machine learning iris logical regression classification
Want to learn how to get started and learn software testing? I'll give you a good chat today
How to earn the first pot of gold in CSDN (we are all creators)
指针 --按字符串相反次序输出其中的所有字符
【指针】使用插入排序法将n个数从小到大进行排列
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
MySQL中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
“Hello IC World”
ES全文索引
王爽汇编语言学习详细笔记一:基础知识
Keil5 MDK's formatting code tool and adding shortcuts
后台登录系统,JDBC连接数据库,做小案例练习
Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers
Fundamentals of digital circuits (II) logic algebra
【指针】数组逆序重新存放后并输出
C language learning summary (I) (under update)