当前位置:网站首页>CSAPP homework answers chapter 789
CSAPP homework answers chapter 789
2022-07-06 14:59:00 【Wuhu hanjinlun】
The answer is for reference only
7.12
The idea of problem solving is to imitate the behavior of linker : Use the relocation record to identify the location, and then use the figure 7.9 The algorithm in calculates the absolute address of relocation , Or simply from the figure 7.10 Extract them from the relocation instructions in . There are two points to be noted 8 Yes movl The directive contains two references that need to be relocated . The first 5 Xing He 8 The instruction of the line contains a pair of buf[1] References to , The initial value is Ox4, The relocated address is calculated as ADDR(buf) +4.
- The first relocation is mov 0x0,%edx, It corresponds to Fig 7-10 Medium 15 That's ok , take swap.o The middle offset is 3-6 On the position of 0x0 Reset to the real address of the runtime 0x80483c8+3=0x80483cb, Its value is 0x804945c
- The second relocation is mov 0x4,%eax, In the picture 16 That's ok , take swap.o The middle offset is 8-b In position 0x4 Relocation and the real address of the runtime 0x80483c8+8=0x80483d0, Its value is 0x8049458
- The third and fourth repositioning are movl $0x4,0x0, It corresponds to Fig 7-10 Of 18 That's ok , take swap.o The middle offset is 10-13 Of 0x0 And the offset is 14-17 Of 0x4 Reset the address at runtime 0x80483c8+10=0x80483d8 and 0x80483c8+14=0x80483dc, And will movl $0x4,0x0 It is amended as follows movl $0x8049458,0x8049548
- The fifth relocation is mov 0x0,%eax, In the figure 7-10 Of 23 That's ok , Set the offset to 1f-22 Of 0x0 Relocate to address 0x80483c8+1f=0x80483e7 Value on 0x8049548
The results are shown in the table below :
chart 7-10 The line number in | Address | value |
---|---|---|
15 | 0x80483cb | 0x004945c |
16 | 0x80483d0 | 0x0049458 |
18 | 0x80483d8 | 0x0049548 |
18 | 0x80483dc | 0x0049458 |
23 | 0x80483e7 | 0x0049548 |
7.13
A
stay .text Section offset of section 12 Function called at , In the section, the offset is 19 A value transfer occurred at , And the value of 0x0, Will be relocated . combination c The code knows that... Will be called here p3() The return value of is placed in edx In the register , And again *xp Add up , Then offset to 21 Position generating function p2() Call to . Therefore, the node offsets of the three relocations are 12,19,21, Separate instructions call 12<p1+0xa>、 Instructions mov 0x0,%eax And instructions call 21<p1+0x19> To relocate . Or use it directly objdump Disassembly can also get the same result .
The results are shown in the table below :
Node offset | Relocation type | Symbol name |
---|---|---|
0x12 | R_386_PC32( Relative reference ) | p3 |
0x19 | R_386_32( Absolutely quote ) | xp |
0x21 | R_386_PC32( Relative reference ) | p2 |
B
.data Section modified xp Value , Relocate to x The address of . therefore .data The section is repositioned only once , The section offset is 0x4, Relocation type is relocation absolute reference , The symbol name is xp.
Node offset | Relocation type | Symbol name |
---|---|---|
0x4 | R_386_32( Absolutely quote ) | xp |
8.23
The parent process receives and captures the first signal , While the handler is still processing the first signal , The second signal is added to the set to be processed , At this time, it will not be received if it is blocked by the handler , When the third signal comes , Because the type is the same as the second signal, it will be discarded , The first 4、5 The same is true of a signal . When the first signal is processed , The kernel notices that there is another pending signal ( The signal 2) Will force the parent process to receive , Then execute the handler , After the second processing , There is no signal to be processed , ends . So only two signals will be received and processed ,counter It's only worth 2, instead of 5.
8.24
The requirement of the topic is to write the read-only text segment in the sub process , This will lead to segment errors , So the second requirement is to output segment errors . To judge abnormal termination, you can use WIFSIGNALED To judge , If not 0 Abnormal termination occurs , The modified code is as follows :
#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);
}
Output is as follows :
9.11
A. The virtual address is 0x027c, Convert to binary :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 . according to 9.6.4 The assumptions of this section ,VPO For address 0 A to 5 position , The title is 111100,VPN by 6 A to 13 position , The title is 00001001, namely 0x9,TLBI yes 6-7 Position as 0x1,TLBT by 8-13 Position as 0x2. Look up the table to know the index bit 0x1、 The tag bit is 0x2, Miss hit ,MMU From the page table PTE Remove from PPN, Get effective PPN by 0x17, There are no missing pages .
The complete table is :
Parameters | value |
---|---|
VPN | 0x9 |
TLB Indexes | 0x1 |
TLB Mark | 0x2 |
TLB hit ?( yes / no ) | no |
Page missing ?( yes / province ) | no |
PPN | 0x17 |
C. Will get the physical page frame number 0x17( 01 0111 ) As the front of the actual physical address 6 position , Constant offset , Still VPO( 11 1100 ), Get the final physical address : 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. Put the... Of the physical address 6-11 Bit 010111 As CT, Its value is 0x17,2-5 Bit 1111 As CI Its value is 0xf,0-1 Bit as block offset CO The value is 0x0. Find the index bit in the cache 0xf Group 、 Marker bit 0x17 The line of 、 Offset for the 0x0 Bytes of , From the cache table 0xf Group of none 0x17 Marked lines , So miss , No return bytes . The results are as follows :
Parameters | value |
---|---|
Byte offset | 0x0 |
Cache index | 0xf |
Cache tags | 0x17 |
A cache hit ?( yes / no ) | no |
Cache bytes returned | - |
9.12
A. The virtual address is 0x03a9, Its binary is 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. according to 9.6.4 The assumptions of this section ,VPO For address 0 A to 5 position , The title is 101001,VPN by 6 A to 13 position , The title is 0000 1110, namely 0xe,TLBI yes 6-7 Position as 0x2,TLBT by 8-13 Position as 0x3. Look up the table to know the index bit 0x2、 The tag bit is 0x3, Since the significant bit is 0 Miss hit ,MMU From the page table PTE Remove from PPN, Get effective PPN by 0x11, There are no missing pages .
The complete table is :
Parameters | value |
---|---|
VPN | 0xe |
TLB Indexes | 0x2 |
TLB Mark | 0x3 |
TLB hit ?( yes / no ) | no |
Page missing ?( yes / province ) | no |
PPN | 0x11 |
C. Will get the physical page frame number 0x11( 01 0001) As the front of the actual physical address 6 position , Constant offset , Still VPO( 10 1001 ), Get the final physical address : 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: Put the... Of the physical address 6-11 Bit 010001 As CT, Its value is 0x11,2-5 Bit 1010 As CI Its value is 0xa,0-1 Bit as block offset CO The value is 0x1. Find the index bit in the cache 0xa Group 、 Marker bit 0x11 The line of 、 Offset for the 0x1 Bytes of , From the cache table 0xa Group of none 0x11 Marked lines , So miss , No return bytes .
Complete form :
Parameters | value |
---|---|
Byte offset | 0x1 |
Cache index | 0xa |
Cache tags | 0x11 |
A cache hit ?( yes / no ) | no |
Cache bytes returned | - |
9.13
A. The virtual address is 0x0040, Its binary is 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. according to 9.6.4 The assumptions of this section ,VPO For address 0 A to 5 position , The title is 000000,,VPN by 6 A to 13 position , The title is 0000 0001, namely 0x1,TLBI yes 6-7 Position as 0x1,TLBT by 8-13 Position as 0x0. When VPN=1 when , The significant bits are 0,PPN nothing . So there is no physical address . There are missing pages .
Parameters | value |
---|---|
VPN | 0x1 |
TLB Indexes | 0x1 |
TLB Mark | 0x0 |
TLB hit ?( yes / no ) | no |
Page missing ?( yes / province ) | yes |
PPN | 0x11 |
C. Page missing
D. Page missing
9.14
The code is as follows
#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;
}
Running results :
边栏推荐
- What is the transaction of MySQL? What is dirty reading and what is unreal reading? Not repeatable?
- Global and Chinese markets of Iam security services 2022-2028: Research Report on technology, participants, trends, market size and share
- 【指针】数组逆序重新存放后并输出
- [oiclass] share prizes
- Four methods of exchanging the values of a and B
- Using flask_ Whooshalchemyplus Jieba realizes global search of flask
- 内网渗透之内网信息收集(三)
- The salary of testers is polarized. How to become an automated test with a monthly salary of 20K?
- [pointer] find the length of the string
- Cadence physical library lef file syntax learning [continuous update]
猜你喜欢
关于交换a和b的值的四种方法
The salary of testers is polarized. How to become an automated test with a monthly salary of 20K?
王爽汇编语言学习详细笔记一:基础知识
Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
Keil5-MDK的格式化代码工具及添加快捷方式
Statistics 8th Edition Jia Junping Chapter IX summary of knowledge points of classified data analysis and answers to exercises after class
移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
数字电路基础(四) 数据分配器、数据选择器和数值比较器
Keil5 MDK's formatting code tool and adding shortcuts
随机推荐
[Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases
【指针】求字符串的长度
The salary of testers is polarized. How to become an automated test with a monthly salary of 20K?
C language learning summary (I) (under update)
Detailed introduction to dynamic programming (with examples)
Wang Shuang's detailed learning notes of assembly language II: registers
ByteDance ten years of experience, old bird, took more than half a year to sort out the software test interview questions
MySQL中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
数字电路基础(一)数制与码制
What is the transaction of MySQL? What is dirty reading and what is unreal reading? Not repeatable?
关于交换a和b的值的四种方法
Global and Chinese market of barrier thin film flexible electronics 2022-2028: Research Report on technology, participants, trends, market size and share
If the position is absolute, touchablehighlight cannot be clicked - touchablehighlight not clickable if position absolute
Fundamentals of digital circuits (II) logic algebra
HackTheBox-Emdee five for life
[pointer] find the value of the largest element in the two-dimensional array
Function: string storage in reverse order
[issue 18] share a Netease go experience
Oracle foundation and system table
Software testing interview summary - common interview questions