当前位置:网站首页>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 :
边栏推荐
- {1,2,3,2,5} duplicate checking problem
- "If life is just like the first sight" -- risc-v
- [pointer] octal to decimal
- Want to learn how to get started and learn software testing? I'll give you a good chat today
- 内网渗透之内网信息收集(三)
- Pointer -- output all characters in the string in reverse order
- Statistics 8th Edition Jia Junping Chapter 5 probability and probability distribution
- Wang Shuang's detailed notes on assembly language learning I: basic knowledge
- 数字电路基础(三)编码器和译码器
- Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
猜你喜欢
[Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases
Fundamentals of digital circuits (I) number system and code system
关于超星脚本出现乱码问题
Intranet information collection of Intranet penetration (3)
Cadence physical library lef file syntax learning [continuous update]
数字电路基础(四) 数据分配器、数据选择器和数值比较器
“人生若只如初见”——RISC-V
Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008
关于交换a和b的值的四种方法
Software testing interview summary - common interview questions
随机推荐
"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
Public key box
指针 --按字符串相反次序输出其中的所有字符
Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class
Function: find the root of the equation by Newton iterative method
Detailed introduction to dynamic programming (with examples)
[oiclass] share prizes
四元数---基本概念(转载)
【指针】查找最大的字符串
王爽汇编语言学习详细笔记一:基础知识
基于485总线的评分系统双机实验报告
Global and Chinese market of RF shielding room 2022-2028: Research Report on technology, participants, trends, market size and share
Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
Get started with Matplotlib drawing
Fundamentals of digital circuit (V) arithmetic operation circuit
The common methods of servlet context, session and request objects and the scope of storing data in servlet.
【指针】数组逆序重新存放后并输出