当前位置:网站首页>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
150x80483cb0x004945c
160x80483d00x0049458
180x80483d80x0049548
180x80483dc0x0049458
230x80483e70x0049548

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
0x12R_386_PC32( Relative reference )p3
0x19R_386_32( Absolutely quote )xp
0x21R_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
0x4R_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 :

 Insert picture description here

9.11

A. The virtual address is 0x027c, Convert to binary :00 0010 0111 1100

131211109876543210
00001001111100

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
VPN0x9
TLB Indexes 0x1
TLB Mark 0x2
TLB hit ?( yes / no ) no
Page missing ?( yes / province ) no
PPN0x17

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

11109876543210
010111111100

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

131211109876543210
00001110101001

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
VPN0xe
TLB Indexes 0x2
TLB Mark 0x3
TLB hit ?( yes / no ) no
Page missing ?( yes / province ) no
PPN0x11

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

11109876543210
010001101001

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

131211109876543210
00000001000000

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
VPN0x1
TLB Indexes 0x1
TLB Mark 0x0
TLB hit ?( yes / no ) no
Page missing ?( yes / province ) yes
PPN0x11

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 :
 Insert picture description here

原网站

版权声明
本文为[Wuhu hanjinlun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060919098469.html