├── Homeworks ├── Homework2 │ ├── 1183710109郭茁宁-Homework1.docx │ ├── 2_59.c │ ├── 2_63.c │ ├── 2_71.c │ ├── 2_75.c │ ├── 2_79.c │ ├── 2_95.c │ └── show_byte.c ├── Homework3 │ └── 郭茁宁-1183710109-Homework3.docx ├── Homework4-5 │ ├── qzs计算机系统第四章.docx │ └── 郭茁宁1183710109-Homework4-5.docx ├── Homework6-7 │ └── Homework6-7郭茁宁113710109.docx └── Homework8-9 │ └── 1183710109郭茁宁-Homework8-9.docx ├── Labs ├── 实验1 │ ├── HITCS-Lab1.pptx │ └── HITICS-lab1 │ │ ├── Datatype.cLinux版 │ │ └── Datatype.c │ │ ├── Datatype.cWin10版 │ │ └── Datatype.c │ │ ├── HITICS-lab1183710115-郭茁宁.doc │ │ ├── cpuWordSize.c │ │ ├── hello.i │ │ ├── hello.o │ │ ├── hello.out │ │ ├── hello.s │ │ ├── hellolinux.c │ │ ├── hellowin.c │ │ └── isLittleEndian.c ├── 实验2 │ ├── HITICS-Lab2.pptx │ └── ICS2-郭茁宁 │ │ ├── cs_atof.c │ │ ├── cs_atoi.c │ │ ├── cs_ftoa.c │ │ ├── cs_itoa.c │ │ ├── fib_dg.c │ │ ├── fib_loop.c │ │ ├── float0.c │ │ ├── floatx.c │ │ ├── utf8len.c │ │ └── 郭茁宁-实验2.doc ├── 实验3 │ ├── HITICS-Lab3.pptx │ ├── SE1801_1183710109.txt │ ├── SE1801_1183710109_郭茁宁.doc │ ├── bomb415 │ │ ├── README │ │ ├── bomb │ │ └── bomb.c │ ├── func4.cpp │ ├── ~$HITICS-Lab3.pptx │ └── ~$TICS-lab3实验报告模板.doc ├── 实验4 │ ├── 1.txt │ ├── 1183710109-郭茁宁.doc │ ├── 2.txt │ ├── 3.txt │ ├── HITICS-Lab4.pptx │ └── buflab-handout │ │ ├── bufbomb │ │ ├── hex2raw │ │ └── makecookie ├── 实验5 │ ├── HITICS-Lab5.pptx │ ├── linklab-1183710109.tar │ ├── phase1.o │ ├── phase2.o │ ├── phase3.o │ ├── phase3_path.o │ └── 郭茁宁1183710109-lab5.doc ├── 实验6 │ ├── HITICS-Lab6.pptx │ ├── cachelab-handout │ │ ├── Makefile │ │ ├── README │ │ ├── cachelab.c │ │ ├── cachelab.h │ │ ├── csim-ref │ │ ├── csim.c │ │ ├── driver.py │ │ ├── test-csim │ │ ├── test-trans.c │ │ ├── tracegen.c │ │ ├── traces │ │ │ ├── dave.trace │ │ │ ├── long.trace │ │ │ ├── trans.trace │ │ │ ├── yi.trace │ │ │ └── yi2.trace │ │ └── trans.c │ ├── csim.c │ ├── trans.c │ ├── ~$TICS-lab6实验报告模板.doc │ ├── ~WRL0001.tmp │ └── 郭茁宁1183710109Lab6.doc ├── 实验7 │ ├── 1183710109-郭茁宁-实验7.doc │ ├── HITICS-Lab7-shell.pptx │ ├── shlab-handout-hit │ │ ├── Makefile │ │ ├── README │ │ ├── myint │ │ ├── myint.c │ │ ├── myspin │ │ ├── myspin.c │ │ ├── mysplit │ │ ├── mysplit.c │ │ ├── mystop │ │ ├── mystop.c │ │ ├── sdriver.pl │ │ ├── trace01.txt │ │ ├── trace02.txt │ │ ├── trace03.txt │ │ ├── trace04.txt │ │ ├── trace05.txt │ │ ├── trace06.txt │ │ ├── trace07.txt │ │ ├── trace08.txt │ │ ├── trace09.txt │ │ ├── trace10.txt │ │ ├── trace11.txt │ │ ├── trace12.txt │ │ ├── trace13.txt │ │ ├── trace14.txt │ │ ├── trace15.txt │ │ ├── trace16.txt │ │ ├── tsh │ │ ├── tsh.c │ │ ├── tsh0.c │ │ ├── tshref │ │ └── tshref.out │ ├── tsh.c │ ├── tsh_qzs.c │ └── ~WRL0617.tmp └── 实验8 │ ├── 1183710109-郭茁宁-lab8.doc │ ├── HITICS-Lab8 malloc │ ├── README-malloclab.txt │ ├── malloclab-handout │ └── malloclab.pdf │ ├── HITICS-Lab8-malloc.pptx │ ├── mm.c │ └── shlab-handout-hit │ ├── Makefile │ ├── README │ ├── myint │ ├── myint.c │ ├── myspin │ ├── myspin.c │ ├── mysplit │ ├── mysplit.c │ ├── mystop │ ├── mystop.c │ ├── sdriver.pl │ ├── trace01.txt │ ├── trace02.txt │ ├── trace03.txt │ ├── trace04.txt │ ├── trace05.txt │ ├── trace06.txt │ ├── trace07.txt │ ├── trace08.txt │ ├── trace09.txt │ ├── trace10.txt │ ├── trace11.txt │ ├── trace12.txt │ ├── trace13.txt │ ├── trace14.txt │ ├── trace15.txt │ ├── trace16.txt │ ├── tsh │ ├── tsh.c │ ├── tsh0.c │ ├── tshref │ └── tshref.out ├── PPTs ├── HIT-ICS第10章 系统级IO.ppt ├── HIT-ICS第1章 课程概述.pptx ├── HIT-ICS第2章 I :信息的表示和处理Ⅰ:位、整数.pptx ├── HIT-ICS第2章 Ⅱ:浮点数yc.pptx ├── HIT-ICS第3章 程序的机器级表示Ⅰ:基础-part01-s.pptx ├── HIT-ICS第3章 程序的机器级表示Ⅰ:基础-part02.pptx ├── HIT-ICS第3章 程序的机器级表示Ⅰ:基础-part03.pptx ├── HIT-ICS第3章 程序的机器级表示Ⅱ:控制.pptx ├── HIT-ICS第3章 程序的机器级表示Ⅲ:过程.pptx ├── HIT-ICS第3章 程序的机器级表示Ⅳ:数据.pptx ├── HIT-ICS第3章 程序的机器级表示Ⅴ:高级主题.pptx ├── HIT-ICS第4章 处理器体系结构-L1-指令集体系结构.pptx ├── HIT-ICS第4章 处理器体系结构-L2 逻辑设计.pptx ├── HIT-ICS第4章 处理器体系结构-L3-顺序执行的处理器.pptx ├── HIT-ICS第4章 处理器体系结构-L4-流水线的实现基础.pptx ├── HIT-ICS第4章 处理器体系结构-L5-流水线实现的高级技术.pptx ├── HIT-ICS第5章-优化.pptx ├── HIT-ICS第6章- L1 存储器层级结构与局部性.pptx ├── HIT-ICS第6章- L2 高速缓冲器Cache.pptx ├── HIT-ICS第7章- 连接.pptx ├── HIT-ICS第8章 异常控制流 L1异常与进程.pptx ├── HIT-ICS第8章 异常控制流 L2信号与非本地跳转.pptx ├── HIT-ICS第9章 L1虚拟内存-概念.ppt ├── HIT-ICS第9章 L2虚拟内存-系统.pptx ├── HIT-ICS第9章 L2虚拟内存-系统new.pptx ├── HIT-ICS第9章 L3动态内存分配-基本概念.ppt ├── HIT-ICS第9章 L4动态内存分配-高级概念.ppt └── qa2019.pptx ├── README.md ├── 大作业 ├── Disas_hello.s ├── HIT-ICS2019大作业要求.ppt ├── elf.txt ├── hello ├── hello.c ├── hello.i ├── hello.o ├── hello.s ├── hello1.elf ├── hello_objdump.s ├── 郭茁宁-1183710109-HIT-ICS2019大作业报告.docx ├── 郭茁宁-1183710109-HIT-ICS2019大作业报告.md └── 郭茁宁-1183710109-HIT-ICS2019大作业报告.pdf ├── 期末复习QA ├── QA1-8.md ├── QA10.md ├── QA11.md ├── QA12.md ├── QA13.md ├── QA14.md ├── QA15 - 21.md ├── QA22.md ├── QA23.md ├── QA24.md ├── QA25.md ├── QA26.md ├── QA28.md ├── QA9.md └── qa2019.pdf └── 期末考试 ├── ics2017aa .doc ├── ics2018aa.docx └── 计算机系统2019A参考答案 .doc /Homeworks/Homework2/1183710109郭茁宁-Homework1.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Homeworks/Homework2/1183710109郭茁宁-Homework1.docx -------------------------------------------------------------------------------- /Homeworks/Homework2/2_59.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int x=0x89ABCDEF,y=0x76543210; 6 | printf("%02x\n",(x&0x000000FF)|(y&0xFFFFFF00)); 7 | return 0; 8 | } -------------------------------------------------------------------------------- /Homeworks/Homework2/2_63.c: -------------------------------------------------------------------------------- 1 | #include 2 | unsigned srl(unsigned x,int k) 3 | { 4 | unsigned xsra = (int) x >> k; 5 | return (xsra&(~(-1<<(8*sizeof(int)-k)))); 6 | } 7 | int sra(int x,int k) 8 | { 9 | int xsrl = (unsigned) x >> k; 10 | int flag=(xsrl&(1<<(8*sizeof(int)-k-1))); 11 | if (flag) return (xsrl|(-1<<(8*sizeof(int)-k))); else return xsrl; 12 | } 13 | int main() 14 | { 15 | int x1,k; 16 | for(;;) 17 | { 18 | scanf("%x %d",&x1,&k); 19 | unsigned x2; 20 | x2=(unsigned)x1; 21 | printf("逻辑右移:%02x>>%d ->%02x\n",x2,k,srl(x2,k)); 22 | printf("算术右移:%02x>>%d ->%02x\n",x1,k,sra(x1,k)); 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Homeworks/Homework2/2_71.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int Merge(unsigned a,int b) 5 | { 6 | int x=a<<((3-b)<<3); 7 | b=sizeof(int)<<3; 8 | return x>>b; 9 | } 10 | 11 | int main() 12 | { 13 | unsigned a; 14 | int b; 15 | scanf("%x %d",&a,&b); 16 | printf("%x\n",Merge(a,b)); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Homeworks/Homework2/2_75.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int add_out(unsigned x, unsigned y){return x + y >= x;} 5 | 6 | unsigned uhp(unsigned x, unsigned y) 7 | { 8 | int w = sizeof(int)<<3, i; 9 | unsigned high = 0; 10 | unsigned sum = (y&0x1)?x:0; 11 | for(i=1; i>i) & 0x1 ) 14 | { 15 | high += x>>(w-i); 16 | if(!add_out(sum, x< 2 | #include 3 | 4 | int mul3div4(int x) 5 | { 6 | return (x>>2)+(x>>1)+((((x<<1)&0x3)+(x&0x3))>>2); 7 | } 8 | 9 | int main() 10 | { 11 | int x; 12 | scanf("%d",&x); 13 | printf("%d\n",mul3div4(x)); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Homeworks/Homework2/2_95.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | float_bits float_half(float_bits f) 5 | { 6 | unsigned sign=f>>31; 7 | unsigned exp=(f>>23) & 0xff; 8 | unsigned frac=f&0x7fffff; 9 | if( exp == 0xff ) 10 | return f; 11 | else if(exp==0) 12 | return (sign<<31) | ((frac>>1)+(1 & (frac&3==3))); 13 | else if(exp==1) 14 | return (sign<<31) | ((exp-1)<<23) | ( (frac>>1)&0x400000); 15 | else 16 | return (sign<<31) | ((exp-1)<<23) | frac; 17 | } 18 | 19 | int main() 20 | { 21 | float_bits x; 22 | scanf("%f",&x); 23 | printf("%f\n",float_half(x)); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Homeworks/Homework2/show_byte.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef unsigned char* byte_pointer; 4 | 5 | void show_bytes(byte_pointer ch, int len){ 6 | printf("show bytes:"); 7 | int i; 8 | for (i = 0; i < len; i++) 9 | printf ("%.2x ", ch[i]); 10 | printf("\n"); 11 | } 12 | void show_short(short x) 13 | { 14 | show_bytes((byte_pointer) &x,sizeof(short )); 15 | } 16 | void show_long(long x) 17 | { 18 | show_bytes((byte_pointer) &x,sizeof(long )); 19 | } 20 | void show_double(double x) 21 | { 22 | show_bytes((byte_pointer) &x,sizeof(double )); 23 | } 24 | int main() 25 | { 26 | int x = 1; 27 | short y = 32767; 28 | long z = 32767; 29 | double d = 3.14; 30 | printf("linux:\nshow number: %d %d %ld %f\n",x,y,z,d); 31 | show_bytes((byte_pointer) &x,sizeof(int )); 32 | show_short(y); 33 | show_long(z); 34 | show_double(d); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Homeworks/Homework3/郭茁宁-1183710109-Homework3.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Homeworks/Homework3/郭茁宁-1183710109-Homework3.docx -------------------------------------------------------------------------------- /Homeworks/Homework4-5/qzs计算机系统第四章.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Homeworks/Homework4-5/qzs计算机系统第四章.docx -------------------------------------------------------------------------------- /Homeworks/Homework4-5/郭茁宁1183710109-Homework4-5.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Homeworks/Homework4-5/郭茁宁1183710109-Homework4-5.docx -------------------------------------------------------------------------------- /Homeworks/Homework6-7/Homework6-7郭茁宁113710109.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Homeworks/Homework6-7/Homework6-7郭茁宁113710109.docx -------------------------------------------------------------------------------- /Homeworks/Homework8-9/1183710109郭茁宁-Homework8-9.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Homeworks/Homework8-9/1183710109郭茁宁-Homework8-9.docx -------------------------------------------------------------------------------- /Labs/实验1/HITCS-Lab1.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验1/HITCS-Lab1.pptx -------------------------------------------------------------------------------- /Labs/实验1/HITICS-lab1/Datatype.cLinux版/Datatype.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct student1 6 | { 7 | char stunum[11]; 8 | double height; 9 | }; 10 | 11 | union student2 12 | { 13 | char n; 14 | double weight; 15 | }; 16 | 17 | enum g {male, female, others}; 18 | 19 | typedef unsigned char *byte_pointer; 20 | int stuid = 1183710109; 21 | char Firstname = 'G'; 22 | float bir = 20000417; 23 | double ID_d = 350581200004173518; 24 | char *name = &Firstname; 25 | int grade[3] = {97,98,99}; 26 | unsigned int age = 20; 27 | 28 | void show_bytes(byte_pointer data,size_t len) 29 | { 30 | printf(" "); 31 | size_t i; 32 | for(i=0;i 2 | #include 3 | 4 | int main(void) 5 | { 6 | printf("%d\n",sizeof(int *)*8); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /Labs/实验1/HITICS-lab1/hello.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验1/HITICS-lab1/hello.o -------------------------------------------------------------------------------- /Labs/实验1/HITICS-lab1/hello.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验1/HITICS-lab1/hello.out -------------------------------------------------------------------------------- /Labs/实验1/HITICS-lab1/hello.s: -------------------------------------------------------------------------------- 1 | .file "HelloLinux.c" 2 | .text 3 | .section .rodata 4 | .LC0: 5 | .string "Hello 1183710115\346\233\262\346\231\272\345\234\243" 6 | .text 7 | .globl main 8 | .type main, @function 9 | main: 10 | .LFB0: 11 | .cfi_startproc 12 | pushq %rbp 13 | .cfi_def_cfa_offset 16 14 | .cfi_offset 6, -16 15 | movq %rsp, %rbp 16 | .cfi_def_cfa_register 6 17 | leaq .LC0(%rip), %rdi 18 | call puts@PLT 19 | movl $0, %eax 20 | popq %rbp 21 | .cfi_def_cfa 7, 8 22 | ret 23 | .cfi_endproc 24 | .LFE0: 25 | .size main, .-main 26 | .ident "GCC: (Ubuntu 8.3.0-6ubuntu1) 8.3.0" 27 | .section .note.GNU-stack,"",@progbits 28 | -------------------------------------------------------------------------------- /Labs/实验1/HITICS-lab1/hellolinux.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | printf("Hello 1183710109 郭茁宁\n"); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /Labs/实验1/HITICS-lab1/hellowin.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验1/HITICS-lab1/hellowin.c -------------------------------------------------------------------------------- /Labs/实验1/HITICS-lab1/isLittleEndian.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | union code 4 | { 5 | int a; 6 | char c; 7 | }n; 8 | 9 | int isLittleEndian() 10 | { 11 | n.a = 0x4422; 12 | if (n.c==0x22) return 0; 13 | return 1; 14 | } 15 | 16 | int main(void) 17 | { 18 | if (isLittleEndian()) printf("Big endian\n"); 19 | else printf("Little endian\n"); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Labs/实验2/HITICS-Lab2.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验2/HITICS-Lab2.pptx -------------------------------------------------------------------------------- /Labs/实验2/ICS2-郭茁宁/cs_atof.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验2/ICS2-郭茁宁/cs_atof.c -------------------------------------------------------------------------------- /Labs/实验2/ICS2-郭茁宁/cs_atoi.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验2/ICS2-郭茁宁/cs_atoi.c -------------------------------------------------------------------------------- /Labs/实验2/ICS2-郭茁宁/cs_ftoa.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验2/ICS2-郭茁宁/cs_ftoa.c -------------------------------------------------------------------------------- /Labs/实验2/ICS2-郭茁宁/cs_itoa.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验2/ICS2-郭茁宁/cs_itoa.c -------------------------------------------------------------------------------- /Labs/实验2/ICS2-郭茁宁/fib_dg.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int fib_dg1(int x); 4 | long fib_dg2(int x); 5 | unsigned int fib_dg3(int x); 6 | unsigned long fib_dg4(int x); 7 | void fib_int(); 8 | void fib_long(); 9 | void fib_unsignedint(); 10 | void fib_unsignedlong(); 11 | int main(void) 12 | { 13 | int isum, n1, n2, n3, n4, num; 14 | long lsum; 15 | unsigned int uisum; 16 | unsigned long ulsum; 17 | while(1) 18 | { 19 | system ("cls"); 20 | printf(" Select data type \n"); 21 | printf(" 1.int \n"); 22 | printf(" 2.long \n"); 23 | printf(" 3.unsigned int \n"); 24 | printf(" 4.unsigned long \n"); 25 | printf("Input number(input 0 exit):"); 26 | scanf("%d",&num); 27 | if(num==0) 28 | return 0; 29 | switch(num) 30 | { 31 | case 1: fib_int(); 32 | break; 33 | case 2: fib_long(); 34 | break; 35 | case 3: fib_unsignedint(); 36 | break; 37 | case 4: fib_unsignedlong(); 38 | break; 39 | default: printf("Input Error!"); 40 | } 41 | } 42 | } 43 | 44 | void fib_int() 45 | { 46 | int isum, n1; 47 | system("cls"); 48 | printf("int:\n"); 49 | do 50 | { 51 | printf("Input n(input 0 exit):"); 52 | scanf("%d",&n1); 53 | getchar(); 54 | if(n1==0) 55 | break; 56 | isum = fib_dg1(n1); 57 | printf("%d\n",isum); 58 | }while(n1!=0); 59 | system("pause"); 60 | } 61 | 62 | void fib_long() 63 | { 64 | int n2; 65 | long lsum; 66 | system("cls"); 67 | printf("long:\n"); 68 | do 69 | { 70 | printf("Input n(input 0 exit):"); 71 | scanf("%d",&n2); 72 | getchar(); 73 | if(n2==0) 74 | break; 75 | lsum = fib_dg2(n2); 76 | printf("%ld\n",lsum); 77 | }while(n2!=0); 78 | system("pause"); 79 | } 80 | 81 | void fib_unsignedint() 82 | { 83 | int n3; 84 | unsigned int uisum; 85 | system("cls"); 86 | printf("unsigned int:\n"); 87 | do 88 | { 89 | printf("Input n(input 0 exit):"); 90 | scanf("%d",&n3); 91 | getchar(); 92 | if(n3==0) 93 | break; 94 | uisum = fib_dg3(n3); 95 | printf("%u\n",uisum); 96 | }while(n3!=0); 97 | system("pause"); 98 | } 99 | 100 | void fib_unsignedlong() 101 | { 102 | int n4; 103 | unsigned long ulsum; 104 | system("cls"); 105 | printf("unsigned long:\n"); 106 | do 107 | { 108 | printf("Input n(input 0 exit):"); 109 | scanf("%d",&n4); 110 | getchar(); 111 | if(n4==0) 112 | break; 113 | ulsum = fib_dg4(n4); 114 | printf("%lu\n",ulsum); 115 | }while(n4!=0); 116 | system("pause"); 117 | } 118 | 119 | int fib_dg1(int x) 120 | { 121 | if(x==1) 122 | return 0; 123 | else if(x==2) 124 | return 1; 125 | else 126 | return fib_dg1(x-1)+fib_dg1(x-2); 127 | } 128 | 129 | long fib_dg2(int x) 130 | { 131 | if(x==1) 132 | return 0; 133 | else if(x==2) 134 | return 1; 135 | else 136 | return fib_dg1(x-1)+fib_dg1(x-2); 137 | } 138 | 139 | unsigned int fib_dg3(int x) 140 | { 141 | if(x==1) 142 | return 0; 143 | else if(x==2) 144 | return 1; 145 | else 146 | return fib_dg1(x-1)+fib_dg1(x-2); 147 | } 148 | 149 | unsigned long fib_dg4(int x) 150 | { 151 | if(x==1) 152 | return 0; 153 | else if(x==2) 154 | return 1; 155 | else 156 | return fib_dg1(x-1)+fib_dg1(x-2); 157 | } 158 | -------------------------------------------------------------------------------- /Labs/实验2/ICS2-郭茁宁/fib_loop.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void fib_int(); 4 | void fib_long(); 5 | void fib_unsignedint(); 6 | void fib_unsignedlong(); 7 | int main(void) 8 | { 9 | int num; 10 | while(1) 11 | { 12 | system ("cls"); 13 | printf(" Select data type \n"); 14 | printf(" 1.int \n"); 15 | printf(" 2.long \n"); 16 | printf(" 3.unsigned int \n"); 17 | printf(" 4.unsigned long \n"); 18 | printf("Input number(input 0 exit):"); 19 | scanf("%d",&num); 20 | if(num==0) 21 | return 0; 22 | switch(num) 23 | { 24 | case 1: fib_int(); 25 | break; 26 | case 2: fib_long(); 27 | break; 28 | case 3: fib_unsignedint(); 29 | break; 30 | case 4: fib_unsignedlong(); 31 | break; 32 | default: printf("Input error!"); 33 | } 34 | } 35 | } 36 | 37 | void fib_int() 38 | { 39 | int isum, fisum, i, n1, t; 40 | system("cls"); 41 | printf("int\n"); 42 | do 43 | { 44 | printf("Input n(input 0 exit):"); 45 | scanf("%d",&n1); 46 | getchar(); 47 | if(n1==0) 48 | break; 49 | else if(n1==1) 50 | { 51 | isum = 0; 52 | } 53 | else if(n1==2) 54 | { 55 | isum = 1; 56 | } 57 | else 58 | { 59 | fisum = 0; 60 | isum = 1; 61 | for(i=3;i<=n1;i++) 62 | { 63 | t = isum; 64 | isum = isum + fisum; 65 | fisum = t; 66 | } 67 | } 68 | printf("%d\n",isum); 69 | }while(n1!=0); 70 | system("pause"); 71 | } 72 | 73 | void fib_long() 74 | { 75 | int n2, i; 76 | long flsum, lsum, t; 77 | system("cls"); 78 | printf("long:\n"); 79 | do 80 | { 81 | printf("Input n(input 0 exit):"); 82 | scanf("%d",&n2); 83 | getchar(); 84 | if(n2==0) 85 | break; 86 | else if(n2==1) 87 | { 88 | lsum = 0; 89 | } 90 | else if(n2==2) 91 | { 92 | lsum = 1; 93 | } 94 | else 95 | { 96 | flsum = 0; 97 | lsum = 1; 98 | for(i=3;i<=n2;i++) 99 | { 100 | t = lsum; 101 | lsum = lsum + flsum; 102 | flsum = t; 103 | } 104 | } 105 | printf("%ld\n",lsum); 106 | }while(n2!=0); 107 | system("pause"); 108 | } 109 | 110 | void fib_unsignedint() 111 | { 112 | int i, n3; 113 | unsigned int fuisum, uisum, t; 114 | system("cls"); 115 | printf("unsigned int:\n"); 116 | do 117 | { 118 | printf("Input n(input 0 exit):"); 119 | scanf("%d",&n3); 120 | getchar(); 121 | if(n3==0) 122 | break; 123 | else if(n3==1) 124 | { 125 | uisum = 0; 126 | } 127 | else if(n3==2) 128 | { 129 | uisum = 1; 130 | } 131 | else 132 | { 133 | fuisum = 0; 134 | uisum = 1; 135 | for(i=3;i<=n3;i++) 136 | { 137 | t = uisum; 138 | uisum = uisum + fuisum; 139 | fuisum = t; 140 | } 141 | } 142 | printf("%u\n",uisum); 143 | }while(n3!=0); 144 | system("pause"); 145 | } 146 | 147 | void fib_unsignedlong() 148 | { 149 | int i, n4; 150 | unsigned long fulsum, ulsum, t; 151 | system("cls"); 152 | printf("unsigned long:\n"); 153 | do 154 | { 155 | printf("Input n(input 0 exit):"); 156 | scanf("%d",&n4); 157 | getchar(); 158 | if(n4==0) 159 | break; 160 | else if(n4==1) 161 | { 162 | ulsum = 0; 163 | } 164 | else if(n4==2) 165 | { 166 | ulsum = 1; 167 | } 168 | else 169 | { 170 | fulsum = 0; 171 | ulsum = 1; 172 | for(i=3;i<=n4;i++) 173 | { 174 | t = ulsum; 175 | ulsum = ulsum + fulsum; 176 | fulsum = t; 177 | } 178 | } 179 | printf("%lu\n",ulsum); 180 | }while(n4!=0); 181 | system("pause"); 182 | } 183 | 184 | -------------------------------------------------------------------------------- /Labs/实验2/ICS2-郭茁宁/float0.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验2/ICS2-郭茁宁/float0.c -------------------------------------------------------------------------------- /Labs/实验2/ICS2-郭茁宁/floatx.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验2/ICS2-郭茁宁/floatx.c -------------------------------------------------------------------------------- /Labs/实验2/ICS2-郭茁宁/utf8len.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define N 1000 4 | 5 | int utf8len(char* a); 6 | 7 | int main(void) 8 | { 9 | char str[N]; 10 | printf("输入字符串:\n"); 11 | fgets(str,1000,stdin); 12 | printf("字符串个数为:%d",utf8len(str)); 13 | return 0; 14 | } 15 | 16 | int utf8len(char* a) 17 | { 18 | unsigned char* p; 19 | int i=0, j, num=0; 20 | while (a[i]!='\0') i++; 21 | p = (unsigned char*)&a; 22 | for(j=0;j 24 | #include 25 | #include "support.h" 26 | #include "phases.h" 27 | 28 | /* 29 | * Note to self: Remember to erase this file so my victims will have no 30 | * idea what is going on, and so they will all blow up in a 31 | * spectaculary fiendish explosion. -- Dr. Evil 32 | */ 33 | 34 | FILE *infile; 35 | 36 | int main(int argc, char *argv[]) 37 | { 38 | char *input; 39 | 40 | /* Note to self: remember to port this bomb to Windows and put a 41 | * fantastic GUI on it. */ 42 | 43 | /* When run with no arguments, the bomb reads its input lines 44 | * from standard input. */ 45 | if (argc == 1) { 46 | infile = stdin; 47 | } 48 | 49 | /* When run with one argument , the bomb reads from 50 | * until EOF, and then switches to standard input. Thus, as you 51 | * defuse each phase, you can add its defusing string to and 52 | * avoid having to retype it. */ 53 | else if (argc == 2) { 54 | if (!(infile = fopen(argv[1], "r"))) { 55 | printf("%s: Error: Couldn't open %s\n", argv[0], argv[1]); 56 | exit(8); 57 | } 58 | } 59 | 60 | /* You can't call the bomb with more than 1 command line argument. */ 61 | else { 62 | printf("Usage: %s []\n", argv[0]); 63 | exit(8); 64 | } 65 | 66 | /* Do all sorts of secret stuff that makes the bomb harder to defuse. */ 67 | initialize_bomb(); 68 | 69 | printf("Welcome to my fiendish little bomb. You have 6 phases with\n"); 70 | printf("which to blow yourself up. Have a nice day!\n"); 71 | 72 | /* Hmm... Six phases must be more secure than one phase! */ 73 | input = read_line(); /* Get input */ 74 | phase_1(input); /* Run the phase */ 75 | phase_defused(); /* Drat! They figured it out! 76 | * Let me know how they did it. */ 77 | printf("Phase 1 defused. How about the next one?\n"); 78 | 79 | /* The second phase is harder. No one will ever figure out 80 | * how to defuse this... */ 81 | input = read_line(); 82 | phase_2(input); 83 | phase_defused(); 84 | printf("That's number 2. Keep going!\n"); 85 | 86 | /* I guess this is too easy so far. Some more complex code will 87 | * confuse people. */ 88 | input = read_line(); 89 | phase_3(input); 90 | phase_defused(); 91 | printf("Halfway there!\n"); 92 | 93 | /* Oh yeah? Well, how good is your math? Try on this saucy problem! */ 94 | input = read_line(); 95 | phase_4(input); 96 | phase_defused(); 97 | printf("So you got that one. Try this one.\n"); 98 | 99 | /* Round and 'round in memory we go, where we stop, the bomb blows! */ 100 | input = read_line(); 101 | phase_5(input); 102 | phase_defused(); 103 | printf("Good work! On to the next...\n"); 104 | 105 | /* This phase will never be used, since no one will get past the 106 | * earlier ones. But just in case, make this one extra hard. */ 107 | input = read_line(); 108 | phase_6(input); 109 | phase_defused(); 110 | 111 | /* Wow, they got it! But isn't something... missing? Perhaps 112 | * something they overlooked? Mua ha ha ha ha! */ 113 | 114 | return 0; 115 | } 116 | -------------------------------------------------------------------------------- /Labs/实验3/func4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int x;//rdi 5 | 6 | int func4(int d,int s) 7 | { 8 | int a,b; 9 | a=d; 10 | a-=s; 11 | b=a/2+s; 12 | if (b>x) return func4(b-1,s)+b; 13 | else if (b make 9 | 10 | Check the correctness of your simulator: 11 | linux> ./test-csim 12 | 13 | Check the correctness and performance of your transpose functions: 14 | linux> ./test-trans -M 32 -N 32 15 | linux> ./test-trans -M 64 -N 64 16 | linux> ./test-trans -M 61 -N 67 17 | 18 | Check everything at once (this is the program that your instructor runs): 19 | linux> ./driver.py 20 | 21 | ****** 22 | Files: 23 | ****** 24 | 25 | # You will modifying and handing in these two files 26 | csim.c Your cache simulator 27 | trans.c Your transpose function 28 | 29 | # Tools for evaluating your simulator and transpose function 30 | Makefile Builds the simulator and tools 31 | README This file 32 | driver.py* The driver program, runs test-csim and test-trans 33 | cachelab.c Required helper functions 34 | cachelab.h Required header file 35 | csim-ref* The executable reference cache simulator 36 | test-csim* Tests your cache simulator 37 | test-trans.c Tests your transpose function 38 | tracegen.c Helper program used by test-trans 39 | traces/ Trace files used by test-csim.c 40 | -------------------------------------------------------------------------------- /Labs/实验6/cachelab-handout/cachelab.c: -------------------------------------------------------------------------------- 1 | /* 2 | * cachelab.c - Cache Lab helper functions 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include "cachelab.h" 8 | #include 9 | 10 | trans_func_t func_list[MAX_TRANS_FUNCS]; 11 | int func_counter = 0; 12 | 13 | /* 14 | * printSummary - Summarize the cache simulation statistics. Student cache simulators 15 | * must call this function in order to be properly autograded. 16 | */ 17 | void printSummary(int hits, int misses, int evictions) 18 | { 19 | printf("hits:%d misses:%d evictions:%d\n", hits, misses, evictions); 20 | FILE* output_fp = fopen(".csim_results", "w"); 21 | assert(output_fp); 22 | fprintf(output_fp, "%d %d %d\n", hits, misses, evictions); 23 | fclose(output_fp); 24 | } 25 | 26 | /* 27 | * initMatrix - Initialize the given matrix 28 | */ 29 | void initMatrix(int M, int N, int A[N][M], int B[M][N]) 30 | { 31 | int i, j; 32 | srand(time(NULL)); 33 | for (i = 0; i < N; i++){ 34 | for (j = 0; j < M; j++){ 35 | // A[i][j] = i+j; /* The matrix created this way is symmetric */ 36 | A[i][j]=rand(); 37 | B[j][i]=rand(); 38 | } 39 | } 40 | } 41 | 42 | void randMatrix(int M, int N, int A[N][M]) { 43 | int i, j; 44 | srand(time(NULL)); 45 | for (i = 0; i < N; i++){ 46 | for (j = 0; j < M; j++){ 47 | // A[i][j] = i+j; /* The matrix created this way is symmetric */ 48 | A[i][j]=rand(); 49 | } 50 | } 51 | } 52 | 53 | /* 54 | * correctTrans - baseline transpose function used to evaluate correctness 55 | */ 56 | void correctTrans(int M, int N, int A[N][M], int B[M][N]) 57 | { 58 | int i, j, tmp; 59 | for (i = 0; i < N; i++){ 60 | for (j = 0; j < M; j++){ 61 | tmp = A[i][j]; 62 | B[j][i] = tmp; 63 | } 64 | } 65 | } 66 | 67 | 68 | 69 | /* 70 | * registerTransFunction - Add the given trans function into your list 71 | * of functions to be tested 72 | */ 73 | void registerTransFunction(void (*trans)(int M, int N, int[N][M], int[M][N]), 74 | char* desc) 75 | { 76 | func_list[func_counter].func_ptr = trans; 77 | func_list[func_counter].description = desc; 78 | func_list[func_counter].correct = 0; 79 | func_list[func_counter].num_hits = 0; 80 | func_list[func_counter].num_misses = 0; 81 | func_list[func_counter].num_evictions =0; 82 | func_counter++; 83 | } 84 | -------------------------------------------------------------------------------- /Labs/实验6/cachelab-handout/cachelab.h: -------------------------------------------------------------------------------- 1 | /* 2 | * cachelab.h - Prototypes for Cache Lab helper functions 3 | */ 4 | 5 | #ifndef CACHELAB_TOOLS_H 6 | #define CACHELAB_TOOLS_H 7 | 8 | #define MAX_TRANS_FUNCS 100 9 | 10 | typedef struct trans_func{ 11 | void (*func_ptr)(int M,int N,int[N][M],int[M][N]); 12 | char* description; 13 | char correct; 14 | unsigned int num_hits; 15 | unsigned int num_misses; 16 | unsigned int num_evictions; 17 | } trans_func_t; 18 | 19 | /* 20 | * printSummary - This function provides a standard way for your cache 21 | * simulator * to display its final hit and miss statistics 22 | */ 23 | void printSummary(int hits, /* number of hits */ 24 | int misses, /* number of misses */ 25 | int evictions); /* number of evictions */ 26 | 27 | /* Fill the matrix with data */ 28 | void initMatrix(int M, int N, int A[N][M], int B[M][N]); 29 | 30 | /* The baseline trans function that produces correct results. */ 31 | void correctTrans(int M, int N, int A[N][M], int B[M][N]); 32 | 33 | /* Add the given function to the function list */ 34 | void registerTransFunction( 35 | void (*trans)(int M,int N,int[N][M],int[M][N]), char* desc); 36 | 37 | #endif /* CACHELAB_TOOLS_H */ 38 | -------------------------------------------------------------------------------- /Labs/实验6/cachelab-handout/csim-ref: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验6/cachelab-handout/csim-ref -------------------------------------------------------------------------------- /Labs/实验6/cachelab-handout/csim.c: -------------------------------------------------------------------------------- 1 | #include "cachelab.h" 2 | 3 | int main() 4 | { 5 | printSummary(0, 0, 0); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /Labs/实验6/cachelab-handout/driver.py: -------------------------------------------------------------------------------- 1 | #!/usr//bin/python 2 | # 3 | # driver.py - The driver tests the correctness of the student's cache 4 | # simulator and the correctness and performance of their transpose 5 | # function. It uses ./test-csim to check the correctness of the 6 | # simulator and it runs ./test-trans on three different sized 7 | # matrices (32x32, 64x64, and 61x67) to test the correctness and 8 | # performance of the transpose function. 9 | # 10 | import subprocess; 11 | import re; 12 | import os; 13 | import sys; 14 | import optparse; 15 | 16 | # 17 | # computeMissScore - compute the score depending on the number of 18 | # cache misses 19 | # 20 | def computeMissScore(miss, lower, upper, full_score): 21 | if miss <= lower: 22 | return full_score 23 | if miss >= upper: 24 | return 0 25 | 26 | score = (miss - lower) * 1.0 27 | range = (upper- lower) * 1.0 28 | return round((1 - score / range) * full_score, 1) 29 | 30 | # 31 | # main - Main function 32 | # 33 | def main(): 34 | 35 | # Configure maxscores here 36 | maxscore= {}; 37 | maxscore['csim'] = 27 38 | maxscore['transc'] = 1 39 | maxscore['trans32'] = 8 40 | maxscore['trans64'] = 8 41 | maxscore['trans61'] = 10 42 | 43 | # Parse the command line arguments 44 | p = optparse.OptionParser() 45 | p.add_option("-A", action="store_true", dest="autograde", 46 | help="emit autoresult string for Autolab"); 47 | opts, args = p.parse_args() 48 | autograde = opts.autograde 49 | 50 | # Check the correctness of the cache simulator 51 | print "Part A: Testing cache simulator" 52 | print "Running ./test-csim" 53 | p = subprocess.Popen("./test-csim", 54 | shell=True, stdout=subprocess.PIPE) 55 | stdout_data = p.communicate()[0] 56 | 57 | # Emit the output from test-csim 58 | stdout_data = re.split('\n', stdout_data) 59 | for line in stdout_data: 60 | if re.match("TEST_CSIM_RESULTS", line): 61 | resultsim = re.findall(r'(\d+)', line) 62 | else: 63 | print "%s" % (line) 64 | 65 | # Check the correctness and performance of the transpose function 66 | # 32x32 transpose 67 | print "Part B: Testing transpose function" 68 | print "Running ./test-trans -M 32 -N 32" 69 | p = subprocess.Popen("./test-trans -M 32 -N 32 | grep TEST_TRANS_RESULTS", 70 | shell=True, stdout=subprocess.PIPE) 71 | stdout_data = p.communicate()[0] 72 | result32 = re.findall(r'(\d+)', stdout_data) 73 | 74 | # 64x64 transpose 75 | print "Running ./test-trans -M 64 -N 64" 76 | p = subprocess.Popen("./test-trans -M 64 -N 64 | grep TEST_TRANS_RESULTS", 77 | shell=True, stdout=subprocess.PIPE) 78 | stdout_data = p.communicate()[0] 79 | result64 = re.findall(r'(\d+)', stdout_data) 80 | 81 | # 61x67 transpose 82 | print "Running ./test-trans -M 61 -N 67" 83 | p = subprocess.Popen("./test-trans -M 61 -N 67 | grep TEST_TRANS_RESULTS", 84 | shell=True, stdout=subprocess.PIPE) 85 | stdout_data = p.communicate()[0] 86 | result61 = re.findall(r'(\d+)', stdout_data) 87 | 88 | # Compute the scores for each step 89 | csim_cscore = map(int, resultsim[0:1]) 90 | trans_cscore = int(result32[0]) * int(result64[0]) * int(result61[0]); 91 | miss32 = int(result32[1]) 92 | miss64 = int(result64[1]) 93 | miss61 = int(result61[1]) 94 | trans32_score = computeMissScore(miss32, 300, 600, maxscore['trans32']) * int(result32[0]) 95 | trans64_score = computeMissScore(miss64, 1300, 2000, maxscore['trans64']) * int(result64[0]) 96 | trans61_score = computeMissScore(miss61, 2000, 3000, maxscore['trans61']) * int(result61[0]) 97 | total_score = csim_cscore[0] + trans32_score + trans64_score + trans61_score 98 | 99 | # Summarize the results 100 | print "\nCache Lab summary:" 101 | print "%-22s%8s%10s%12s" % ("", "Points", "Max pts", "Misses") 102 | print "%-22s%8.1f%10d" % ("Csim correctness", csim_cscore[0], 103 | maxscore['csim']) 104 | 105 | misses = str(miss32) 106 | if miss32 == 2**31-1 : 107 | misses = "invalid" 108 | print "%-22s%8.1f%10d%12s" % ("Trans perf 32x32", trans32_score, 109 | maxscore['trans32'], misses) 110 | 111 | misses = str(miss64) 112 | if miss64 == 2**31-1 : 113 | misses = "invalid" 114 | print "%-22s%8.1f%10d%12s" % ("Trans perf 64x64", trans64_score, 115 | maxscore['trans64'], misses) 116 | 117 | misses = str(miss61) 118 | if miss61 == 2**31-1 : 119 | misses = "invalid" 120 | print "%-22s%8.1f%10d%12s" % ("Trans perf 61x67", trans61_score, 121 | maxscore['trans61'], misses) 122 | 123 | print "%22s%8.1f%10d" % ("Total points", total_score, 124 | maxscore['csim'] + 125 | maxscore['trans32'] + 126 | maxscore['trans64'] + 127 | maxscore['trans61']) 128 | 129 | # Emit autoresult string for Autolab if called with -A option 130 | if autograde: 131 | autoresult="%.1f:%d:%d:%d" % (total_score, miss32, miss64, miss61) 132 | print "\nAUTORESULT_STRING=%s" % autoresult 133 | 134 | 135 | # execute main only if called as a script 136 | if __name__ == "__main__": 137 | main() 138 | 139 | -------------------------------------------------------------------------------- /Labs/实验6/cachelab-handout/test-csim: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验6/cachelab-handout/test-csim -------------------------------------------------------------------------------- /Labs/实验6/cachelab-handout/test-trans.c: -------------------------------------------------------------------------------- 1 | /* 2 | * test-trans.c - Checks the correctness and performance of all of the 3 | * student's transpose functions and records the results for their 4 | * official submitted version as well. 5 | */ 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "cachelab.h" 15 | #include // fir WEXITSTATUS 16 | #include // for INT_MAX 17 | 18 | /* Maximum array dimension */ 19 | #define MAXN 256 20 | 21 | /* The description string for the transpose_submit() function that the 22 | student submits for credit */ 23 | #define SUBMIT_DESCRIPTION "Transpose submission" 24 | 25 | /* External function defined in trans.c */ 26 | extern void registerFunctions(); 27 | 28 | /* External variables defined in cachelab-tools.c */ 29 | extern trans_func_t func_list[MAX_TRANS_FUNCS]; 30 | extern int func_counter; 31 | 32 | /* Globals set on the command line */ 33 | static int M = 0; 34 | static int N = 0; 35 | 36 | /* The correctness and performance for the submitted transpose function */ 37 | struct results { 38 | int funcid; 39 | int correct; 40 | int misses; 41 | }; 42 | static struct results results = {-1, 0, INT_MAX}; 43 | 44 | /* 45 | * eval_perf - Evaluate the performance of the registered transpose functions 46 | */ 47 | void eval_perf(unsigned int s, unsigned int E, unsigned int b) 48 | { 49 | int i,flag; 50 | unsigned int len, hits, misses, evictions; 51 | unsigned long long int marker_start, marker_end, addr; 52 | char buf[1000], cmd[255]; 53 | char filename[128]; 54 | 55 | registerFunctions(); 56 | 57 | /* Open the complete trace file */ 58 | FILE* full_trace_fp; 59 | FILE* part_trace_fp; 60 | 61 | /* Evaluate the performance of each registered transpose function */ 62 | 63 | for (i=0; i trace.tmp", M, N,i); 72 | flag=WEXITSTATUS(system(cmd)); 73 | if (0!=flag) { 74 | printf("Validation error at function %d! Run ./tracegen -M %d -N %d -F %d for details.\nSkipping performance evaluation for this function.\n",flag-1,M,N,i); 75 | continue; 76 | } 77 | 78 | /* Get the start and end marker addresses */ 79 | FILE* marker_fp = fopen(".marker", "r"); 80 | assert(marker_fp); 81 | fscanf(marker_fp, "%llx %llx", &marker_start, &marker_end); 82 | fclose(marker_fp); 83 | 84 | 85 | func_list[i].correct=1; 86 | 87 | /* Save the correctness of the transpose submission */ 88 | if (results.funcid == i ) { 89 | results.correct = 1; 90 | } 91 | 92 | full_trace_fp = fopen("trace.tmp", "r"); 93 | assert(full_trace_fp); 94 | 95 | 96 | /* Filtered trace for each transpose function goes in a separate file */ 97 | sprintf(filename, "trace.f%d", i); 98 | part_trace_fp = fopen(filename, "w"); 99 | assert(part_trace_fp); 100 | 101 | /* Locate trace corresponding to the trans function */ 102 | flag = 0; 103 | while (fgets(buf, 1000, full_trace_fp) != NULL) { 104 | 105 | /* We are only interested in memory access instructions */ 106 | if (buf[0]==' ' && buf[2]==' ' && 107 | (buf[1]=='S' || buf[1]=='M' || buf[1]=='L' )) { 108 | sscanf(buf+3, "%llx,%u", &addr, &len); 109 | 110 | /* If start marker found, set flag */ 111 | if (addr == marker_start) 112 | flag = 1; 113 | 114 | /* Valgrind creates many spurious accesses to the 115 | stack that have nothing to do with the students 116 | code. At the moment, we are ignoring all stack 117 | accesses by using the simple filter of recording 118 | accesses to only the low 32-bit portion of the 119 | address space. At some point it would be nice to 120 | try to do more informed filtering so that would 121 | eliminate the valgrind stack references while 122 | include the student stack references. */ 123 | if (flag && addr < 0xffffffff) { 124 | fputs(buf, part_trace_fp); 125 | } 126 | 127 | /* if end marker found, close trace file */ 128 | if (addr == marker_end) { 129 | flag = 0; 130 | fclose(part_trace_fp); 131 | break; 132 | } 133 | } 134 | } 135 | fclose(full_trace_fp); 136 | 137 | /* Run the reference simulator */ 138 | printf("Step 2: Evaluating performance (s=%d, E=%d, b=%d)\n", s, E, b); 139 | char cmd[255]; 140 | sprintf(cmd, "./csim-ref -s %u -E %u -b %u -t trace.f%d > /dev/null", 141 | s, E, b, i); 142 | system(cmd); 143 | 144 | /* Collect results from the reference simulator */ 145 | FILE* in_fp = fopen(".csim_results","r"); 146 | assert(in_fp); 147 | fscanf(in_fp, "%u %u %u", &hits, &misses, &evictions); 148 | fclose(in_fp); 149 | func_list[i].num_hits = hits; 150 | func_list[i].num_misses = misses; 151 | func_list[i].num_evictions = evictions; 152 | printf("func %u (%s): hits:%u, misses:%u, evictions:%u\n", 153 | i, func_list[i].description, hits, misses, evictions); 154 | 155 | /* If it is transpose_submit(), record number of misses */ 156 | if (results.funcid == i) { 157 | results.misses = misses; 158 | } 159 | } 160 | 161 | } 162 | 163 | /* 164 | * usage - Print usage info 165 | */ 166 | void usage(char *argv[]){ 167 | printf("Usage: %s [-h] -M -N \n", argv[0]); 168 | printf("Options:\n"); 169 | printf(" -h Print this help message.\n"); 170 | printf(" -M Number of matrix rows (max %d)\n", MAXN); 171 | printf(" -N Number of matrix columns (max %d)\n", MAXN); 172 | printf("Example: %s -M 8 -N 8\n", argv[0]); 173 | } 174 | 175 | /* 176 | * sigsegv_handler - SIGSEGV handler 177 | */ 178 | void sigsegv_handler(int signum){ 179 | printf("Error: Segmentation Fault.\n"); 180 | printf("TEST_TRANS_RESULTS=0:0\n"); 181 | fflush(stdout); 182 | exit(1); 183 | } 184 | 185 | /* 186 | * sigalrm_handler - SIGALRM handler 187 | */ 188 | void sigalrm_handler(int signum){ 189 | printf("Error: Program timed out.\n"); 190 | printf("TEST_TRANS_RESULTS=0:0\n"); 191 | fflush(stdout); 192 | exit(1); 193 | } 194 | 195 | /* 196 | * main - Main routine 197 | */ 198 | int main(int argc, char* argv[]) 199 | { 200 | char c; 201 | 202 | while ((c = getopt(argc,argv,"M:N:h")) != -1) { 203 | switch(c) { 204 | case 'M': 205 | M = atoi(optarg); 206 | break; 207 | case 'N': 208 | N = atoi(optarg); 209 | break; 210 | case 'h': 211 | usage(argv); 212 | exit(0); 213 | default: 214 | usage(argv); 215 | exit(1); 216 | } 217 | } 218 | 219 | if (M == 0 || N == 0) { 220 | printf("Error: Missing required argument\n"); 221 | usage(argv); 222 | exit(1); 223 | } 224 | 225 | if (M > MAXN || N > MAXN) { 226 | printf("Error: M or N exceeds %d\n", MAXN); 227 | usage(argv); 228 | exit(1); 229 | } 230 | 231 | /* Install SIGSEGV and SIGALRM handlers */ 232 | if (signal(SIGSEGV, sigsegv_handler) == SIG_ERR) { 233 | fprintf(stderr, "Unable to install SIGALRM handler\n"); 234 | exit(1); 235 | } 236 | 237 | if (signal(SIGALRM, sigalrm_handler) == SIG_ERR) { 238 | fprintf(stderr, "Unable to install SIGALRM handler\n"); 239 | exit(1); 240 | } 241 | 242 | /* Time out and give up after a while */ 243 | alarm(120); 244 | 245 | /* Check the performance of the student's transpose function */ 246 | eval_perf(5, 1, 5); 247 | 248 | /* Emit the results for this particular test */ 249 | if (results.funcid == -1) { 250 | printf("\nError: We could not find your transpose_submit() function\n"); 251 | printf("Error: Please ensure that description field is exactly \"%s\"\n", 252 | SUBMIT_DESCRIPTION); 253 | printf("\nTEST_TRANS_RESULTS=0:0\n"); 254 | } 255 | else { 256 | printf("\nSummary for official submission (func %d): correctness=%d misses=%d\n", 257 | results.funcid, results.correct, results.misses); 258 | printf("\nTEST_TRANS_RESULTS=%d:%d\n", results.correct, results.misses); 259 | } 260 | return 0; 261 | } 262 | -------------------------------------------------------------------------------- /Labs/实验6/cachelab-handout/tracegen.c: -------------------------------------------------------------------------------- 1 | /* 2 | * tracegen.c - Running the binary tracegen with valgrind produces 3 | * a memory trace of all of the registered transpose functions. 4 | * 5 | * The beginning and end of each registered transpose function's trace 6 | * is indicated by reading from "marker" addresses. These two marker 7 | * addresses are recorded in file for later use. 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "cachelab.h" 16 | #include 17 | 18 | /* External variables declared in cachelab.c */ 19 | extern trans_func_t func_list[MAX_TRANS_FUNCS]; 20 | extern int func_counter; 21 | 22 | /* External function from trans.c */ 23 | extern void registerFunctions(); 24 | 25 | /* Markers used to bound trace regions of interest */ 26 | volatile char MARKER_START, MARKER_END; 27 | 28 | static int A[256][256]; 29 | static int B[256][256]; 30 | static int M; 31 | static int N; 32 | 33 | 34 | int validate(int fn,int M, int N, int A[N][M], int B[M][N]) { 35 | int C[M][N]; 36 | memset(C,0,sizeof(C)); 37 | correctTrans(M,N,A,C); 38 | for(int i=0;i 11 | #include "cachelab.h" 12 | 13 | int is_transpose(int M, int N, int A[N][M], int B[M][N]); 14 | 15 | /* 16 | * transpose_submit - This is the solution transpose function that you 17 | * will be graded on for Part B of the assignment. Do not change 18 | * the description string "Transpose submission", as the driver 19 | * searches for that string to identify the transpose function to 20 | * be graded. 21 | */ 22 | char transpose_submit_desc[] = "Transpose submission"; 23 | void transpose_submit(int M, int N, int A[N][M], int B[M][N]) 24 | { 25 | } 26 | 27 | /* 28 | * You can define additional transpose functions below. We've defined 29 | * a simple one below to help you get started. 30 | */ 31 | 32 | /* 33 | * trans - A simple baseline transpose function, not optimized for the cache. 34 | */ 35 | char trans_desc[] = "Simple row-wise scan transpose"; 36 | void trans(int M, int N, int A[N][M], int B[M][N]) 37 | { 38 | int i, j, tmp; 39 | 40 | for (i = 0; i < N; i++) { 41 | for (j = 0; j < M; j++) { 42 | tmp = A[i][j]; 43 | B[j][i] = tmp; 44 | } 45 | } 46 | 47 | } 48 | 49 | /* 50 | * registerFunctions - This function registers your transpose 51 | * functions with the driver. At runtime, the driver will 52 | * evaluate each of the registered functions and summarize their 53 | * performance. This is a handy way to experiment with different 54 | * transpose strategies. 55 | */ 56 | void registerFunctions() 57 | { 58 | /* Register your solution function */ 59 | registerTransFunction(transpose_submit, transpose_submit_desc); 60 | 61 | /* Register any additional transpose functions */ 62 | registerTransFunction(trans, trans_desc); 63 | 64 | } 65 | 66 | /* 67 | * is_transpose - This helper function checks if B is the transpose of 68 | * A. You can check the correctness of your transpose by calling 69 | * it before returning from the transpose function. 70 | */ 71 | int is_transpose(int M, int N, int A[N][M], int B[M][N]) 72 | { 73 | int i, j; 74 | 75 | for (i = 0; i < N; i++) { 76 | for (j = 0; j < M; ++j) { 77 | if (A[i][j] != B[j][i]) { 78 | return 0; 79 | } 80 | } 81 | } 82 | return 1; 83 | } 84 | 85 | -------------------------------------------------------------------------------- /Labs/实验6/csim.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include "cachelab.h" 11 | 12 | //#define DEBUG_ON 13 | #define ADDRESS_LENGTH 64 14 | 15 | /* Type: Memory address */ 16 | typedef unsigned long long int mem_addr_t; 17 | 18 | /* Type: Cache line 19 | LRU is a counter used to implement LRU replacement policy */ 20 | typedef struct cache_line { 21 | char valid; 22 | mem_addr_t tag; 23 | unsigned long long int lru; 24 | } cache_line_t; 25 | 26 | typedef cache_line_t* cache_set_t; 27 | typedef cache_set_t* cache_t; 28 | 29 | /* Globals set by command line args */ 30 | int verbosity = 0; /* print trace if set */ 31 | int s = 0; /* set index bits */ 32 | int b = 0; /* block offset bits */ 33 | int E = 0; /* associativity */ 34 | char* trace_file = NULL; 35 | 36 | /* Derived from command line args */ 37 | int S; /* number of sets */ 38 | int B; /* block size (bytes) */ 39 | 40 | /* Counters used to record cache statistics */ 41 | int miss_count = 0; 42 | int hit_count = 0; 43 | int eviction_count = 0; 44 | unsigned long long int lru_counter = 1; 45 | 46 | /* The cache we are simulating */ 47 | cache_t cache; 48 | mem_addr_t set_index_mask; 49 | 50 | /* 51 | * initCache - Allocate memory, write 0's for valid and tag and LRU 52 | * also computes the set_index_mask 53 | */ 54 | void initCache() 55 | { 56 | //todo... 57 | int i, j; 58 | cache = (cache_set_t*)malloc(sizeof(cache_set_t)*S); 59 | if (cache == NULL) 60 | printf("Fail to get enough space"); 61 | for (i = 0;i < S;i++) 62 | { 63 | cache[i] = (cache_line_t*)malloc(sizeof(cache_line_t)*E); 64 | if (cache[i] == NULL) 65 | printf("Fail to get enough space"); 66 | 67 | } 68 | for (i = 0;i < S;i++) { 69 | for (j = 0;j < E;j++) { 70 | cache[i][j].tag = 0; 71 | cache[i][j].valid = 0; 72 | cache[i][j].lru = 0; 73 | } 74 | } 75 | set_index_mask = (1 << s) - 1; 76 | } 77 | 78 | 79 | /* 80 | * freeCache - free allocated memory 81 | */ 82 | void freeCache() 83 | { 84 | //todo... 85 | int i; 86 | for (i=0;i> b)&set_index_mask; 106 | mem_addr_t add_tag = addr >> (b + s); 107 | for (i = 0;i < E;i++) { 108 | if ((cache[add_set][i].tag == add_tag) && cache[add_set][i].valid) { 109 | flag = 1; 110 | find = i; 111 | } 112 | } 113 | if (flag) { 114 | hit_count++; 115 | { if(verbosity) printf("hit "); 116 | for (j = 0;j < E;j++) { 117 | cache[add_set][j].lru++; 118 | } 119 | cache[add_set][find].lru = 0; 120 | } 121 | } 122 | if (!flag) { 123 | 124 | if(verbosity) printf("miss "); 125 | miss_count++; 126 | max = cache[add_set][0].lru; 127 | for (i = 0;i < E;i++) { 128 | if (cache[add_set][i].lru >= max) { 129 | max = cache[add_set][i].lru; 130 | find = i; 131 | } 132 | } 133 | 134 | cache[add_set][find].tag = add_tag; 135 | if (cache[add_set][find].valid == 1) { 136 | if(verbosity) printf("eviction "); 137 | eviction_count++; 138 | } 139 | else { 140 | cache[add_set][find].valid = 1; 141 | } 142 | for (j = 0;j < E;j++) { 143 | cache[add_set][j].lru++; 144 | } 145 | cache[add_set][find].lru = 0; 146 | } 147 | } 148 | 149 | 150 | /* 151 | * replayTrace - replays the given trace file against the cache 152 | */ 153 | void replayTrace(char* trace_fn) 154 | { 155 | char buf[1000]; 156 | mem_addr_t addr=0; 157 | unsigned int len=0; 158 | FILE* trace_fp = fopen(trace_fn, "r"); 159 | 160 | if(!trace_fp){ 161 | fprintf(stderr, "%s: %s\n", trace_fn, strerror(errno)); 162 | exit(1); 163 | } 164 | 165 | while( fgets(buf, 1000, trace_fp) != NULL) { 166 | if(buf[1]=='S' || buf[1]=='L' || buf[1]=='M') { 167 | sscanf(buf+3, "%llx,%u", &addr, &len); 168 | 169 | if(verbosity) 170 | printf("%c %llx,%u ", buf[1], addr, len); 171 | 172 | accessData(addr); 173 | 174 | /* If the instruction is R/W then access again */ 175 | if(buf[1]=='M') 176 | accessData(addr); 177 | 178 | if (verbosity) 179 | printf("\n"); 180 | } 181 | } 182 | 183 | fclose(trace_fp); 184 | } 185 | 186 | /* 187 | * printUsage - Print usage info 188 | */ 189 | void printUsage(char* argv[]) 190 | { 191 | printf("Usage: %s [-hv] -s -E -b -t \n", argv[0]); 192 | printf("Options:\n"); 193 | printf(" -h Print this help message.\n"); 194 | printf(" -v Optional verbose flag.\n"); 195 | printf(" -s Number of set index bits.\n"); 196 | printf(" -E Number of lines per set.\n"); 197 | printf(" -b Number of block offset bits.\n"); 198 | printf(" -t Trace file.\n"); 199 | printf("\nExamples:\n"); 200 | printf(" linux> %s -s 4 -E 1 -b 4 -t traces/yi.trace\n", argv[0]); 201 | printf(" linux> %s -v -s 8 -E 2 -b 4 -t traces/yi.trace\n", argv[0]); 202 | exit(0); 203 | } 204 | 205 | /* 206 | * main - Main routine 207 | */ 208 | int main(int argc, char* argv[]) 209 | { 210 | char c; 211 | 212 | while( (c=getopt(argc,argv,"s:E:b:t:vh")) != -1){ 213 | switch(c){ 214 | case 's': 215 | s = atoi(optarg); 216 | break; 217 | case 'E': 218 | E = atoi(optarg); 219 | break; 220 | case 'b': 221 | b = atoi(optarg); 222 | break; 223 | case 't': 224 | trace_file = optarg; 225 | break; 226 | case 'v': 227 | verbosity = 1; 228 | break; 229 | case 'h': 230 | printUsage(argv); 231 | exit(0); 232 | default: 233 | printUsage(argv); 234 | exit(1); 235 | } 236 | } 237 | 238 | /* Make sure that all required command line args were specified */ 239 | if (s == 0 || E == 0 || b == 0 || trace_file == NULL) { 240 | printf("%s: Missing required command line argument\n", argv[0]); 241 | printUsage(argv); 242 | exit(1); 243 | } 244 | 245 | /* Compute S, E and B from command line args */ 246 | S = 1< 11 | #include "cachelab.h" 12 | 13 | int is_transpose(int M, int N, int A[N][M], int B[M][N]); 14 | 15 | /* 16 | * transpose_submit - This is the solution transpose function that you 17 | * will be graded on for Part B of the assignment. Do not change 18 | * the description string "Transpose submission", as the driver 19 | * searches for that string to identify the transpose function to 20 | * be graded. 21 | */ 22 | char transpose_submit_desc[] = "Transpose submission"; 23 | void transpose_submit(int M, int N, int A[N][M], int B[M][N]) 24 | { 25 | int i,j,k,m,t1,t2,t3,t4,t5,t6,t7,t8; 26 | if(M==32){ 27 | for(i=0;i<32;i=i+8){ 28 | for(j=0;j<32;j++){ 29 | t1=A[j][i]; 30 | t2=A[j][i+1]; 31 | t3=A[j][i+2]; 32 | t4=A[j][i+3]; 33 | t5=A[j][i+4]; 34 | t6=A[j][i+5]; 35 | t7=A[j][i+6]; 36 | t8=A[j][i+7]; 37 | B[i][j]=t1; 38 | B[i+1][j]=t2; 39 | B[i+2][j]=t3; 40 | B[i+3][j]=t4; 41 | B[i+4][j]=t5; 42 | B[i+5][j]=t6; 43 | B[i+6][j]=t7; 44 | B[i+7][j]=t8; 45 | } 46 | } 47 | } 48 | if(M==64){ 49 | for ( i = 0; i < 64;i += 8) 50 | { 51 | for ( j = 0; j < 64; j += 8) 52 | { 53 | for ( k = i; k < i + 4; ++k) 54 | { 55 | 56 | t1 = A[k][j]; 57 | t2 = A[k][j+1]; 58 | t3 = A[k][j+2]; 59 | t4 = A[k][j+3]; 60 | t5 = A[k][j+4]; 61 | t6 = A[k][j+5]; 62 | t7 = A[k][j+6]; 63 | t8 = A[k][j+7]; 64 | 65 | B[j][k] = t1; 66 | B[j+1][k] = t2; 67 | B[j+2][k] = t3; 68 | B[j+3][k] = t4; 69 | 70 | B[j][k+4] = t8; 71 | B[j+1][k+4] = t7; 72 | B[j+2][k+4] = t6; 73 | B[j+3][k+4] = t5; 74 | } 75 | for ( m = 0; m < 4; ++m) 76 | { 77 | 78 | t1 = A[i+4][j+3-m]; 79 | t2 = A[i+5][j+3-m]; 80 | t3 = A[i+6][j+3-m]; 81 | t4 = A[i+7][j+3-m]; 82 | t5 = A[i+4][j+4+m]; 83 | t6 = A[i+5][j+4+m]; 84 | t7 = A[i+6][j+4+m]; 85 | t8 = A[i+7][j+4+m]; 86 | 87 | 88 | B[j+4+m][i] = B[j+3-m][i+4]; 89 | B[j+4+m][i+1] = B[j+3-m][i+5]; 90 | B[j+4+m][i+2] = B[j+3-m][i+6]; 91 | B[j+4+m][i+3] = B[j+3-m][i+7]; 92 | 93 | B[j+3-m][i+4] = t1; 94 | B[j+3-m][i+5] = t2; 95 | B[j+3-m][i+6] = t3; 96 | B[j+3-m][i+7] = t4; 97 | B[j+4+m][i+4] = t5; 98 | B[j+4+m][i+5] = t6; 99 | B[j+4+m][i+6] = t7; 100 | B[j+4+m][i+7] = t8; 101 | } 102 | } 103 | } 104 | } 105 | if(M==61){ 106 | for(i=0;i<61;i=i+16){ 107 | for(j=0;j<67;j=j+16){ 108 | for(k=j;k and spins for seconds 19 | mysplit.c # Forks a child that spins for seconds 20 | mystop.c # Spins for seconds and sends SIGTSTP to itself 21 | myint.c # Spins for seconds and sends SIGINT to itself 22 | 23 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/myint: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验7/shlab-handout-hit/myint -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/myint.c: -------------------------------------------------------------------------------- 1 | /* 2 | * myint.c - Another handy routine for testing your tiny shell 3 | * 4 | * usage: myint 5 | * Sleeps for seconds and sends SIGINT to itself. 6 | * 7 | */ 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | int main(int argc, char **argv) 16 | { 17 | int i, secs; 18 | pid_t pid; 19 | 20 | if (argc != 2) { 21 | fprintf(stderr, "Usage: %s \n", argv[0]); 22 | exit(0); 23 | } 24 | secs = atoi(argv[1]); 25 | 26 | for (i=0; i < secs; i++) 27 | sleep(1); 28 | 29 | pid = getpid(); 30 | 31 | if (kill(pid, SIGINT) < 0) 32 | fprintf(stderr, "kill (int) error"); 33 | 34 | exit(0); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/myspin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验7/shlab-handout-hit/myspin -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/myspin.c: -------------------------------------------------------------------------------- 1 | /* 2 | * myspin.c - A handy program for testing your tiny shell 3 | * 4 | * usage: myspin 5 | * Sleeps for seconds in 1-second chunks. 6 | * 7 | */ 8 | #include 9 | #include 10 | #include 11 | 12 | int main(int argc, char **argv) 13 | { 14 | int i, secs; 15 | 16 | if (argc != 2) { 17 | fprintf(stderr, "Usage: %s \n", argv[0]); 18 | exit(0); 19 | } 20 | secs = atoi(argv[1]); 21 | for (i=0; i < secs; i++) 22 | sleep(1); 23 | exit(0); 24 | } 25 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/mysplit: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验7/shlab-handout-hit/mysplit -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/mysplit.c: -------------------------------------------------------------------------------- 1 | /* 2 | * mysplit.c - Another handy routine for testing your tiny shell 3 | * 4 | * usage: mysplit 5 | * Fork a child that spins for seconds in 1-second chunks. 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | int main(int argc, char **argv) 15 | { 16 | int i, secs; 17 | 18 | if (argc != 2) { 19 | fprintf(stderr, "Usage: %s \n", argv[0]); 20 | exit(0); 21 | } 22 | secs = atoi(argv[1]); 23 | 24 | 25 | if (fork() == 0) { /* child */ 26 | for (i=0; i < secs; i++) 27 | sleep(1); 28 | exit(0); 29 | } 30 | 31 | /* parent waits for child to terminate */ 32 | wait(NULL); 33 | 34 | exit(0); 35 | } 36 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/mystop: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验7/shlab-handout-hit/mystop -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/mystop.c: -------------------------------------------------------------------------------- 1 | /* 2 | * mystop.c - Another handy routine for testing your tiny shell 3 | * 4 | * usage: mystop 5 | * Sleeps for seconds and sends SIGTSTP to itself. 6 | * 7 | */ 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | int main(int argc, char **argv) 16 | { 17 | int i, secs; 18 | pid_t pid; 19 | 20 | if (argc != 2) { 21 | fprintf(stderr, "Usage: %s \n", argv[0]); 22 | exit(0); 23 | } 24 | secs = atoi(argv[1]); 25 | 26 | for (i=0; i < secs; i++) 27 | sleep(1); 28 | 29 | pid = getpid(); 30 | 31 | if (kill(-pid, SIGTSTP) < 0) 32 | fprintf(stderr, "kill (tstp) error"); 33 | 34 | exit(0); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/sdriver.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | #!/usr/local/bin/perl 3 | use Getopt::Std; 4 | use FileHandle; 5 | use IPC::Open2; 6 | 7 | ####################################################################### 8 | # sdriver.pl - Shell driver 9 | # 10 | # Copyright (c) 2002, R. Bryant and D. O'Hallaron, All rights reserved. 11 | # May not be used, modified, or copied without permission. 12 | # 13 | # The driver runs a student's shell program as a child, sends 14 | # commands and signals to the child as directed by a trace file, 15 | # and captures and displays the output produced by the child. 16 | # 17 | # Tracefile format: 18 | # 19 | # The tracefile consists of text lines that are either blank lines, 20 | # comment lines, driver commands, or shell commands. Blank lines are 21 | # ignored. Comment lines begin with "#" and are echo'd without change 22 | # to stdout. Driver commands are intepreted by the driver and are not 23 | # passed to the child shell. All other lines are shell commands and 24 | # are passed without modification to the shell, which reads them on 25 | # stdin. Output produced by the child on stdout/stderr is read by 26 | # the parent and printed on its stdout. 27 | # 28 | # Driver commands: 29 | # TSTP Send a SIGTSTP signal to the child 30 | # INT Send a SIGINT signal to the child 31 | # QUIT Send a SIGQUIT signal to the child 32 | # KILL Send a SIGKILL signal to the child 33 | # CLOSE Close Writer (sends EOF signal to child) 34 | # WAIT Wait() for child to terminate 35 | # SLEEP Sleep for seconds 36 | # 37 | ###################################################################### 38 | 39 | # 40 | # usage - print help message and terminate 41 | # 42 | sub usage 43 | { 44 | printf STDERR "$_[0]\n"; 45 | printf STDERR "Usage: $0 [-hv] -t -s -a \n"; 46 | printf STDERR "Options:\n"; 47 | printf STDERR " -h Print this message\n"; 48 | printf STDERR " -v Be more verbose\n"; 49 | printf STDERR " -t Trace file\n"; 50 | printf STDERR " -s Shell program to test\n"; 51 | printf STDERR " -a Shell arguments\n"; 52 | printf STDERR " -g Generate output for autograder\n"; 53 | die "\n" ; 54 | } 55 | 56 | # Parse the command line arguments 57 | getopts('hgvt:s:a:'); 58 | if ($opt_h) { 59 | usage(); 60 | } 61 | if (!$opt_t) { 62 | usage("Missing required -t argument"); 63 | } 64 | if (!$opt_s) { 65 | usage("Missing required -s argument"); 66 | } 67 | $verbose = $opt_v; 68 | $infile = $opt_t; 69 | $shellprog = $opt_s; 70 | $shellargs = $opt_a; 71 | $grade = $opt_g; 72 | 73 | # Make sure the input script exists and is readable 74 | -e $infile 75 | or die "$0: ERROR: $infile not found\n"; 76 | -r $infile 77 | or die "$0: ERROR: $infile is not readable\n"; 78 | 79 | # Make sure the shell program exists and is executable 80 | -e $shellprog 81 | or die "$0: ERROR: $shellprog not found\n"; 82 | -x $shellprog 83 | or die "$0: ERROR: $shellprog is not executable\n"; 84 | 85 | 86 | # Open the input script 87 | open INFILE, $infile 88 | or die "$0: ERROR: Couldn't open input file $infile: $!\n"; 89 | 90 | # 91 | # Fork a child, run the shell in it, and connect the parent 92 | # and child with a pair of unidirectional pipes: 93 | # parent:Writer -> child:stdin 94 | # child:stdout -> parent:Reader 95 | # 96 | $pid = open2(\*Reader, \*Writer, "$shellprog $shellargs"); 97 | Writer->autoflush(); 98 | 99 | # The autograder will want to know the child shell's pid 100 | if ($grade) { 101 | print ("pid=$pid\n"); 102 | } 103 | 104 | # 105 | # Parent reads a trace file, sends commands to the child shell. 106 | # 107 | while () { 108 | $line = $_; 109 | chomp($line); 110 | 111 | # Comment line 112 | if ($line =~ /^#/) { 113 | print "$line\n"; 114 | } 115 | 116 | # Blank line 117 | elsif ($line =~ /^\s*$/) { 118 | if ($verbose) { 119 | print "$0: Ignoring blank line\n"; 120 | } 121 | } 122 | 123 | # Send SIGTSTP (ctrl-z) 124 | elsif ($line =~ /TSTP/) { 125 | if ($verbose) { 126 | print "$0: Sending SIGTSTP signal to process $pid\n"; 127 | } 128 | kill 'TSTP', $pid; 129 | } 130 | 131 | # Send SIGINT (ctrl-c) 132 | elsif ($line =~ /INT/) { 133 | if ($verbose) { 134 | print "$0: Sending SIGINT signal to process $pid\n"; 135 | } 136 | kill 'INT', $pid; 137 | } 138 | 139 | # Send SIGQUIT (whenever we need graceful termination) 140 | elsif ($line =~ /QUIT/) { 141 | if ($verbose) { 142 | print "$0: Sending SIGQUIT signal to process $pid\n"; 143 | } 144 | kill 'QUIT', $pid; 145 | } 146 | 147 | # Send SIGKILL 148 | elsif ($line =~ /KILL/) { 149 | if ($verbose) { 150 | print "$0: Sending SIGKILL signal to process $pid\n"; 151 | } 152 | kill 'KILL', $pid; 153 | } 154 | 155 | # Close pipe (sends EOF notification to child) 156 | elsif ($line =~ /CLOSE/) { 157 | if ($verbose) { 158 | print "$0: Closing output end of pipe to child $pid\n"; 159 | } 160 | close Writer; 161 | } 162 | 163 | # Wait for child to terminate 164 | elsif ($line =~ /WAIT/) { 165 | if ($verbose) { 166 | print "$0: Waiting for child $pid\n"; 167 | } 168 | wait; 169 | if ($verbose) { 170 | print "$0: Child $pid reaped\n"; 171 | } 172 | } 173 | 174 | # Sleep 175 | elsif ($line =~ /SLEEP (\d+)/) { 176 | if ($verbose) { 177 | print "$0: Sleeping $1 secs\n"; 178 | } 179 | sleep $1; 180 | } 181 | 182 | # Unknown input 183 | else { 184 | if ($verbose) { 185 | print "$0: Sending :$line: to child $pid\n"; 186 | } 187 | print Writer "$line\n"; 188 | } 189 | } 190 | 191 | # 192 | # Parent echoes the output produced by the child. 193 | # 194 | close Writer; 195 | if ($verbose) { 196 | print "$0: Reading data from child $pid\n"; 197 | } 198 | while ($line = ) { 199 | print $line; 200 | } 201 | close Reader; 202 | 203 | # Finally, parent reaps child 204 | wait; 205 | 206 | if ($verbose) { 207 | print "$0: Shell terminated\n"; 208 | } 209 | 210 | exit; 211 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/trace01.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace01.txt - Properly terminate on EOF. 3 | # 4 | CLOSE 5 | WAIT 6 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/trace02.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace02.txt - Process builtin quit command. 3 | # 4 | quit 5 | WAIT 6 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/trace03.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace03.txt - Run a foreground job. 3 | # 4 | /bin/echo tsh> quit 5 | quit 6 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/trace04.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace04.txt - Run a background job. 3 | # 4 | /bin/echo -e tsh> ./myspin 1 \046 5 | ./myspin 1 & 6 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/trace05.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace05.txt - Process jobs builtin command. 3 | # 4 | /bin/echo -e tsh> ./myspin 2 \046 5 | ./myspin 2 & 6 | 7 | /bin/echo -e tsh> ./myspin 3 \046 8 | ./myspin 3 & 9 | 10 | /bin/echo tsh> jobs 11 | jobs 12 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/trace06.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace06.txt - Forward SIGINT to foreground job. 3 | # 4 | /bin/echo -e tsh> ./myspin 4 5 | ./myspin 4 6 | 7 | SLEEP 2 8 | INT 9 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/trace07.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace07.txt - Forward SIGINT only to foreground job. 3 | # 4 | /bin/echo -e tsh> ./myspin 4 \046 5 | ./myspin 4 & 6 | 7 | /bin/echo -e tsh> ./myspin 5 8 | ./myspin 5 9 | 10 | SLEEP 2 11 | INT 12 | 13 | /bin/echo tsh> jobs 14 | jobs 15 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/trace08.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace08.txt - Forward SIGTSTP only to foreground job. 3 | # 4 | /bin/echo -e tsh> ./myspin 4 \046 5 | ./myspin 4 & 6 | 7 | /bin/echo -e tsh> ./myspin 5 8 | ./myspin 5 9 | 10 | SLEEP 2 11 | TSTP 12 | 13 | /bin/echo tsh> jobs 14 | jobs 15 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/trace09.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace09.txt - Process bg builtin command 3 | # 4 | /bin/echo -e tsh> ./myspin 4 \046 5 | ./myspin 4 & 6 | 7 | /bin/echo -e tsh> ./myspin 5 8 | ./myspin 5 9 | 10 | SLEEP 2 11 | TSTP 12 | 13 | /bin/echo tsh> jobs 14 | jobs 15 | 16 | /bin/echo tsh> bg %2 17 | bg %2 18 | 19 | /bin/echo tsh> jobs 20 | jobs 21 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/trace10.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace10.txt - Process fg builtin command. 3 | # 4 | /bin/echo -e tsh> ./myspin 4 \046 5 | ./myspin 4 & 6 | 7 | SLEEP 1 8 | /bin/echo tsh> fg %1 9 | fg %1 10 | 11 | SLEEP 1 12 | TSTP 13 | 14 | /bin/echo tsh> jobs 15 | jobs 16 | 17 | /bin/echo tsh> fg %1 18 | fg %1 19 | 20 | /bin/echo tsh> jobs 21 | jobs 22 | 23 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/trace11.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace11.txt - Forward SIGINT to every process in foreground process group 3 | # 4 | /bin/echo -e tsh> ./mysplit 4 5 | ./mysplit 4 6 | 7 | SLEEP 2 8 | INT 9 | 10 | /bin/echo tsh> /bin/ps a 11 | /bin/ps a 12 | 13 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/trace12.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace12.txt - Forward SIGTSTP to every process in foreground process group 3 | # 4 | /bin/echo -e tsh> ./mysplit 4 5 | ./mysplit 4 6 | 7 | SLEEP 2 8 | TSTP 9 | 10 | /bin/echo tsh> jobs 11 | jobs 12 | 13 | /bin/echo tsh> /bin/ps a 14 | /bin/ps a 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/trace13.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace13.txt - Restart every stopped process in process group 3 | # 4 | /bin/echo -e tsh> ./mysplit 4 5 | ./mysplit 4 6 | 7 | SLEEP 2 8 | TSTP 9 | 10 | /bin/echo tsh> jobs 11 | jobs 12 | 13 | /bin/echo tsh> /bin/ps a 14 | /bin/ps a 15 | 16 | /bin/echo tsh> fg %1 17 | fg %1 18 | 19 | /bin/echo tsh> /bin/ps a 20 | /bin/ps a 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/trace14.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace14.txt - Simple error handling 3 | # 4 | /bin/echo tsh> ./bogus 5 | ./bogus 6 | 7 | /bin/echo -e tsh> ./myspin 4 \046 8 | ./myspin 4 & 9 | 10 | /bin/echo tsh> fg 11 | fg 12 | 13 | /bin/echo tsh> bg 14 | bg 15 | 16 | /bin/echo tsh> fg a 17 | fg a 18 | 19 | /bin/echo tsh> bg a 20 | bg a 21 | 22 | /bin/echo tsh> fg 9999999 23 | fg 9999999 24 | 25 | /bin/echo tsh> bg 9999999 26 | bg 9999999 27 | 28 | /bin/echo tsh> fg %2 29 | fg %2 30 | 31 | /bin/echo tsh> fg %1 32 | fg %1 33 | 34 | SLEEP 2 35 | TSTP 36 | 37 | /bin/echo tsh> bg %2 38 | bg %2 39 | 40 | /bin/echo tsh> bg %1 41 | bg %1 42 | 43 | /bin/echo tsh> jobs 44 | jobs 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/trace15.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace15.txt - Putting it all together 3 | # 4 | 5 | /bin/echo tsh> ./bogus 6 | ./bogus 7 | 8 | /bin/echo tsh> ./myspin 10 9 | ./myspin 10 10 | 11 | SLEEP 2 12 | INT 13 | 14 | /bin/echo -e tsh> ./myspin 3 \046 15 | ./myspin 3 & 16 | 17 | /bin/echo -e tsh> ./myspin 4 \046 18 | ./myspin 4 & 19 | 20 | /bin/echo tsh> jobs 21 | jobs 22 | 23 | /bin/echo tsh> fg %1 24 | fg %1 25 | 26 | SLEEP 2 27 | TSTP 28 | 29 | /bin/echo tsh> jobs 30 | jobs 31 | 32 | /bin/echo tsh> bg %3 33 | bg %3 34 | 35 | /bin/echo tsh> bg %1 36 | bg %1 37 | 38 | /bin/echo tsh> jobs 39 | jobs 40 | 41 | /bin/echo tsh> fg %1 42 | fg %1 43 | 44 | /bin/echo tsh> quit 45 | quit 46 | 47 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/trace16.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace16.txt - Tests whether the shell can handle SIGTSTP and SIGINT 3 | # signals that come from other processes instead of the terminal. 4 | # 5 | 6 | /bin/echo tsh> ./mystop 2 7 | ./mystop 2 8 | 9 | SLEEP 3 10 | 11 | /bin/echo tsh> jobs 12 | jobs 13 | 14 | /bin/echo tsh> ./myint 2 15 | ./myint 2 16 | 17 | -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/tsh: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验7/shlab-handout-hit/tsh -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/tshref: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验7/shlab-handout-hit/tshref -------------------------------------------------------------------------------- /Labs/实验7/shlab-handout-hit/tshref.out: -------------------------------------------------------------------------------- 1 | make[1]: Entering directory `/afs/cs.cmu.edu/project/ics/im/labs/shlab/src' 2 | ./sdriver.pl -t trace01.txt -s ./tsh -a "-p" 3 | # 4 | # trace01.txt - Properly terminate on EOF. 5 | # 6 | ./sdriver.pl -t trace02.txt -s ./tsh -a "-p" 7 | # 8 | # trace02.txt - Process builtin quit command. 9 | # 10 | ./sdriver.pl -t trace03.txt -s ./tsh -a "-p" 11 | # 12 | # trace03.txt - Run a foreground job. 13 | # 14 | tsh> quit 15 | ./sdriver.pl -t trace04.txt -s ./tsh -a "-p" 16 | # 17 | # trace04.txt - Run a background job. 18 | # 19 | tsh> ./myspin 1 & 20 | [1] (26252) ./myspin 1 & 21 | ./sdriver.pl -t trace05.txt -s ./tsh -a "-p" 22 | # 23 | # trace05.txt - Process jobs builtin command. 24 | # 25 | tsh> ./myspin 2 & 26 | [1] (26256) ./myspin 2 & 27 | tsh> ./myspin 3 & 28 | [2] (26258) ./myspin 3 & 29 | tsh> jobs 30 | [1] (26256) Running ./myspin 2 & 31 | [2] (26258) Running ./myspin 3 & 32 | ./sdriver.pl -t trace06.txt -s ./tsh -a "-p" 33 | # 34 | # trace06.txt - Forward SIGINT to foreground job. 35 | # 36 | tsh> ./myspin 4 37 | Job [1] (26263) terminated by signal 2 38 | ./sdriver.pl -t trace07.txt -s ./tsh -a "-p" 39 | # 40 | # trace07.txt - Forward SIGINT only to foreground job. 41 | # 42 | tsh> ./myspin 4 & 43 | [1] (26267) ./myspin 4 & 44 | tsh> ./myspin 5 45 | Job [2] (26269) terminated by signal 2 46 | tsh> jobs 47 | [1] (26267) Running ./myspin 4 & 48 | ./sdriver.pl -t trace08.txt -s ./tsh -a "-p" 49 | # 50 | # trace08.txt - Forward SIGTSTP only to foreground job. 51 | # 52 | tsh> ./myspin 4 & 53 | [1] (26274) ./myspin 4 & 54 | tsh> ./myspin 5 55 | Job [2] (26276) stopped by signal 20 56 | tsh> jobs 57 | [1] (26274) Running ./myspin 4 & 58 | [2] (26276) Stopped ./myspin 5 59 | ./sdriver.pl -t trace09.txt -s ./tsh -a "-p" 60 | # 61 | # trace09.txt - Process bg builtin command 62 | # 63 | tsh> ./myspin 4 & 64 | [1] (26281) ./myspin 4 & 65 | tsh> ./myspin 5 66 | Job [2] (26283) stopped by signal 20 67 | tsh> jobs 68 | [1] (26281) Running ./myspin 4 & 69 | [2] (26283) Stopped ./myspin 5 70 | tsh> bg %2 71 | [2] (26283) ./myspin 5 72 | tsh> jobs 73 | [1] (26281) Running ./myspin 4 & 74 | [2] (26283) Running ./myspin 5 75 | ./sdriver.pl -t trace10.txt -s ./tsh -a "-p" 76 | # 77 | # trace10.txt - Process fg builtin command. 78 | # 79 | tsh> ./myspin 4 & 80 | [1] (26290) ./myspin 4 & 81 | tsh> fg %1 82 | Job [1] (26290) stopped by signal 20 83 | tsh> jobs 84 | [1] (26290) Stopped ./myspin 4 & 85 | tsh> fg %1 86 | tsh> jobs 87 | ./sdriver.pl -t trace11.txt -s ./tsh -a "-p" 88 | # 89 | # trace11.txt - Forward SIGINT to every process in foreground process group 90 | # 91 | tsh> ./mysplit 4 92 | Job [1] (26298) terminated by signal 2 93 | tsh> /bin/ps a 94 | PID TTY STAT TIME COMMAND 95 | 25181 pts/3 S 0:00 -usr/local/bin/tcsh -i 96 | 26239 pts/3 S 0:00 make tshrefout 97 | 26240 pts/3 S 0:00 /bin/sh -c make tests > tshref.out 2>&1 98 | 26241 pts/3 S 0:00 make tests 99 | 26295 pts/3 S 0:00 perl ./sdriver.pl -t trace11.txt -s ./tsh -a -p 100 | 26296 pts/3 S 0:00 ./tsh -p 101 | 26301 pts/3 R 0:00 /bin/ps a 102 | ./sdriver.pl -t trace12.txt -s ./tsh -a "-p" 103 | # 104 | # trace12.txt - Forward SIGTSTP to every process in foreground process group 105 | # 106 | tsh> ./mysplit 4 107 | Job [1] (26305) stopped by signal 20 108 | tsh> jobs 109 | [1] (26305) Stopped ./mysplit 4 110 | tsh> /bin/ps a 111 | PID TTY STAT TIME COMMAND 112 | 25181 pts/3 S 0:00 -usr/local/bin/tcsh -i 113 | 26239 pts/3 S 0:00 make tshrefout 114 | 26240 pts/3 S 0:00 /bin/sh -c make tests > tshref.out 2>&1 115 | 26241 pts/3 S 0:00 make tests 116 | 26302 pts/3 S 0:00 perl ./sdriver.pl -t trace12.txt -s ./tsh -a -p 117 | 26303 pts/3 S 0:00 ./tsh -p 118 | 26305 pts/3 T 0:00 ./mysplit 4 119 | 26306 pts/3 T 0:00 ./mysplit 4 120 | 26309 pts/3 R 0:00 /bin/ps a 121 | ./sdriver.pl -t trace13.txt -s ./tsh -a "-p" 122 | # 123 | # trace13.txt - Restart every stopped process in process group 124 | # 125 | tsh> ./mysplit 4 126 | Job [1] (26313) stopped by signal 20 127 | tsh> jobs 128 | [1] (26313) Stopped ./mysplit 4 129 | tsh> /bin/ps a 130 | PID TTY STAT TIME COMMAND 131 | 25181 pts/3 S 0:00 -usr/local/bin/tcsh -i 132 | 26239 pts/3 S 0:00 make tshrefout 133 | 26240 pts/3 S 0:00 /bin/sh -c make tests > tshref.out 2>&1 134 | 26241 pts/3 S 0:00 make tests 135 | 26310 pts/3 S 0:00 perl ./sdriver.pl -t trace13.txt -s ./tsh -a -p 136 | 26311 pts/3 S 0:00 ./tsh -p 137 | 26313 pts/3 T 0:00 ./mysplit 4 138 | 26314 pts/3 T 0:00 ./mysplit 4 139 | 26317 pts/3 R 0:00 /bin/ps a 140 | tsh> fg %1 141 | tsh> /bin/ps a 142 | PID TTY STAT TIME COMMAND 143 | 25181 pts/3 S 0:00 -usr/local/bin/tcsh -i 144 | 26239 pts/3 S 0:00 make tshrefout 145 | 26240 pts/3 S 0:00 /bin/sh -c make tests > tshref.out 2>&1 146 | 26241 pts/3 S 0:00 make tests 147 | 26310 pts/3 S 0:00 perl ./sdriver.pl -t trace13.txt -s ./tsh -a -p 148 | 26311 pts/3 S 0:00 ./tsh -p 149 | 26320 pts/3 R 0:00 /bin/ps a 150 | ./sdriver.pl -t trace14.txt -s ./tsh -a "-p" 151 | # 152 | # trace14.txt - Simple error handling 153 | # 154 | tsh> ./bogus 155 | ./bogus: Command not found 156 | tsh> ./myspin 4 & 157 | [1] (26326) ./myspin 4 & 158 | tsh> fg 159 | fg command requires PID or %jobid argument 160 | tsh> bg 161 | bg command requires PID or %jobid argument 162 | tsh> fg a 163 | fg: argument must be a PID or %jobid 164 | tsh> bg a 165 | bg: argument must be a PID or %jobid 166 | tsh> fg 9999999 167 | (9999999): No such process 168 | tsh> bg 9999999 169 | (9999999): No such process 170 | tsh> fg %2 171 | %2: No such job 172 | tsh> fg %1 173 | Job [1] (26326) stopped by signal 20 174 | tsh> bg %2 175 | %2: No such job 176 | tsh> bg %1 177 | [1] (26326) ./myspin 4 & 178 | tsh> jobs 179 | [1] (26326) Running ./myspin 4 & 180 | ./sdriver.pl -t trace15.txt -s ./tsh -a "-p" 181 | # 182 | # trace15.txt - Putting it all together 183 | # 184 | tsh> ./bogus 185 | ./bogus: Command not found 186 | tsh> ./myspin 10 187 | Job [1] (26343) terminated by signal 2 188 | tsh> ./myspin 3 & 189 | [1] (26345) ./myspin 3 & 190 | tsh> ./myspin 4 & 191 | [2] (26347) ./myspin 4 & 192 | tsh> jobs 193 | [1] (26345) Running ./myspin 3 & 194 | [2] (26347) Running ./myspin 4 & 195 | tsh> fg %1 196 | Job [1] (26345) stopped by signal 20 197 | tsh> jobs 198 | [1] (26345) Stopped ./myspin 3 & 199 | [2] (26347) Running ./myspin 4 & 200 | tsh> bg %3 201 | %3: No such job 202 | tsh> bg %1 203 | [1] (26345) ./myspin 3 & 204 | tsh> jobs 205 | [1] (26345) Running ./myspin 3 & 206 | [2] (26347) Running ./myspin 4 & 207 | tsh> fg %1 208 | tsh> quit 209 | ./sdriver.pl -t trace16.txt -s ./tsh -a "-p" 210 | # 211 | # trace16.txt - Tests whether the shell can handle SIGTSTP and SIGINT 212 | # signals that come from other processes instead of the terminal. 213 | # 214 | tsh> ./mystop 2 215 | Job [1] (26359) stopped by signal 20 216 | tsh> jobs 217 | [1] (26359) Stopped ./mystop 2 218 | tsh> ./myint 2 219 | Job [2] (26362) terminated by signal 2 220 | make[1]: Leaving directory `/afs/cs.cmu.edu/project/ics/im/labs/shlab/src' 221 | -------------------------------------------------------------------------------- /Labs/实验7/tsh.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验7/tsh.c -------------------------------------------------------------------------------- /Labs/实验7/tsh_qzs.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验7/tsh_qzs.c -------------------------------------------------------------------------------- /Labs/实验7/~WRL0617.tmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验7/~WRL0617.tmp -------------------------------------------------------------------------------- /Labs/实验8/1183710109-郭茁宁-lab8.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验8/1183710109-郭茁宁-lab8.doc -------------------------------------------------------------------------------- /Labs/实验8/HITICS-Lab8 malloc/README-malloclab.txt: -------------------------------------------------------------------------------- 1 | ##################################################################### 2 | # CS:APP Malloc Lab 3 | # Directions to Instructors 4 | # 5 | # Copyright (c) 2002, R. Bryant and D. O'Hallaron, All rights reserved. 6 | # May not be used, modified, or copied without permission. 7 | # 8 | ###################################################################### 9 | 10 | This directory contains the files that you will need to run the CS:APP 11 | malloc lab, which develops a student's understanding of pointers, 12 | system-level programming, and memory managment. 13 | 14 | ************ 15 | 1. Overview 16 | ************ 17 | 18 | In this lab, students write their own storage allocator, in particular 19 | implementations of the malloc, free, and realloc functions. A 20 | trace-driven driver (mdriver) evaluates the quality of the student's 21 | implementation in terms of space utilization and throughput. 22 | 23 | ******** 24 | 2. Files 25 | ******** 26 | 27 | Makefile Makefile that builds the Lab 28 | README This file 29 | grade/ Autograding scripts 30 | malloclab-handout/ The files handed out to the students 31 | src/ The driver sources 32 | traces/ The malloc/free/realloc trace files used by the driver 33 | writeup/ The malloc lab writeup 34 | 35 | ******************* 36 | 3. Building the Lab 37 | ******************* 38 | 39 | Step 1: Configure the driver in src/ for your site. See src/README for 40 | detailed information on the driver. 41 | 42 | Step 2: Modify the Latex writeup in writeup/ to reflect the handout 43 | and handin directions for your site. If you don't use Latex, use your 44 | favorite document preparation system to prepare Postcript and PDF 45 | versions of the writeup in malloclab.ps and malloclab.pdf. 46 | 47 | Step 3: Modify the LABNAME and DEST variables in ./Makefile for your 48 | site. 49 | 50 | Step 4: Type "make" to build the $(LABNAME)-handout.tar file. 51 | 52 | Step 5: Type "make dist" to copy the $(LABNAME)-handout.tar file 53 | and the writeup to the distribution directory where the students 54 | will retrieve the lab. 55 | 56 | ****************** 57 | 4. Grading the Lab 58 | ****************** 59 | 60 | There are autograding scripts for this lab. See grade/README for 61 | details. 62 | -------------------------------------------------------------------------------- /Labs/实验8/HITICS-Lab8 malloc/malloclab-handout: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验8/HITICS-Lab8 malloc/malloclab-handout -------------------------------------------------------------------------------- /Labs/实验8/HITICS-Lab8 malloc/malloclab.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验8/HITICS-Lab8 malloc/malloclab.pdf -------------------------------------------------------------------------------- /Labs/实验8/HITICS-Lab8-malloc.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验8/HITICS-Lab8-malloc.pptx -------------------------------------------------------------------------------- /Labs/实验8/mm.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验8/mm.c -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for the CS:APP Shell Lab 2 | 3 | TEAM = NOBODY 4 | VERSION = 1 5 | HANDINDIR = /afs/cs/academic/class/15213-f02/L5/handin 6 | DRIVER = ./sdriver.pl 7 | TSH = ./tsh 8 | TSHREF = ./tshref 9 | TSHARGS = "-p" 10 | CC = gcc 11 | CFLAGS = -Wall -O2 12 | FILES = $(TSH) ./myspin ./mysplit ./mystop ./myint 13 | 14 | all: $(FILES) 15 | 16 | ################## 17 | # Handin your work 18 | ################## 19 | handin: 20 | cp tsh.c $(HANDINDIR)/$(TEAM)-$(VERSION)-tsh.c 21 | 22 | 23 | ################## 24 | # Regression tests 25 | ################## 26 | 27 | # Run tests using the student's shell program 28 | test01: 29 | $(DRIVER) -t trace01.txt -s $(TSH) -a $(TSHARGS) 30 | test02: 31 | $(DRIVER) -t trace02.txt -s $(TSH) -a $(TSHARGS) 32 | test03: 33 | $(DRIVER) -t trace03.txt -s $(TSH) -a $(TSHARGS) 34 | test04: 35 | $(DRIVER) -t trace04.txt -s $(TSH) -a $(TSHARGS) 36 | test05: 37 | $(DRIVER) -t trace05.txt -s $(TSH) -a $(TSHARGS) 38 | test06: 39 | $(DRIVER) -t trace06.txt -s $(TSH) -a $(TSHARGS) 40 | test07: 41 | $(DRIVER) -t trace07.txt -s $(TSH) -a $(TSHARGS) 42 | test08: 43 | $(DRIVER) -t trace08.txt -s $(TSH) -a $(TSHARGS) 44 | test09: 45 | $(DRIVER) -t trace09.txt -s $(TSH) -a $(TSHARGS) 46 | test10: 47 | $(DRIVER) -t trace10.txt -s $(TSH) -a $(TSHARGS) 48 | test11: 49 | $(DRIVER) -t trace11.txt -s $(TSH) -a $(TSHARGS) 50 | test12: 51 | $(DRIVER) -t trace12.txt -s $(TSH) -a $(TSHARGS) 52 | test13: 53 | $(DRIVER) -t trace13.txt -s $(TSH) -a $(TSHARGS) 54 | test14: 55 | $(DRIVER) -t trace14.txt -s $(TSH) -a $(TSHARGS) 56 | test15: 57 | $(DRIVER) -t trace15.txt -s $(TSH) -a $(TSHARGS) 58 | test16: 59 | $(DRIVER) -t trace16.txt -s $(TSH) -a $(TSHARGS) 60 | 61 | # Run the tests using the reference shell program 62 | rtest01: 63 | $(DRIVER) -t trace01.txt -s $(TSHREF) -a $(TSHARGS) 64 | rtest02: 65 | $(DRIVER) -t trace02.txt -s $(TSHREF) -a $(TSHARGS) 66 | rtest03: 67 | $(DRIVER) -t trace03.txt -s $(TSHREF) -a $(TSHARGS) 68 | rtest04: 69 | $(DRIVER) -t trace04.txt -s $(TSHREF) -a $(TSHARGS) 70 | rtest05: 71 | $(DRIVER) -t trace05.txt -s $(TSHREF) -a $(TSHARGS) 72 | rtest06: 73 | $(DRIVER) -t trace06.txt -s $(TSHREF) -a $(TSHARGS) 74 | rtest07: 75 | $(DRIVER) -t trace07.txt -s $(TSHREF) -a $(TSHARGS) 76 | rtest08: 77 | $(DRIVER) -t trace08.txt -s $(TSHREF) -a $(TSHARGS) 78 | rtest09: 79 | $(DRIVER) -t trace09.txt -s $(TSHREF) -a $(TSHARGS) 80 | rtest10: 81 | $(DRIVER) -t trace10.txt -s $(TSHREF) -a $(TSHARGS) 82 | rtest11: 83 | $(DRIVER) -t trace11.txt -s $(TSHREF) -a $(TSHARGS) 84 | rtest12: 85 | $(DRIVER) -t trace12.txt -s $(TSHREF) -a $(TSHARGS) 86 | rtest13: 87 | $(DRIVER) -t trace13.txt -s $(TSHREF) -a $(TSHARGS) 88 | rtest14: 89 | $(DRIVER) -t trace14.txt -s $(TSHREF) -a $(TSHARGS) 90 | rtest15: 91 | $(DRIVER) -t trace15.txt -s $(TSHREF) -a $(TSHARGS) 92 | rtest16: 93 | $(DRIVER) -t trace16.txt -s $(TSHREF) -a $(TSHARGS) 94 | 95 | 96 | # clean up 97 | clean: 98 | rm -f $(FILES) *.o *~ 99 | 100 | 101 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/README: -------------------------------------------------------------------------------- 1 | ################ 2 | CS:APP Shell Lab 3 | ################ 4 | 5 | Files: 6 | 7 | Makefile # Compiles your shell program and runs the tests 8 | README # This file 9 | tsh.c # The shell program that you will write and hand in 10 | tshref # The reference shell binary. 11 | 12 | # The remaining files are used to test your shell 13 | sdriver.pl # The trace-driven shell driver 14 | trace*.txt # The 15 trace files that control the shell driver 15 | tshref.out # Example output of the reference shell on all 15 traces 16 | 17 | # Little C programs that are called by the trace files 18 | myspin.c # Takes argument and spins for seconds 19 | mysplit.c # Forks a child that spins for seconds 20 | mystop.c # Spins for seconds and sends SIGTSTP to itself 21 | myint.c # Spins for seconds and sends SIGINT to itself 22 | 23 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/myint: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验8/shlab-handout-hit/myint -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/myint.c: -------------------------------------------------------------------------------- 1 | /* 2 | * myint.c - Another handy routine for testing your tiny shell 3 | * 4 | * usage: myint 5 | * Sleeps for seconds and sends SIGINT to itself. 6 | * 7 | */ 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | int main(int argc, char **argv) 16 | { 17 | int i, secs; 18 | pid_t pid; 19 | 20 | if (argc != 2) { 21 | fprintf(stderr, "Usage: %s \n", argv[0]); 22 | exit(0); 23 | } 24 | secs = atoi(argv[1]); 25 | 26 | for (i=0; i < secs; i++) 27 | sleep(1); 28 | 29 | pid = getpid(); 30 | 31 | if (kill(pid, SIGINT) < 0) 32 | fprintf(stderr, "kill (int) error"); 33 | 34 | exit(0); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/myspin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验8/shlab-handout-hit/myspin -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/myspin.c: -------------------------------------------------------------------------------- 1 | /* 2 | * myspin.c - A handy program for testing your tiny shell 3 | * 4 | * usage: myspin 5 | * Sleeps for seconds in 1-second chunks. 6 | * 7 | */ 8 | #include 9 | #include 10 | #include 11 | 12 | int main(int argc, char **argv) 13 | { 14 | int i, secs; 15 | 16 | if (argc != 2) { 17 | fprintf(stderr, "Usage: %s \n", argv[0]); 18 | exit(0); 19 | } 20 | secs = atoi(argv[1]); 21 | for (i=0; i < secs; i++) 22 | sleep(1); 23 | exit(0); 24 | } 25 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/mysplit: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验8/shlab-handout-hit/mysplit -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/mysplit.c: -------------------------------------------------------------------------------- 1 | /* 2 | * mysplit.c - Another handy routine for testing your tiny shell 3 | * 4 | * usage: mysplit 5 | * Fork a child that spins for seconds in 1-second chunks. 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | int main(int argc, char **argv) 15 | { 16 | int i, secs; 17 | 18 | if (argc != 2) { 19 | fprintf(stderr, "Usage: %s \n", argv[0]); 20 | exit(0); 21 | } 22 | secs = atoi(argv[1]); 23 | 24 | 25 | if (fork() == 0) { /* child */ 26 | for (i=0; i < secs; i++) 27 | sleep(1); 28 | exit(0); 29 | } 30 | 31 | /* parent waits for child to terminate */ 32 | wait(NULL); 33 | 34 | exit(0); 35 | } 36 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/mystop: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验8/shlab-handout-hit/mystop -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/mystop.c: -------------------------------------------------------------------------------- 1 | /* 2 | * mystop.c - Another handy routine for testing your tiny shell 3 | * 4 | * usage: mystop 5 | * Sleeps for seconds and sends SIGTSTP to itself. 6 | * 7 | */ 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | int main(int argc, char **argv) 16 | { 17 | int i, secs; 18 | pid_t pid; 19 | 20 | if (argc != 2) { 21 | fprintf(stderr, "Usage: %s \n", argv[0]); 22 | exit(0); 23 | } 24 | secs = atoi(argv[1]); 25 | 26 | for (i=0; i < secs; i++) 27 | sleep(1); 28 | 29 | pid = getpid(); 30 | 31 | if (kill(-pid, SIGTSTP) < 0) 32 | fprintf(stderr, "kill (tstp) error"); 33 | 34 | exit(0); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/sdriver.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | #!/usr/local/bin/perl 3 | use Getopt::Std; 4 | use FileHandle; 5 | use IPC::Open2; 6 | 7 | ####################################################################### 8 | # sdriver.pl - Shell driver 9 | # 10 | # Copyright (c) 2002, R. Bryant and D. O'Hallaron, All rights reserved. 11 | # May not be used, modified, or copied without permission. 12 | # 13 | # The driver runs a student's shell program as a child, sends 14 | # commands and signals to the child as directed by a trace file, 15 | # and captures and displays the output produced by the child. 16 | # 17 | # Tracefile format: 18 | # 19 | # The tracefile consists of text lines that are either blank lines, 20 | # comment lines, driver commands, or shell commands. Blank lines are 21 | # ignored. Comment lines begin with "#" and are echo'd without change 22 | # to stdout. Driver commands are intepreted by the driver and are not 23 | # passed to the child shell. All other lines are shell commands and 24 | # are passed without modification to the shell, which reads them on 25 | # stdin. Output produced by the child on stdout/stderr is read by 26 | # the parent and printed on its stdout. 27 | # 28 | # Driver commands: 29 | # TSTP Send a SIGTSTP signal to the child 30 | # INT Send a SIGINT signal to the child 31 | # QUIT Send a SIGQUIT signal to the child 32 | # KILL Send a SIGKILL signal to the child 33 | # CLOSE Close Writer (sends EOF signal to child) 34 | # WAIT Wait() for child to terminate 35 | # SLEEP Sleep for seconds 36 | # 37 | ###################################################################### 38 | 39 | # 40 | # usage - print help message and terminate 41 | # 42 | sub usage 43 | { 44 | printf STDERR "$_[0]\n"; 45 | printf STDERR "Usage: $0 [-hv] -t -s -a \n"; 46 | printf STDERR "Options:\n"; 47 | printf STDERR " -h Print this message\n"; 48 | printf STDERR " -v Be more verbose\n"; 49 | printf STDERR " -t Trace file\n"; 50 | printf STDERR " -s Shell program to test\n"; 51 | printf STDERR " -a Shell arguments\n"; 52 | printf STDERR " -g Generate output for autograder\n"; 53 | die "\n" ; 54 | } 55 | 56 | # Parse the command line arguments 57 | getopts('hgvt:s:a:'); 58 | if ($opt_h) { 59 | usage(); 60 | } 61 | if (!$opt_t) { 62 | usage("Missing required -t argument"); 63 | } 64 | if (!$opt_s) { 65 | usage("Missing required -s argument"); 66 | } 67 | $verbose = $opt_v; 68 | $infile = $opt_t; 69 | $shellprog = $opt_s; 70 | $shellargs = $opt_a; 71 | $grade = $opt_g; 72 | 73 | # Make sure the input script exists and is readable 74 | -e $infile 75 | or die "$0: ERROR: $infile not found\n"; 76 | -r $infile 77 | or die "$0: ERROR: $infile is not readable\n"; 78 | 79 | # Make sure the shell program exists and is executable 80 | -e $shellprog 81 | or die "$0: ERROR: $shellprog not found\n"; 82 | -x $shellprog 83 | or die "$0: ERROR: $shellprog is not executable\n"; 84 | 85 | 86 | # Open the input script 87 | open INFILE, $infile 88 | or die "$0: ERROR: Couldn't open input file $infile: $!\n"; 89 | 90 | # 91 | # Fork a child, run the shell in it, and connect the parent 92 | # and child with a pair of unidirectional pipes: 93 | # parent:Writer -> child:stdin 94 | # child:stdout -> parent:Reader 95 | # 96 | $pid = open2(\*Reader, \*Writer, "$shellprog $shellargs"); 97 | Writer->autoflush(); 98 | 99 | # The autograder will want to know the child shell's pid 100 | if ($grade) { 101 | print ("pid=$pid\n"); 102 | } 103 | 104 | # 105 | # Parent reads a trace file, sends commands to the child shell. 106 | # 107 | while () { 108 | $line = $_; 109 | chomp($line); 110 | 111 | # Comment line 112 | if ($line =~ /^#/) { 113 | print "$line\n"; 114 | } 115 | 116 | # Blank line 117 | elsif ($line =~ /^\s*$/) { 118 | if ($verbose) { 119 | print "$0: Ignoring blank line\n"; 120 | } 121 | } 122 | 123 | # Send SIGTSTP (ctrl-z) 124 | elsif ($line =~ /TSTP/) { 125 | if ($verbose) { 126 | print "$0: Sending SIGTSTP signal to process $pid\n"; 127 | } 128 | kill 'TSTP', $pid; 129 | } 130 | 131 | # Send SIGINT (ctrl-c) 132 | elsif ($line =~ /INT/) { 133 | if ($verbose) { 134 | print "$0: Sending SIGINT signal to process $pid\n"; 135 | } 136 | kill 'INT', $pid; 137 | } 138 | 139 | # Send SIGQUIT (whenever we need graceful termination) 140 | elsif ($line =~ /QUIT/) { 141 | if ($verbose) { 142 | print "$0: Sending SIGQUIT signal to process $pid\n"; 143 | } 144 | kill 'QUIT', $pid; 145 | } 146 | 147 | # Send SIGKILL 148 | elsif ($line =~ /KILL/) { 149 | if ($verbose) { 150 | print "$0: Sending SIGKILL signal to process $pid\n"; 151 | } 152 | kill 'KILL', $pid; 153 | } 154 | 155 | # Close pipe (sends EOF notification to child) 156 | elsif ($line =~ /CLOSE/) { 157 | if ($verbose) { 158 | print "$0: Closing output end of pipe to child $pid\n"; 159 | } 160 | close Writer; 161 | } 162 | 163 | # Wait for child to terminate 164 | elsif ($line =~ /WAIT/) { 165 | if ($verbose) { 166 | print "$0: Waiting for child $pid\n"; 167 | } 168 | wait; 169 | if ($verbose) { 170 | print "$0: Child $pid reaped\n"; 171 | } 172 | } 173 | 174 | # Sleep 175 | elsif ($line =~ /SLEEP (\d+)/) { 176 | if ($verbose) { 177 | print "$0: Sleeping $1 secs\n"; 178 | } 179 | sleep $1; 180 | } 181 | 182 | # Unknown input 183 | else { 184 | if ($verbose) { 185 | print "$0: Sending :$line: to child $pid\n"; 186 | } 187 | print Writer "$line\n"; 188 | } 189 | } 190 | 191 | # 192 | # Parent echoes the output produced by the child. 193 | # 194 | close Writer; 195 | if ($verbose) { 196 | print "$0: Reading data from child $pid\n"; 197 | } 198 | while ($line = ) { 199 | print $line; 200 | } 201 | close Reader; 202 | 203 | # Finally, parent reaps child 204 | wait; 205 | 206 | if ($verbose) { 207 | print "$0: Shell terminated\n"; 208 | } 209 | 210 | exit; 211 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/trace01.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace01.txt - Properly terminate on EOF. 3 | # 4 | CLOSE 5 | WAIT 6 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/trace02.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace02.txt - Process builtin quit command. 3 | # 4 | quit 5 | WAIT 6 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/trace03.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace03.txt - Run a foreground job. 3 | # 4 | /bin/echo tsh> quit 5 | quit 6 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/trace04.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace04.txt - Run a background job. 3 | # 4 | /bin/echo -e tsh> ./myspin 1 \046 5 | ./myspin 1 & 6 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/trace05.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace05.txt - Process jobs builtin command. 3 | # 4 | /bin/echo -e tsh> ./myspin 2 \046 5 | ./myspin 2 & 6 | 7 | /bin/echo -e tsh> ./myspin 3 \046 8 | ./myspin 3 & 9 | 10 | /bin/echo tsh> jobs 11 | jobs 12 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/trace06.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace06.txt - Forward SIGINT to foreground job. 3 | # 4 | /bin/echo -e tsh> ./myspin 4 5 | ./myspin 4 6 | 7 | SLEEP 2 8 | INT 9 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/trace07.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace07.txt - Forward SIGINT only to foreground job. 3 | # 4 | /bin/echo -e tsh> ./myspin 4 \046 5 | ./myspin 4 & 6 | 7 | /bin/echo -e tsh> ./myspin 5 8 | ./myspin 5 9 | 10 | SLEEP 2 11 | INT 12 | 13 | /bin/echo tsh> jobs 14 | jobs 15 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/trace08.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace08.txt - Forward SIGTSTP only to foreground job. 3 | # 4 | /bin/echo -e tsh> ./myspin 4 \046 5 | ./myspin 4 & 6 | 7 | /bin/echo -e tsh> ./myspin 5 8 | ./myspin 5 9 | 10 | SLEEP 2 11 | TSTP 12 | 13 | /bin/echo tsh> jobs 14 | jobs 15 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/trace09.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace09.txt - Process bg builtin command 3 | # 4 | /bin/echo -e tsh> ./myspin 4 \046 5 | ./myspin 4 & 6 | 7 | /bin/echo -e tsh> ./myspin 5 8 | ./myspin 5 9 | 10 | SLEEP 2 11 | TSTP 12 | 13 | /bin/echo tsh> jobs 14 | jobs 15 | 16 | /bin/echo tsh> bg %2 17 | bg %2 18 | 19 | /bin/echo tsh> jobs 20 | jobs 21 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/trace10.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace10.txt - Process fg builtin command. 3 | # 4 | /bin/echo -e tsh> ./myspin 4 \046 5 | ./myspin 4 & 6 | 7 | SLEEP 1 8 | /bin/echo tsh> fg %1 9 | fg %1 10 | 11 | SLEEP 1 12 | TSTP 13 | 14 | /bin/echo tsh> jobs 15 | jobs 16 | 17 | /bin/echo tsh> fg %1 18 | fg %1 19 | 20 | /bin/echo tsh> jobs 21 | jobs 22 | 23 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/trace11.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace11.txt - Forward SIGINT to every process in foreground process group 3 | # 4 | /bin/echo -e tsh> ./mysplit 4 5 | ./mysplit 4 6 | 7 | SLEEP 2 8 | INT 9 | 10 | /bin/echo tsh> /bin/ps a 11 | /bin/ps a 12 | 13 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/trace12.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace12.txt - Forward SIGTSTP to every process in foreground process group 3 | # 4 | /bin/echo -e tsh> ./mysplit 4 5 | ./mysplit 4 6 | 7 | SLEEP 2 8 | TSTP 9 | 10 | /bin/echo tsh> jobs 11 | jobs 12 | 13 | /bin/echo tsh> /bin/ps a 14 | /bin/ps a 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/trace13.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace13.txt - Restart every stopped process in process group 3 | # 4 | /bin/echo -e tsh> ./mysplit 4 5 | ./mysplit 4 6 | 7 | SLEEP 2 8 | TSTP 9 | 10 | /bin/echo tsh> jobs 11 | jobs 12 | 13 | /bin/echo tsh> /bin/ps a 14 | /bin/ps a 15 | 16 | /bin/echo tsh> fg %1 17 | fg %1 18 | 19 | /bin/echo tsh> /bin/ps a 20 | /bin/ps a 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/trace14.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace14.txt - Simple error handling 3 | # 4 | /bin/echo tsh> ./bogus 5 | ./bogus 6 | 7 | /bin/echo -e tsh> ./myspin 4 \046 8 | ./myspin 4 & 9 | 10 | /bin/echo tsh> fg 11 | fg 12 | 13 | /bin/echo tsh> bg 14 | bg 15 | 16 | /bin/echo tsh> fg a 17 | fg a 18 | 19 | /bin/echo tsh> bg a 20 | bg a 21 | 22 | /bin/echo tsh> fg 9999999 23 | fg 9999999 24 | 25 | /bin/echo tsh> bg 9999999 26 | bg 9999999 27 | 28 | /bin/echo tsh> fg %2 29 | fg %2 30 | 31 | /bin/echo tsh> fg %1 32 | fg %1 33 | 34 | SLEEP 2 35 | TSTP 36 | 37 | /bin/echo tsh> bg %2 38 | bg %2 39 | 40 | /bin/echo tsh> bg %1 41 | bg %1 42 | 43 | /bin/echo tsh> jobs 44 | jobs 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/trace15.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace15.txt - Putting it all together 3 | # 4 | 5 | /bin/echo tsh> ./bogus 6 | ./bogus 7 | 8 | /bin/echo tsh> ./myspin 10 9 | ./myspin 10 10 | 11 | SLEEP 2 12 | INT 13 | 14 | /bin/echo -e tsh> ./myspin 3 \046 15 | ./myspin 3 & 16 | 17 | /bin/echo -e tsh> ./myspin 4 \046 18 | ./myspin 4 & 19 | 20 | /bin/echo tsh> jobs 21 | jobs 22 | 23 | /bin/echo tsh> fg %1 24 | fg %1 25 | 26 | SLEEP 2 27 | TSTP 28 | 29 | /bin/echo tsh> jobs 30 | jobs 31 | 32 | /bin/echo tsh> bg %3 33 | bg %3 34 | 35 | /bin/echo tsh> bg %1 36 | bg %1 37 | 38 | /bin/echo tsh> jobs 39 | jobs 40 | 41 | /bin/echo tsh> fg %1 42 | fg %1 43 | 44 | /bin/echo tsh> quit 45 | quit 46 | 47 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/trace16.txt: -------------------------------------------------------------------------------- 1 | # 2 | # trace16.txt - Tests whether the shell can handle SIGTSTP and SIGINT 3 | # signals that come from other processes instead of the terminal. 4 | # 5 | 6 | /bin/echo tsh> ./mystop 2 7 | ./mystop 2 8 | 9 | SLEEP 3 10 | 11 | /bin/echo tsh> jobs 12 | jobs 13 | 14 | /bin/echo tsh> ./myint 2 15 | ./myint 2 16 | 17 | -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/tsh: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验8/shlab-handout-hit/tsh -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/tshref: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/Labs/实验8/shlab-handout-hit/tshref -------------------------------------------------------------------------------- /Labs/实验8/shlab-handout-hit/tshref.out: -------------------------------------------------------------------------------- 1 | make[1]: Entering directory `/afs/cs.cmu.edu/project/ics/im/labs/shlab/src' 2 | ./sdriver.pl -t trace01.txt -s ./tsh -a "-p" 3 | # 4 | # trace01.txt - Properly terminate on EOF. 5 | # 6 | ./sdriver.pl -t trace02.txt -s ./tsh -a "-p" 7 | # 8 | # trace02.txt - Process builtin quit command. 9 | # 10 | ./sdriver.pl -t trace03.txt -s ./tsh -a "-p" 11 | # 12 | # trace03.txt - Run a foreground job. 13 | # 14 | tsh> quit 15 | ./sdriver.pl -t trace04.txt -s ./tsh -a "-p" 16 | # 17 | # trace04.txt - Run a background job. 18 | # 19 | tsh> ./myspin 1 & 20 | [1] (26252) ./myspin 1 & 21 | ./sdriver.pl -t trace05.txt -s ./tsh -a "-p" 22 | # 23 | # trace05.txt - Process jobs builtin command. 24 | # 25 | tsh> ./myspin 2 & 26 | [1] (26256) ./myspin 2 & 27 | tsh> ./myspin 3 & 28 | [2] (26258) ./myspin 3 & 29 | tsh> jobs 30 | [1] (26256) Running ./myspin 2 & 31 | [2] (26258) Running ./myspin 3 & 32 | ./sdriver.pl -t trace06.txt -s ./tsh -a "-p" 33 | # 34 | # trace06.txt - Forward SIGINT to foreground job. 35 | # 36 | tsh> ./myspin 4 37 | Job [1] (26263) terminated by signal 2 38 | ./sdriver.pl -t trace07.txt -s ./tsh -a "-p" 39 | # 40 | # trace07.txt - Forward SIGINT only to foreground job. 41 | # 42 | tsh> ./myspin 4 & 43 | [1] (26267) ./myspin 4 & 44 | tsh> ./myspin 5 45 | Job [2] (26269) terminated by signal 2 46 | tsh> jobs 47 | [1] (26267) Running ./myspin 4 & 48 | ./sdriver.pl -t trace08.txt -s ./tsh -a "-p" 49 | # 50 | # trace08.txt - Forward SIGTSTP only to foreground job. 51 | # 52 | tsh> ./myspin 4 & 53 | [1] (26274) ./myspin 4 & 54 | tsh> ./myspin 5 55 | Job [2] (26276) stopped by signal 20 56 | tsh> jobs 57 | [1] (26274) Running ./myspin 4 & 58 | [2] (26276) Stopped ./myspin 5 59 | ./sdriver.pl -t trace09.txt -s ./tsh -a "-p" 60 | # 61 | # trace09.txt - Process bg builtin command 62 | # 63 | tsh> ./myspin 4 & 64 | [1] (26281) ./myspin 4 & 65 | tsh> ./myspin 5 66 | Job [2] (26283) stopped by signal 20 67 | tsh> jobs 68 | [1] (26281) Running ./myspin 4 & 69 | [2] (26283) Stopped ./myspin 5 70 | tsh> bg %2 71 | [2] (26283) ./myspin 5 72 | tsh> jobs 73 | [1] (26281) Running ./myspin 4 & 74 | [2] (26283) Running ./myspin 5 75 | ./sdriver.pl -t trace10.txt -s ./tsh -a "-p" 76 | # 77 | # trace10.txt - Process fg builtin command. 78 | # 79 | tsh> ./myspin 4 & 80 | [1] (26290) ./myspin 4 & 81 | tsh> fg %1 82 | Job [1] (26290) stopped by signal 20 83 | tsh> jobs 84 | [1] (26290) Stopped ./myspin 4 & 85 | tsh> fg %1 86 | tsh> jobs 87 | ./sdriver.pl -t trace11.txt -s ./tsh -a "-p" 88 | # 89 | # trace11.txt - Forward SIGINT to every process in foreground process group 90 | # 91 | tsh> ./mysplit 4 92 | Job [1] (26298) terminated by signal 2 93 | tsh> /bin/ps a 94 | PID TTY STAT TIME COMMAND 95 | 25181 pts/3 S 0:00 -usr/local/bin/tcsh -i 96 | 26239 pts/3 S 0:00 make tshrefout 97 | 26240 pts/3 S 0:00 /bin/sh -c make tests > tshref.out 2>&1 98 | 26241 pts/3 S 0:00 make tests 99 | 26295 pts/3 S 0:00 perl ./sdriver.pl -t trace11.txt -s ./tsh -a -p 100 | 26296 pts/3 S 0:00 ./tsh -p 101 | 26301 pts/3 R 0:00 /bin/ps a 102 | ./sdriver.pl -t trace12.txt -s ./tsh -a "-p" 103 | # 104 | # trace12.txt - Forward SIGTSTP to every process in foreground process group 105 | # 106 | tsh> ./mysplit 4 107 | Job [1] (26305) stopped by signal 20 108 | tsh> jobs 109 | [1] (26305) Stopped ./mysplit 4 110 | tsh> /bin/ps a 111 | PID TTY STAT TIME COMMAND 112 | 25181 pts/3 S 0:00 -usr/local/bin/tcsh -i 113 | 26239 pts/3 S 0:00 make tshrefout 114 | 26240 pts/3 S 0:00 /bin/sh -c make tests > tshref.out 2>&1 115 | 26241 pts/3 S 0:00 make tests 116 | 26302 pts/3 S 0:00 perl ./sdriver.pl -t trace12.txt -s ./tsh -a -p 117 | 26303 pts/3 S 0:00 ./tsh -p 118 | 26305 pts/3 T 0:00 ./mysplit 4 119 | 26306 pts/3 T 0:00 ./mysplit 4 120 | 26309 pts/3 R 0:00 /bin/ps a 121 | ./sdriver.pl -t trace13.txt -s ./tsh -a "-p" 122 | # 123 | # trace13.txt - Restart every stopped process in process group 124 | # 125 | tsh> ./mysplit 4 126 | Job [1] (26313) stopped by signal 20 127 | tsh> jobs 128 | [1] (26313) Stopped ./mysplit 4 129 | tsh> /bin/ps a 130 | PID TTY STAT TIME COMMAND 131 | 25181 pts/3 S 0:00 -usr/local/bin/tcsh -i 132 | 26239 pts/3 S 0:00 make tshrefout 133 | 26240 pts/3 S 0:00 /bin/sh -c make tests > tshref.out 2>&1 134 | 26241 pts/3 S 0:00 make tests 135 | 26310 pts/3 S 0:00 perl ./sdriver.pl -t trace13.txt -s ./tsh -a -p 136 | 26311 pts/3 S 0:00 ./tsh -p 137 | 26313 pts/3 T 0:00 ./mysplit 4 138 | 26314 pts/3 T 0:00 ./mysplit 4 139 | 26317 pts/3 R 0:00 /bin/ps a 140 | tsh> fg %1 141 | tsh> /bin/ps a 142 | PID TTY STAT TIME COMMAND 143 | 25181 pts/3 S 0:00 -usr/local/bin/tcsh -i 144 | 26239 pts/3 S 0:00 make tshrefout 145 | 26240 pts/3 S 0:00 /bin/sh -c make tests > tshref.out 2>&1 146 | 26241 pts/3 S 0:00 make tests 147 | 26310 pts/3 S 0:00 perl ./sdriver.pl -t trace13.txt -s ./tsh -a -p 148 | 26311 pts/3 S 0:00 ./tsh -p 149 | 26320 pts/3 R 0:00 /bin/ps a 150 | ./sdriver.pl -t trace14.txt -s ./tsh -a "-p" 151 | # 152 | # trace14.txt - Simple error handling 153 | # 154 | tsh> ./bogus 155 | ./bogus: Command not found 156 | tsh> ./myspin 4 & 157 | [1] (26326) ./myspin 4 & 158 | tsh> fg 159 | fg command requires PID or %jobid argument 160 | tsh> bg 161 | bg command requires PID or %jobid argument 162 | tsh> fg a 163 | fg: argument must be a PID or %jobid 164 | tsh> bg a 165 | bg: argument must be a PID or %jobid 166 | tsh> fg 9999999 167 | (9999999): No such process 168 | tsh> bg 9999999 169 | (9999999): No such process 170 | tsh> fg %2 171 | %2: No such job 172 | tsh> fg %1 173 | Job [1] (26326) stopped by signal 20 174 | tsh> bg %2 175 | %2: No such job 176 | tsh> bg %1 177 | [1] (26326) ./myspin 4 & 178 | tsh> jobs 179 | [1] (26326) Running ./myspin 4 & 180 | ./sdriver.pl -t trace15.txt -s ./tsh -a "-p" 181 | # 182 | # trace15.txt - Putting it all together 183 | # 184 | tsh> ./bogus 185 | ./bogus: Command not found 186 | tsh> ./myspin 10 187 | Job [1] (26343) terminated by signal 2 188 | tsh> ./myspin 3 & 189 | [1] (26345) ./myspin 3 & 190 | tsh> ./myspin 4 & 191 | [2] (26347) ./myspin 4 & 192 | tsh> jobs 193 | [1] (26345) Running ./myspin 3 & 194 | [2] (26347) Running ./myspin 4 & 195 | tsh> fg %1 196 | Job [1] (26345) stopped by signal 20 197 | tsh> jobs 198 | [1] (26345) Stopped ./myspin 3 & 199 | [2] (26347) Running ./myspin 4 & 200 | tsh> bg %3 201 | %3: No such job 202 | tsh> bg %1 203 | [1] (26345) ./myspin 3 & 204 | tsh> jobs 205 | [1] (26345) Running ./myspin 3 & 206 | [2] (26347) Running ./myspin 4 & 207 | tsh> fg %1 208 | tsh> quit 209 | ./sdriver.pl -t trace16.txt -s ./tsh -a "-p" 210 | # 211 | # trace16.txt - Tests whether the shell can handle SIGTSTP and SIGINT 212 | # signals that come from other processes instead of the terminal. 213 | # 214 | tsh> ./mystop 2 215 | Job [1] (26359) stopped by signal 20 216 | tsh> jobs 217 | [1] (26359) Stopped ./mystop 2 218 | tsh> ./myint 2 219 | Job [2] (26362) terminated by signal 2 220 | make[1]: Leaving directory `/afs/cs.cmu.edu/project/ics/im/labs/shlab/src' 221 | -------------------------------------------------------------------------------- /PPTs/HIT-ICS第10章 系统级IO.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第10章 系统级IO.ppt -------------------------------------------------------------------------------- /PPTs/HIT-ICS第1章 课程概述.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第1章 课程概述.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第2章 I :信息的表示和处理Ⅰ:位、整数.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第2章 I :信息的表示和处理Ⅰ:位、整数.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第2章 Ⅱ:浮点数yc.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第2章 Ⅱ:浮点数yc.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第3章 程序的机器级表示Ⅰ:基础-part01-s.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第3章 程序的机器级表示Ⅰ:基础-part01-s.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第3章 程序的机器级表示Ⅰ:基础-part02.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第3章 程序的机器级表示Ⅰ:基础-part02.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第3章 程序的机器级表示Ⅰ:基础-part03.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第3章 程序的机器级表示Ⅰ:基础-part03.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第3章 程序的机器级表示Ⅱ:控制.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第3章 程序的机器级表示Ⅱ:控制.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第3章 程序的机器级表示Ⅲ:过程.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第3章 程序的机器级表示Ⅲ:过程.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第3章 程序的机器级表示Ⅳ:数据.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第3章 程序的机器级表示Ⅳ:数据.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第3章 程序的机器级表示Ⅴ:高级主题.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第3章 程序的机器级表示Ⅴ:高级主题.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第4章 处理器体系结构-L1-指令集体系结构.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第4章 处理器体系结构-L1-指令集体系结构.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第4章 处理器体系结构-L2 逻辑设计.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第4章 处理器体系结构-L2 逻辑设计.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第4章 处理器体系结构-L3-顺序执行的处理器.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第4章 处理器体系结构-L3-顺序执行的处理器.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第4章 处理器体系结构-L4-流水线的实现基础.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第4章 处理器体系结构-L4-流水线的实现基础.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第4章 处理器体系结构-L5-流水线实现的高级技术.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第4章 处理器体系结构-L5-流水线实现的高级技术.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第5章-优化.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第5章-优化.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第6章- L1 存储器层级结构与局部性.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第6章- L1 存储器层级结构与局部性.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第6章- L2 高速缓冲器Cache.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第6章- L2 高速缓冲器Cache.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第7章- 连接.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第7章- 连接.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第8章 异常控制流 L1异常与进程.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第8章 异常控制流 L1异常与进程.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第8章 异常控制流 L2信号与非本地跳转.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第8章 异常控制流 L2信号与非本地跳转.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第9章 L1虚拟内存-概念.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第9章 L1虚拟内存-概念.ppt -------------------------------------------------------------------------------- /PPTs/HIT-ICS第9章 L2虚拟内存-系统.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第9章 L2虚拟内存-系统.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第9章 L2虚拟内存-系统new.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第9章 L2虚拟内存-系统new.pptx -------------------------------------------------------------------------------- /PPTs/HIT-ICS第9章 L3动态内存分配-基本概念.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第9章 L3动态内存分配-基本概念.ppt -------------------------------------------------------------------------------- /PPTs/HIT-ICS第9章 L4动态内存分配-高级概念.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/HIT-ICS第9章 L4动态内存分配-高级概念.ppt -------------------------------------------------------------------------------- /PPTs/qa2019.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/PPTs/qa2019.pptx -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 2019Fall-CSAPP 2 | 3 | #### 哈工大2019秋计算机系统课程 4 | - 取走的点点Star吧 5 | 6 | ## PPTs 7 | - 课堂讲义(史先俊老师授课) 8 | - 书本辅助 9 | 10 | ## Homeworks 11 | - CSAPP书后习题 12 | 13 | ## 笔记总结 14 | - [计算机系统_gzn00417的博客](https://blog.csdn.net/gzn00417/category_9707655.html) 15 | - CSAPP第二~九章OneNote笔记总结 16 | 17 | ## 期末复习QA 18 | - 问答题 19 | 20 | ## Labs 21 | - 8次实验 22 | - CMU翻译+HIT原创 23 | 24 | ## 大作业 25 | - [CSDN博客-HIT计算机系统2019大作业报告](https://blog.csdn.net/gzn00417/article/details/103792824 "HIT计算机系统2019大作业报告") 26 | 27 | #### Power By 郭茁宁 28 | -------------------------------------------------------------------------------- /大作业/Disas_hello.s: -------------------------------------------------------------------------------- 1 | 2 | hello.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000
: 8 | 0: 55 push %rbp 9 | 1: 48 89 e5 mov %rsp,%rbp 10 | 4: 48 83 ec 20 sub $0x20,%rsp 11 | 8: 89 7d ec mov %edi,-0x14(%rbp) 12 | b: 48 89 75 e0 mov %rsi,-0x20(%rbp) 13 | f: 83 7d ec 04 cmpl $0x4,-0x14(%rbp) 14 | 13: 74 16 je 2b 15 | 15: 48 8d 3d 00 00 00 00 lea 0x0(%rip),%rdi # 1c 16 | 18: R_X86_64_PC32 .rodata-0x4 17 | 1c: e8 00 00 00 00 callq 21 18 | 1d: R_X86_64_PLT32 puts-0x4 19 | 21: bf 01 00 00 00 mov $0x1,%edi 20 | 26: e8 00 00 00 00 callq 2b 21 | 27: R_X86_64_PLT32 exit-0x4 22 | 2b: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%rbp) 23 | 32: eb 48 jmp 7c 24 | 34: 48 8b 45 e0 mov -0x20(%rbp),%rax 25 | 38: 48 83 c0 10 add $0x10,%rax 26 | 3c: 48 8b 10 mov (%rax),%rdx 27 | 3f: 48 8b 45 e0 mov -0x20(%rbp),%rax 28 | 43: 48 83 c0 08 add $0x8,%rax 29 | 47: 48 8b 00 mov (%rax),%rax 30 | 4a: 48 89 c6 mov %rax,%rsi 31 | 4d: 48 8d 3d 00 00 00 00 lea 0x0(%rip),%rdi # 54 32 | 50: R_X86_64_PC32 .rodata+0x22 33 | 54: b8 00 00 00 00 mov $0x0,%eax 34 | 59: e8 00 00 00 00 callq 5e 35 | 5a: R_X86_64_PLT32 printf-0x4 36 | 5e: 48 8b 45 e0 mov -0x20(%rbp),%rax 37 | 62: 48 83 c0 18 add $0x18,%rax 38 | 66: 48 8b 00 mov (%rax),%rax 39 | 69: 48 89 c7 mov %rax,%rdi 40 | 6c: e8 00 00 00 00 callq 71 41 | 6d: R_X86_64_PLT32 atoi-0x4 42 | 71: 89 c7 mov %eax,%edi 43 | 73: e8 00 00 00 00 callq 78 44 | 74: R_X86_64_PLT32 sleep-0x4 45 | 78: 83 45 fc 01 addl $0x1,-0x4(%rbp) 46 | 7c: 83 7d fc 07 cmpl $0x7,-0x4(%rbp) 47 | 80: 7e b2 jle 34 48 | 82: e8 00 00 00 00 callq 87 49 | 83: R_X86_64_PLT32 getchar-0x4 50 | 87: b8 00 00 00 00 mov $0x0,%eax 51 | 8c: c9 leaveq 52 | 8d: c3 retq 53 | -------------------------------------------------------------------------------- /大作业/HIT-ICS2019大作业要求.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/大作业/HIT-ICS2019大作业要求.ppt -------------------------------------------------------------------------------- /大作业/elf.txt: -------------------------------------------------------------------------------- 1 | ELF Header: 2 | Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 3 | Class: ELF64 4 | Data: 2's complement, little endian 5 | Version: 1 (current) 6 | OS/ABI: UNIX - System V 7 | ABI Version: 0 8 | Type: REL (Relocatable file) 9 | Machine: Advanced Micro Devices X86-64 10 | Version: 0x1 11 | Entry point address: 0x0 12 | Start of program headers: 0 (bytes into file) 13 | Start of section headers: 1152 (bytes into file) 14 | Flags: 0x0 15 | Size of this header: 64 (bytes) 16 | Size of program headers: 0 (bytes) 17 | Number of program headers: 0 18 | Size of section headers: 64 (bytes) 19 | Number of section headers: 13 20 | Section header string table index: 12 21 | 22 | Section Headers: 23 | [Nr] Name Type Address Offset 24 | Size EntSize Flags Link Info Align 25 | [ 0] NULL 0000000000000000 00000000 26 | 0000000000000000 0000000000000000 0 0 0 27 | [ 1] .text PROGBITS 0000000000000000 00000040 28 | 000000000000008e 0000000000000000 AX 0 0 1 29 | [ 2] .rela.text RELA 0000000000000000 00000340 30 | 00000000000000c0 0000000000000018 I 10 1 8 31 | [ 3] .data PROGBITS 0000000000000000 000000ce 32 | 0000000000000000 0000000000000000 WA 0 0 1 33 | [ 4] .bss NOBITS 0000000000000000 000000ce 34 | 0000000000000000 0000000000000000 WA 0 0 1 35 | [ 5] .rodata PROGBITS 0000000000000000 000000d0 36 | 0000000000000033 0000000000000000 A 0 0 8 37 | [ 6] .comment PROGBITS 0000000000000000 00000103 38 | 0000000000000024 0000000000000001 MS 0 0 1 39 | [ 7] .note.GNU-stack PROGBITS 0000000000000000 00000127 40 | 0000000000000000 0000000000000000 0 0 1 41 | [ 8] .eh_frame PROGBITS 0000000000000000 00000128 42 | 0000000000000038 0000000000000000 A 0 0 8 43 | [ 9] .rela.eh_frame RELA 0000000000000000 00000400 44 | 0000000000000018 0000000000000018 I 10 8 8 45 | [10] .symtab SYMTAB 0000000000000000 00000160 46 | 0000000000000198 0000000000000018 11 9 8 47 | [11] .strtab STRTAB 0000000000000000 000002f8 48 | 0000000000000048 0000000000000000 0 0 1 49 | [12] .shstrtab STRTAB 0000000000000000 00000418 50 | 0000000000000061 0000000000000000 0 0 1 51 | Key to Flags: 52 | W (write), A (alloc), X (execute), M (merge), S (strings), I (info), 53 | L (link order), O (extra OS processing required), G (group), T (TLS), 54 | C (compressed), x (unknown), o (OS specific), E (exclude), 55 | l (large), p (processor specific) 56 | 57 | There are no section groups in this file. 58 | 59 | There are no program headers in this file. 60 | 61 | There is no dynamic section in this file. 62 | 63 | Relocation section '.rela.text' at offset 0x340 contains 8 entries: 64 | Offset Info Type Sym. Value Sym. Name + Addend 65 | 000000000018 000500000002 R_X86_64_PC32 0000000000000000 .rodata - 4 66 | 00000000001d 000b00000004 R_X86_64_PLT32 0000000000000000 puts - 4 67 | 000000000027 000c00000004 R_X86_64_PLT32 0000000000000000 exit - 4 68 | 000000000050 000500000002 R_X86_64_PC32 0000000000000000 .rodata + 22 69 | 00000000005a 000d00000004 R_X86_64_PLT32 0000000000000000 printf - 4 70 | 00000000006d 000e00000004 R_X86_64_PLT32 0000000000000000 atoi - 4 71 | 000000000074 000f00000004 R_X86_64_PLT32 0000000000000000 sleep - 4 72 | 000000000083 001000000004 R_X86_64_PLT32 0000000000000000 getchar - 4 73 | 74 | Relocation section '.rela.eh_frame' at offset 0x400 contains 1 entry: 75 | Offset Info Type Sym. Value Sym. Name + Addend 76 | 000000000020 000200000002 R_X86_64_PC32 0000000000000000 .text + 0 77 | 78 | The decoding of unwind sections for machine type Advanced Micro Devices X86-64 is not currently supported. 79 | 80 | Symbol table '.symtab' contains 17 entries: 81 | Num: Value Size Type Bind Vis Ndx Name 82 | 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND 83 | 1: 0000000000000000 0 FILE LOCAL DEFAULT ABS hello.c 84 | 2: 0000000000000000 0 SECTION LOCAL DEFAULT 1 85 | 3: 0000000000000000 0 SECTION LOCAL DEFAULT 3 86 | 4: 0000000000000000 0 SECTION LOCAL DEFAULT 4 87 | 5: 0000000000000000 0 SECTION LOCAL DEFAULT 5 88 | 6: 0000000000000000 0 SECTION LOCAL DEFAULT 7 89 | 7: 0000000000000000 0 SECTION LOCAL DEFAULT 8 90 | 8: 0000000000000000 0 SECTION LOCAL DEFAULT 6 91 | 9: 0000000000000000 142 FUNC GLOBAL DEFAULT 1 main 92 | 10: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND _GLOBAL_OFFSET_TABLE_ 93 | 11: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND puts 94 | 12: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND exit 95 | 13: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND printf 96 | 14: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND atoi 97 | 15: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND sleep 98 | 16: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND getchar 99 | 100 | No version information found in this file. 101 | -------------------------------------------------------------------------------- /大作业/hello: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/大作业/hello -------------------------------------------------------------------------------- /大作业/hello.c: -------------------------------------------------------------------------------- 1 | // 大作业的 hello.c 程序 2 | // gcc -m64 -Og -no-pie -fno-PIC hello.c -o hello 3 | // 程序运行过程中可以按键盘,如不停乱按,包括回车,Ctrl-Z,Ctrl-C等。 4 | // 可以 运行 ps jobs pstree fg 等命令 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | int main(int argc, char *argv[]) 11 | { 12 | int i; 13 | 14 | if (argc != 4) 15 | { 16 | printf("用法: Hello 1183710109 郭茁宁 秒数!\n"); 17 | exit(1); 18 | } 19 | for (i = 0; i < 8; i++) 20 | { 21 | printf("Hello %s %s\n", argv[1], argv[2]); 22 | sleep(atoi(argv[3])); 23 | } 24 | getchar(); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /大作业/hello.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/大作业/hello.o -------------------------------------------------------------------------------- /大作业/hello.s: -------------------------------------------------------------------------------- 1 | .file "hello.c" 2 | .text 3 | .section .rodata 4 | .align 8 5 | .LC0: 6 | .string "\347\224\250\346\263\225: Hello 1183710109 \351\203\255\350\214\201\345\256\201\357\274\201" 7 | .LC1: 8 | .string "Hello %s %s\n" 9 | .text 10 | .globl main 11 | .type main, @function 12 | main: 13 | .LFB6: 14 | .cfi_startproc 15 | pushq %rbp 16 | .cfi_def_cfa_offset 16 17 | .cfi_offset 6, -16 18 | movq %rsp, %rbp 19 | .cfi_def_cfa_register 6 20 | subq $32, %rsp 21 | movl %edi, -20(%rbp) 22 | movq %rsi, -32(%rbp) 23 | cmpl $4, -20(%rbp) 24 | je .L2 25 | leaq .LC0(%rip), %rdi 26 | call puts@PLT 27 | movl $1, %edi 28 | call exit@PLT 29 | .L2: 30 | movl $0, -4(%rbp) 31 | jmp .L3 32 | .L4: 33 | movq -32(%rbp), %rax 34 | addq $16, %rax 35 | movq (%rax), %rdx 36 | movq -32(%rbp), %rax 37 | addq $8, %rax 38 | movq (%rax), %rax 39 | movq %rax, %rsi 40 | leaq .LC1(%rip), %rdi 41 | movl $0, %eax 42 | call printf@PLT 43 | movq -32(%rbp), %rax 44 | addq $24, %rax 45 | movq (%rax), %rax 46 | movq %rax, %rdi 47 | call atoi@PLT 48 | movl %eax, %edi 49 | call sleep@PLT 50 | addl $1, -4(%rbp) 51 | .L3: 52 | cmpl $7, -4(%rbp) 53 | jle .L4 54 | call getchar@PLT 55 | movl $0, %eax 56 | leave 57 | .cfi_def_cfa 7, 8 58 | ret 59 | .cfi_endproc 60 | .LFE6: 61 | .size main, .-main 62 | .ident "GCC: (Ubuntu 8.3.0-6ubuntu1) 8.3.0" 63 | .section .note.GNU-stack,"",@progbits 64 | -------------------------------------------------------------------------------- /大作业/hello_objdump.s: -------------------------------------------------------------------------------- 1 | 2 | hello: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .init: 6 | 7 | 0000000000401000 <_init>: 8 | 401000: 48 83 ec 08 sub $0x8,%rsp 9 | 401004: 48 8b 05 ed 2f 00 00 mov 0x2fed(%rip),%rax # 403ff8 <__gmon_start__> 10 | 40100b: 48 85 c0 test %rax,%rax 11 | 40100e: 74 02 je 401012 <_init+0x12> 12 | 401010: ff d0 callq *%rax 13 | 401012: 48 83 c4 08 add $0x8,%rsp 14 | 401016: c3 retq 15 | 16 | Disassembly of section .plt: 17 | 18 | 0000000000401020 <.plt>: 19 | 401020: ff 35 e2 2f 00 00 pushq 0x2fe2(%rip) # 404008 <_GLOBAL_OFFSET_TABLE_+0x8> 20 | 401026: ff 25 e4 2f 00 00 jmpq *0x2fe4(%rip) # 404010 <_GLOBAL_OFFSET_TABLE_+0x10> 21 | 40102c: 0f 1f 40 00 nopl 0x0(%rax) 22 | 23 | 0000000000401030 : 24 | 401030: ff 25 e2 2f 00 00 jmpq *0x2fe2(%rip) # 404018 25 | 401036: 68 00 00 00 00 pushq $0x0 26 | 40103b: e9 e0 ff ff ff jmpq 401020 <.plt> 27 | 28 | 0000000000401040 : 29 | 401040: ff 25 da 2f 00 00 jmpq *0x2fda(%rip) # 404020 30 | 401046: 68 01 00 00 00 pushq $0x1 31 | 40104b: e9 d0 ff ff ff jmpq 401020 <.plt> 32 | 33 | 0000000000401050 : 34 | 401050: ff 25 d2 2f 00 00 jmpq *0x2fd2(%rip) # 404028 35 | 401056: 68 02 00 00 00 pushq $0x2 36 | 40105b: e9 c0 ff ff ff jmpq 401020 <.plt> 37 | 38 | 0000000000401060 : 39 | 401060: ff 25 ca 2f 00 00 jmpq *0x2fca(%rip) # 404030 40 | 401066: 68 03 00 00 00 pushq $0x3 41 | 40106b: e9 b0 ff ff ff jmpq 401020 <.plt> 42 | 43 | 0000000000401070 : 44 | 401070: ff 25 c2 2f 00 00 jmpq *0x2fc2(%rip) # 404038 45 | 401076: 68 04 00 00 00 pushq $0x4 46 | 40107b: e9 a0 ff ff ff jmpq 401020 <.plt> 47 | 48 | 0000000000401080 : 49 | 401080: ff 25 ba 2f 00 00 jmpq *0x2fba(%rip) # 404040 50 | 401086: 68 05 00 00 00 pushq $0x5 51 | 40108b: e9 90 ff ff ff jmpq 401020 <.plt> 52 | 53 | Disassembly of section .text: 54 | 55 | 0000000000401090 <_start>: 56 | 401090: 31 ed xor %ebp,%ebp 57 | 401092: 49 89 d1 mov %rdx,%r9 58 | 401095: 5e pop %rsi 59 | 401096: 48 89 e2 mov %rsp,%rdx 60 | 401099: 48 83 e4 f0 and $0xfffffffffffffff0,%rsp 61 | 40109d: 50 push %rax 62 | 40109e: 54 push %rsp 63 | 40109f: 49 c7 c0 b0 11 40 00 mov $0x4011b0,%r8 64 | 4010a6: 48 c7 c1 50 11 40 00 mov $0x401150,%rcx 65 | 4010ad: 48 c7 c7 c1 10 40 00 mov $0x4010c1,%rdi 66 | 4010b4: ff 15 36 2f 00 00 callq *0x2f36(%rip) # 403ff0 <__libc_start_main@GLIBC_2.2.5> 67 | 4010ba: f4 hlt 68 | 4010bb: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) 69 | 70 | 00000000004010c0 <_dl_relocate_static_pie>: 71 | 4010c0: c3 retq 72 | 73 | 00000000004010c1
: 74 | 4010c1: 55 push %rbp 75 | 4010c2: 48 89 e5 mov %rsp,%rbp 76 | 4010c5: 48 83 ec 20 sub $0x20,%rsp 77 | 4010c9: 89 7d ec mov %edi,-0x14(%rbp) 78 | 4010cc: 48 89 75 e0 mov %rsi,-0x20(%rbp) 79 | 4010d0: 83 7d ec 04 cmpl $0x4,-0x14(%rbp) 80 | 4010d4: 74 16 je 4010ec 81 | 4010d6: 48 8d 3d 2b 0f 00 00 lea 0xf2b(%rip),%rdi # 402008 <_IO_stdin_used+0x8> 82 | 4010dd: e8 4e ff ff ff callq 401030 83 | 4010e2: bf 01 00 00 00 mov $0x1,%edi 84 | 4010e7: e8 84 ff ff ff callq 401070 85 | 4010ec: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%rbp) 86 | 4010f3: eb 48 jmp 40113d 87 | 4010f5: 48 8b 45 e0 mov -0x20(%rbp),%rax 88 | 4010f9: 48 83 c0 10 add $0x10,%rax 89 | 4010fd: 48 8b 10 mov (%rax),%rdx 90 | 401100: 48 8b 45 e0 mov -0x20(%rbp),%rax 91 | 401104: 48 83 c0 08 add $0x8,%rax 92 | 401108: 48 8b 00 mov (%rax),%rax 93 | 40110b: 48 89 c6 mov %rax,%rsi 94 | 40110e: 48 8d 3d 19 0f 00 00 lea 0xf19(%rip),%rdi # 40202e <_IO_stdin_used+0x2e> 95 | 401115: b8 00 00 00 00 mov $0x0,%eax 96 | 40111a: e8 21 ff ff ff callq 401040 97 | 40111f: 48 8b 45 e0 mov -0x20(%rbp),%rax 98 | 401123: 48 83 c0 18 add $0x18,%rax 99 | 401127: 48 8b 00 mov (%rax),%rax 100 | 40112a: 48 89 c7 mov %rax,%rdi 101 | 40112d: e8 2e ff ff ff callq 401060 102 | 401132: 89 c7 mov %eax,%edi 103 | 401134: e8 47 ff ff ff callq 401080 104 | 401139: 83 45 fc 01 addl $0x1,-0x4(%rbp) 105 | 40113d: 83 7d fc 07 cmpl $0x7,-0x4(%rbp) 106 | 401141: 7e b2 jle 4010f5 107 | 401143: e8 08 ff ff ff callq 401050 108 | 401148: b8 00 00 00 00 mov $0x0,%eax 109 | 40114d: c9 leaveq 110 | 40114e: c3 retq 111 | 40114f: 90 nop 112 | 113 | 0000000000401150 <__libc_csu_init>: 114 | 401150: 41 57 push %r15 115 | 401152: 49 89 d7 mov %rdx,%r15 116 | 401155: 41 56 push %r14 117 | 401157: 49 89 f6 mov %rsi,%r14 118 | 40115a: 41 55 push %r13 119 | 40115c: 41 89 fd mov %edi,%r13d 120 | 40115f: 41 54 push %r12 121 | 401161: 4c 8d 25 e8 2c 00 00 lea 0x2ce8(%rip),%r12 # 403e50 <_DYNAMIC> 122 | 401168: 55 push %rbp 123 | 401169: 48 8d 2d e0 2c 00 00 lea 0x2ce0(%rip),%rbp # 403e50 <_DYNAMIC> 124 | 401170: 53 push %rbx 125 | 401171: 4c 29 e5 sub %r12,%rbp 126 | 401174: 48 83 ec 08 sub $0x8,%rsp 127 | 401178: e8 83 fe ff ff callq 401000 <_init> 128 | 40117d: 48 c1 fd 03 sar $0x3,%rbp 129 | 401181: 74 1b je 40119e <__libc_csu_init+0x4e> 130 | 401183: 31 db xor %ebx,%ebx 131 | 401185: 0f 1f 00 nopl (%rax) 132 | 401188: 4c 89 fa mov %r15,%rdx 133 | 40118b: 4c 89 f6 mov %r14,%rsi 134 | 40118e: 44 89 ef mov %r13d,%edi 135 | 401191: 41 ff 14 dc callq *(%r12,%rbx,8) 136 | 401195: 48 83 c3 01 add $0x1,%rbx 137 | 401199: 48 39 dd cmp %rbx,%rbp 138 | 40119c: 75 ea jne 401188 <__libc_csu_init+0x38> 139 | 40119e: 48 83 c4 08 add $0x8,%rsp 140 | 4011a2: 5b pop %rbx 141 | 4011a3: 5d pop %rbp 142 | 4011a4: 41 5c pop %r12 143 | 4011a6: 41 5d pop %r13 144 | 4011a8: 41 5e pop %r14 145 | 4011aa: 41 5f pop %r15 146 | 4011ac: c3 retq 147 | 4011ad: 0f 1f 00 nopl (%rax) 148 | 149 | 00000000004011b0 <__libc_csu_fini>: 150 | 4011b0: c3 retq 151 | 152 | Disassembly of section .fini: 153 | 154 | 00000000004011b4 <_fini>: 155 | 4011b4: 48 83 ec 08 sub $0x8,%rsp 156 | 4011b8: 48 83 c4 08 add $0x8,%rsp 157 | 4011bc: c3 retq 158 | -------------------------------------------------------------------------------- /大作业/郭茁宁-1183710109-HIT-ICS2019大作业报告.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/大作业/郭茁宁-1183710109-HIT-ICS2019大作业报告.docx -------------------------------------------------------------------------------- /大作业/郭茁宁-1183710109-HIT-ICS2019大作业报告.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/大作业/郭茁宁-1183710109-HIT-ICS2019大作业报告.pdf -------------------------------------------------------------------------------- /期末复习QA/QA1-8.md: -------------------------------------------------------------------------------- 1 | # QA1 2 | ## 1.简述C、ASM、ML的关系,各自优缺点? ## 3 | 机器语言是二进制数据表示的语言,机器可以直接识别;汇编语言是与机器指令一一对应的助记符,是一种低级语言,经过汇编和链接之后机器可以运行;C语言在低级语言的基础上,采用最接近人类自然语言的单词和符号来表示一组低级语言程序。 4 | 5 | **C语言的优点**:提供了结构化程序设计的环境和工具,设计出来的程序可读性好,可维护性强,可靠性高;抽象程度高,远离机器语言,和计算机硬件关系不大,编程门槛低,无需懂计算机原理和计算机结构;编写出来的程序可移植性好,重用率高 6 | 缺点:相对汇编语言和机器语言来说执行效率不高 7 | 8 | **汇编语言的优点:**能发挥计算机的特定和功能,程序紧凑,资源利用率高 9 | 缺点:代码可读性较差,难以维护;特定的汇编语言和特定的机器语言指令集一一对应,只能针对特定体系结构和处理器进行优化,可移植性差;开发效率低,时间长且单调;编程门槛高,需要了解计算机原理和计算机结构 10 | 11 | **机器语言优点:**能利用机器指令精准的描述算法,编程质量高;所占用储存空间小;执行速度快 12 | 缺点:开发过程细节繁琐,编程门槛高;可读性差,不便于交流;严重依赖具体的计算机,可移植性差,重用性差。 13 | 14 | 15 | ##2.Hello.c 经过那些工具、步骤、生成什么类型的文件? ## 16 | ![](https://imgsa.baidu.com/forum/w%3D580/sign=e04ea1d4db3f8794d3ff4826e21b0ead/9ee6fe87e950352ab45767645c43fbf2b2118bba.jpg) 17 | 18 | ##3. 什么是程序可移植性?汇编语言可移植吗?为什么? 19 | 程序可移植性指在不同环境下,其是够具备可以被重复使用的性质。在移植到不同的系统平台使,所做的改动少,则可移植性好;否则,可移植性差。 20 | 21 | 22 | **汇编语言可移植性差。** 23 | 24 | 原因:汇编语言是机器语言的主机语言,针对特定的指令系统,和特定的机器指令集一一对应;程序结构性差,不便于模块化的设计,也造成可移植性差 25 | 26 | ## 4. 编译与解释有什么区别?各举出2个语言的例子 27 | 编译程序是将高级语言程序转换为机器级目标程序,执行时只需要启动目标程序即可,如将C、C++编译为Windows上的可执行2进制文件。 28 | ## 5. 解释是将高级语言语句逐条翻译成机器指令并立即执行,不生成目标 文件,例如Java需要目标机器上安装JRE。 29 | ## 6. 程序优化的目的? ## 30 | 1. 提高程序运行效率(性能) 31 | 2. 使程序更加节省存储空间和运行空间 32 | 3. 程序更加正确 33 | 4. 更可靠 34 | 5. 可移植性更好 35 | 6. 功能更强大 36 | 7. 使用更方便 37 | 8. 格式符合编程规范,接口规范 38 | 9. 更易懂(能读明白,有注释,模块化) 39 | 10. 更美 40 | 41 | 7. 计算机软件与硬件的界面/接口是什么? 42 | 操作系统内核是应用程序和硬件之间的媒介。它提供三个基本的抽象: 43 | 1. 文件是对I/O设备的抽象 44 | 2. 虚拟内存是对主存和磁盘的抽象 45 | 3. 进程是处理器、主存和I/O设备的抽象 46 | 47 | ## 8. 简述中间层语言及其运行机制 (这个应该不会考吧,没找到答案,下面这段随便看看吧) 48 | 因为很多程序只能在一种操作系统上运行。在其他操作系统上不能运行。那怎么能够支持多重硬件平台、多种软件平台(包括操作系统)呢? 49 | 50 | 这就要用到一种新的技术——中间层语言,微软有自己的中间层语言,Java也有(叫Java字节码),凡是微软的语言比如C、C++,在运行时都要先编译成MSIL(中间层语言),然后通过微软的通用语言运行时(Common Language Runtime)来运行,而Java语言在编译成Java字节码之后要在Java虚拟机上运行,Java虚拟机通过通用语言运行时对生成的中间层代码进行解释。这样程序的可移植性就有了提高。因此我们一般编程只需面对中间层预言、虚拟机即可,这是计算机系统抽象表示的发展趋势 51 | 52 | 在编译器将源代码编译为目的码的过程中,会先将源代码转换为一个或多个的中间表述,以方便编译器进行最佳化,并产生出目的机器的机器语言。通常,中间语言的设计与一般的机器语言有三个不同之处: 53 | 54 | 每个指令代表仅有一个基本的操作。举例来说,在微处理器中出现的shift-add定址模式在中间语言不会出现。 55 | 56 | 指令集内可能不会包含控制流程的资讯。 57 | 58 | 暂存器可用的数量可能会很大,甚至没有限制。 59 | 60 | 最常见的中间语言表述形式,是三位址码(Three address code)。 61 | 62 | 这个术语也同时用来代称一些作为中间层的语言,有些高级语言不会输出为机器语言,它们仅会输出这种中间语言,而这些中间语言则会像一般语言一样,提交给编译器,编译为机器语言。这通常被用于让最佳化的过程更简单,也用于增进可移植性的能力,改进移植的方式则是利用中间语言的编译器,可以编译出许多中央处理器及操作系统可使用的机器码,例如C语言。中间语言的复杂度,通常介于高阶语言及低级语言之间,例如汇编语言。 63 | 64 | ## 9. 设计开发一个Java处理器是否可行? ## 65 | 查不到 66 | https://blog.csdn.net/zxadcsdn/article/details/86409600 67 | 这篇博客画了一个java处理器的图 68 | 69 | 70 | ## 11. 程序执行结果与那些相关呢?程序编写、语言处理系统、OS、ISA-机器语言、微体系结构等 ## 71 | 程序执行结果不仅取决于算法、程序编写,而且取决于语言处理系统、操作系统、ISA、微体系结构。 72 | # QA2 # 73 | ## 1.8086 CPU内部寄存器组有哪几个?都是多少位的 ##? 74 | ![](https://imgsa.baidu.com/forum/w%3D580/sign=c8cb904d9fef76c6d0d2fb23ad16fdf6/3a3ac30828381f30c432cfc5a6014c086e06f04d.jpg) 75 | 都是16位 76 | 汇编语言中寄存器功能的解释: 77 | https://blog.csdn.net/a675311/article/details/53220590 78 | 79 | ## 2.8086 访问存储器的地址有哪部分组成?物理地址怎么形成的? ## 80 | 81 | 逻辑地址由段地址和段内偏移地址两部分组成。 82 | 83 | 存储单元的物理地址由地址加法器生成。寻址时,CPU首先将段地址和段内偏移地址送入地址加法器,地址加法器将段地址向左偏移四位并且与段内偏移地址相加,得到一个20位的物理地址。 84 | 85 | ## 3.BIU与EU是怎么完成指令执行的? ## 86 | BIU指总线接口单元,EU指执行单元。 87 | 88 | 当指令队列中有2个空字节时,BIU自动把指令取到指令队列中,当指令队列已满,而EU无总线访问请求时,BIU进入空闲状态。 89 | 90 | EU从指令队列的头部取出指令,并执行该指令。在执行中,如果需要访问内存或者I/O设备,则EU请求BIU取操作数,并直到需要的操作数到来后,EU才继续操作。若BIU处于空闲态,它立即响应请求,若BIU正在取指令到指令队列,它先完成取指令操作,再响应EU的请求。 91 | 92 | EU在执行转移、调用,返回等指令时,指令队列中的指令被清除,BIU重新从存储器中取出指令送到指令队列,EU才继续执行指令。 93 | 94 | ## 4. int x ; AX=100; AX=AX+256; x=AX; 95 | 程序/指令中的常数在内存哪个区域/段? ## 96 | 看不懂题目 97 | 未初始化的静态变量和初始化为0的静态变量或者全局变量在.bss区 98 | 已初始化的静态变量和全局变量在.data区 99 | 局部变量在栈里 100 | 101 | ## 5.全局变量x以一种什么方式在机器/汇编程序中出现?怎么访问的?至少访几次存储器 ##? 102 | 采用内存引用的方式访问全局变量。 103 | ## 104 | ## 6.C语言的数据类型char、short int、int、long、long long、float、double、long double、指针等在32/64位系统中占多少个字节?邮编为什么char[]? ## ## 105 | ![](https://imgsa.baidu.com/forum/w%3D580/sign=b836fddeb6014a90813e46b599773971/0b4437deb48f8c5432cf874335292df5e0fe7f50.jpg) 106 | 107 | (实验报告里面扒下来的) 108 | 109 | # QA3 # 110 | ## 0. 计算机是64位的是指CPU寄存器是64位的? ## 111 | 计算机是xx位的,指的是计算机CPU字的长度(字长)。 112 | 计算机中ALU的位数 = CPU中通用寄存器的位数 = 计算机的位数 113 | 114 | ## 1. 常量表达式是谁来计算的? ## 115 | 编译器在编译阶段计算。 116 | 117 | ## 2.0与‘0'谁大?差多少?空间呢?? ## 118 | 字符‘0’的ASCII码是48,则‘0’比0大,差48。 119 | 120 | 空间和声明的类型有关,如果0声明为int, ‘0’声明为char,则数字0占用的空间较大。 121 | 122 | ## 2. C源程序中的有符号常数,是怎么变成二进制补码的和进行运算的? 123 | 什么破问题。 124 | 125 | ## 3. strlen(“1234567 我想毕业\n”)=?怎么算汉字数 ## 126 | strlen计算的长度是该字符串对应的编码占用的字节数,所以这个长度在不同的编码下结果也不相同。例如,gbk编码下中文字符占用2字节,UTF-8存储中文时占2~4个字节. 127 | 计算汉字数?在UTF-8里,英文字符仍然跟ASCII编码一样,因此原先的函数库可以继续使用。而中文的编码范围一般是在0080-07FF之间,因此是2个字节表示。计算长度时可以根据 128 | 1字节 0xxxxxxx 129 | 2字节 110xxxxx 10xxxxxx 130 | 3字节 1110xxxx 10xxxxxx 10xxxxxx 131 | 4字节 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 132 | 5字节 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 133 | 6字节 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 134 | 由开始的0和1的个数来判断字节数,从而计算。(我自己这么觉得,查不到一个靠谱的答案) 135 | [https://blog.csdn.net/u014431852/article/details/48003321](https://blog.csdn.net/u014431852/article/details/48003321) 136 | [https://blog.csdn.net/tinyletero/article/details/8201465](https://blog.csdn.net/tinyletero/article/details/8201465) 137 | ## 5.汉字是怎么输入进入源程序的(Win/Linux)? ## 138 | ## 6. OS内核的编码决定了基于其上的系统软件与应用软件的编码要与其一致。不一致怎么办?要转换吗?谁来转换? ## 139 | 要转换。 140 | ## 7.当前的源程序编码是什么? 可以变吗? ## 141 | 142 | [https://wenku.baidu.com/view/191f12537cd184254b353560.html](https://wenku.baidu.com/view/191f12537cd184254b353560.html)这个讲的很清楚 143 | ## 8. 汉字是怎么显示/打印输出的? ## 144 | ASCII码->编号->字库中点阵图编码->打印 145 | 146 | # QA4 147 | ## 1. IEEE754比整数部分10位+小数部分20位的表示方法有什么优点? 缺点呢—考虑下? ## 148 | 优点:可表示数值范围较大,精度较高;不知道有啥缺点,等我问问老师 149 | ## 2. float非无穷的最大值,最小值? ## 150 | float非无穷的最大值IEEE754表示是0111_1111_0111_1111_1111_1111_1111_1111 151 | 阶码是127,尾数是2-2^(-23)。所以最大值是(2-2^(-23))* 2^127 152 | 最小值符号位取1即可 153 | 正数的最小值,IEEE754表示是0000_0000_0000_0000_0000_0000_0000_0001 154 | 阶码是1-127 = -126,尾数是2^(-23)。值是2^(-149) 155 | ## 3. Float的最大绝对值?最小绝对值? ## 156 | 和上面重复 157 | ## 4. float数 1,65536,0.4,-1,0的内存表示 ## 158 | 1 = 1*2^0 则frac = 000_0000_0000_0000_0000_0000, exp = 0111_1111(127) 159 | 表示是0011_1111_1000_0000_0000_0000_0000_0000 160 | 65536 = 2^16, 则frac = 000_0000_0000_0000_0000_0000,exp = 1000_1111 161 | 表示是0100_0111_1000_0000_0000_0000_0000_0000 162 | 163 | 0.4 = 0.0110_0110_0110……,涉及到舍入问题,frac = 100_1100_1100_1100_1100_1100 164 | exp = 0111_1101(125),表示是0011_1111_0101_1011_0110_1101_1011_0111 165 | -1和0略 166 | ## 5. 一个数的Float形式是唯一的吗?(除了0) ## 167 | 是,根据IEEE754标准,一个数字对应的格式是唯一的 168 | ## 6. 每一个IEEE754编码对应的数是唯一的吗? ## 169 | 不是,有些数字不能用二进制数字精确表示,涉及到舍入问题,可能不同的数字舍入之后对应的IEEE754编码完全相同。另外,当数字超出IEEE编码所能表示的范围,不同数字对应的编码都是一样的。(原因是我自己的想法) 170 | ## 7. Float的阶码范围是多少? ## 171 | -126 ~ +127 172 | 简述Float数据的浮点数密度分布? 173 | 分布不均匀,靠近远点处它们更稠密(课本P80) 174 | ## 8. C语言中除以0一定报错溢出吗? ## 175 | 整数报错,浮点无穷大X/0>Y 可以 176 | ## 9. int与float都占32个二进制位,Float与INT相比谁的个数多?各自是多少个?多多少? (+-0、nan) ## 177 | Int表示的个数多(不知道这个表示是什么意思,我理解为精确表示) 178 | 179 | Int 可以表示2^32个数字(+0和-0看做不同的数) 180 | 181 | float可以表示2^32-2^24个数字(+0.0和-0.0看做不同的数,不算INF和NAN) 182 | 183 | ## 10. Float的最大密度区间(非无穷)?Float数多少?密度多少? ## 184 | 按照阶码区域写出float的最大密度区域的范围及其密度,最小密度区域及其密度(区域长度/表示的浮点个数) 185 | 186 | 阶码是0000_0000(表示的数值范围是-1.1754942E-38 到1.1754942E-38)、(1 - (1/2)^23)*2^-149_(最小密度区间)、阶码是1111_1110(表示的数值范围是-3.4028235E38到-1.7014118E38和1.7014118E38到3.4028235E38)、 187 | (2^104 - 2^81)(最大密度区间) 188 | 189 | 190 | 微观世界:能够区别最小的变化___2^-149_____,其10进制科学记数法为_____1.4E-45____ 191 | 192 | 宏观世界:不能区别最大的变化____2^128____,其10进制科学记数法为______3.4*10^38___ 193 | 194 | ## 11. Float的最小密度区间?Float数多少?密度多少? ## 195 | 同上 196 | ## 12. Float最大密度区间是最小密度区间的密度的多少倍? ## 197 | 约等于2^253 - 2^230 198 | ## 13. float最大的负数是多少==最小的正数是多少? ## 199 | 正数的最小值,IEEE754表示是0000_0000_0000_0000_0000_0000_0000_0001 200 | 阶码是1-127 = -126,尾数是2^(-23)。值是2^(-149) 201 | 202 | ## 14. 怎么判断和定义浮点数的无穷大以及NaN? ## 203 | 当exp部分都是1 204 | 205 | 如果frac部分的位模式都是1表示INF 206 | 207 | 否则,表示NAN 208 | 209 | 浮点数的表示,越小精度越高,越大精度越低,这也基本符合数据处理的规律。太大的数据差点没啥,就是个规模而已。如人口、GDP等,没有必要到个、圆角分。 210 | ## 15. Float使用注意事项? 怎么比较两个float数据? ## 211 | 先判断是不是NAN,如果有一个数字是NAN,比较无意义。 212 | 213 | 如果均是数字,则按照下面原则进行比较: 214 | 215 | 先比较符号位,符号位是0的比符号位是1的大 216 | 。 217 | 阶码从最高位进行比较,例如阶码第一位是1的数字比阶码第一位是0的数字大,如果第一位相同向后比较即可,遵从同样的规则,直到分区处大小或者阶码相同。 218 | 219 | 如果阶码相同则比较尾数部分,同样是先比较高位之后比较低位即可。 220 | 221 | ## 16. 浮点数的舍入采用什么规则? ## 222 | IEEE浮点格式定义了四种不同方式的舍入方法,默认的方式是找到最接近的匹配,而其他三种可用于计算上界和下界。向偶数舍入是默认的舍入方法。 223 | ## 17. 向偶数舍入方法,尾数会不会产生溢出,如果产生溢出怎么办?阶码会不会溢出?怎么办?printf函数把浮点数按照十进制数打印%10.2f,按照什么舍入原则呢? 到底是什么?不要想当然 ## 224 | 向偶数舍入,尾数可能产生溢出。 225 | 226 | 产生溢出,把阶码加一即可。 227 | 228 | 阶码产生溢出,说明这个数字超出了浮点数可以表示的范围。 229 | 230 | 按照十进制数字的四舍五入原则。 231 | ## 18. 通用寄存器的通用是什么意思 ## 232 | 因为通用寄存器既可用于传送和暂存数据,也可参与算术逻辑运算,并保存运算结果。 233 | ## 19. 32位CPU怎么实现64位加法 ## 234 | 大多数处理器都包含一个进位位和溢出位来支持位宽大于机器字长的数字的运算。 235 | 进位位用于无符号运算,溢出位用于有符号数运算。并且,位宽较大的数字分开存储即可。(参考家庭作业2.75) 236 | 237 | 大多数操作基本上是串行的。当在二进制电平上进行加法运算时,您需要两个输入位,并产生一个结果位和一个进位位。然后当将下一个最低有效位加入时,进位位用作输入,以此类推( 238 | 239 | CPU字长只是限制了它可以运行的最大时钟速度 240 | 241 | 原文链接http://www.it1352.com/489105.html 242 | 243 | ## (*)20. 数组、结构、指针各用什么寄存器指示? ## 244 | 245 | 246 | ## 21. 标志寄存器有什么作用? ## 247 | 248 | (课本)他们描述了最近的算术和逻辑操作的属性,可以检测这些寄存器来执行条件分支指令。 249 | 250 | (博客)具有以下 3 种作用。 251 | 252 |   (1)用来存储相关指令的某些执行结果; 253 | 254 |   (2)用来为 CPU 执行相关指令提供行为依据; 255 | 256 |   (3)用来控制 CPU 的相关工作方式 257 | 258 | ## 21. 状态标志位有哪几个?加法运算会影响那些? ## 259 | 记住四个就行: 260 | CF:进位标志。最近的操作使最高位产生了进位。可用来检查无符号操作的溢出。 261 | 262 | 263 | ZF:零标志。最近的操作得出的结果为0. 264 | 265 | SF:符号标志。最近的操作得到的结果为负数。 266 | 267 | OF:溢出标志。最近的操作导致一个补码溢出一一正溢出或负溢出。 268 | 269 | 加法运算都可能改变。 270 | 详细介绍: 271 | https://www.cnblogs.com/eleven24/articles/7783283.html 272 | 273 | ## 22.控制标志位 IF/DF作用,怎么改变?(应该不考,毕竟书上没有提到) ## 274 | DF标记块传送是从低地址向高地址传,还是从高地址向低地址传。 275 | 276 | IF可以屏蔽 可屏蔽中断请求INTR 277 | 278 | 如果外设有可屏蔽中断请求INTR,而此时CPU内IF=0,那么CPU不会响应中断 279 | 280 | 只有可屏蔽中断请求INTR和IF有关系, 281 | 282 | 内中断和不可屏蔽中断NMI,都不受IF的影响 283 | 284 | ## 23. CF与OF是什么关系? ## 285 | 286 | CF针对无符号数(将寄存器中的操作数都看作是无符号数) 287 | 288 | OF针对有符号数(将寄存器中的操作数都看作是有符号数) 289 | 290 | ## 24. 计算机怎么判断两个数相加是否超出了范围? ## 291 | 292 | CF位来判断无符号数运算的溢出,OF位来判断补码运算是否溢出。 293 | 294 | ## 25. 怎么修改IP寄存器? ## 295 | 296 | IP寄存器的值通常不能直接被修改,需要使用call,ret,jmp等控制转移指令类修改。 297 | ## 26. 32/64位的cpu中寄存器都是32/64位的? ## 298 | 299 | 不是,第三章最后提到的媒体寄存器是256位的,段寄存器(有的说32位,有的说16位) 300 | 301 | CPU的字长 == 通用寄存器的位宽 302 | 303 | ## 27. 请列出C语言的所有操作/指令,与汇编语言对比,说明汇编语言的优点 ## 304 | 重点:逻辑操作/位操作 305 | 306 | 这个不会 307 | 308 | ## 28. 操作数的寻址方式有哪几种? ## 309 | ![](https://imgsa.baidu.com/forum/w%3D580/sign=c9f0e7b379c6a7efb926a82ecdfbafe9/a31dec381f30e9249678de5d43086e061c95f790.jpg) 310 | ## 29. 一/二维数组采用什么寻址方式? ## 311 | 比例变址寻址 312 | ## 30.结构体的某一个整型成员采取什么寻址方式? ## 313 | 314 | ## 31. 结构体的整形数组采用什么寻址方式? ## 315 | 316 | ## 一个C生成的执行程序是多少位的是由谁来决定的? ## 317 | 318 | (A)CPU (B)OS (C)编译器 (D)源程序 319 | 320 | ** C ** 321 | 322 | -------------------------------------------------------------------------------- /期末复习QA/QA10.md: -------------------------------------------------------------------------------- 1 | # Q&A 10 2 | 3 | --- 4 | ## switch多分支,case条件码规律时是怎么实现的?(注意我们讲的与书上不同,看下不同在哪儿?) 5 | ### 主要采用的数据结构是什么? 6 | 跳转表 7 | ### 跳转表的元素类型?(不同) 8 | 一个代码段的地址 9 | ### 跳转表的元素个数怎么计算? 10 | 跳转元素的最大可能值-最小可能值 11 | ### 跳转表的初值是什么?什么时候由谁来赋初值的? 12 | 没看懂 13 | ### 元素下标怎么计算? 14 | 跳转元素-跳转元素最小可能值 15 | ### 怎么计算跳转地址? 16 | 当下标在跳转表中时,跳转地址=基地址+下标*8 17 | ### 跳转指令是什么寻址方式? 18 | 变址寻址 19 | 20 | --- 21 | ## 叶子节点与非叶子节点的函数汇编语言实现有什么区别? 22 | 叶子节点的函数无需使用push,pop等保存,传递参数,也无需使用call调用其他函数,但非叶子节点的函数需要 23 | 24 | --- 25 | ## 递归程序的汇编语言是怎么实现的? 26 | 递归无需特殊的处理 27 | 栈帧意味着每个函数调用私有的存储 28 | 保存的寄存器 、局部变量 29 | 保存的返回地址 30 | 寄存器保存约定: 31 | 防止函数调用损毁其他函数(调用)的数据 32 | 除非C代码明确地这样做(如缓冲区溢出) 33 | 栈的使用原则:遵循 调用/返回 模式 34 | 如 P调用Q, 然后Q 在P结束之前返回 35 | 后入、先出 36 | 对互递归同样有效 37 | P调用Q; Q调用P -------------------------------------------------------------------------------- /期末复习QA/QA11.md: -------------------------------------------------------------------------------- 1 | # Q&A11 2 | 3 | --- 4 | ## 静态局部变量是怎么生成汇编语言的? 5 | 空间:全局区 6 | 赋初值:定义该变量的函数第一次被调用时 7 | 生命周期:整个源程序的周期 8 | 9 | --- 10 | ## 分析全局变量、局部变量、传值的参数的生命周期 11 | 静态全局变量:整个源程序的周期 12 | 动态全局变量:整个源程序的周期 13 | 静态局部变量:整个源程序的周期 14 | 动态局部变量:定义它的函数的周期 15 | 传值的参数:定义它的函数的周期 16 | 17 | --- 18 | ## 简述缓冲器溢出的原理 19 | 缓冲区溢出,简单的说就是计算机对接收的输入数据没有进行有效的检测,向缓冲区内填充数据时超过了缓冲区本身的容量,而导致数据溢出到被分配空间之外的内存空间,使得溢出的数据覆盖了其他内存空间的数据。 20 | 21 | --- 22 | ## 怎么攻击缓冲器溢出的漏洞? 23 | 攻击原理(3个采分点):向程序输入缓冲区写入特定的数据,例如在gets读入字符串时,使位于栈中的缓冲区数据溢出,用特定的内容覆盖栈中的内容,例如函数返回地址等,使得程序在读入字符串,结束函数gets从栈中读取返回地址时,错误地返回到特定的位置,执行特定的代码,达到攻击的目的。 24 | 25 | --- 26 | ## 怎么防范缓冲器溢出漏洞? 27 | 防范方法(2个采分点,有2个就算对): 28 | 1. 代码中避免溢出漏洞:例如使用限制字符串长度的库函数 29 | 2. 随机栈偏移:程序启动后,在栈中分配随机数量的空间,将移动整个程序使用的栈空间地址 30 | 3. 限制可执行代码的区域 31 | 4. 进行栈破坏检查——金丝雀 32 | 33 | --- 34 | ## 结构体的成员在汇编语言层面是怎么操作的? 35 | 结构体用内存块来表示 36 | 足够大,可容纳所有字段 37 | 字段顺序必须与声明一致 38 | 即便其他顺序能使得内存更紧凑——也不行! 39 | 编译器决定总的尺寸和各字段位置 40 | 机器级程序不解读(理解)源代码中的结构体 41 | 数组元素的地址 42 | 每个结构体成员的偏移量(Offset)是在编译阶段确定的 43 | 地址计算形式:r + size*idx 44 | 结构体成员通过结构体基地址+偏移量的方式访问 45 | 46 | --- 47 | #结构体成员作为参数传输是怎么实现的? 48 | 传递结构体基地址+偏移量处的对应成员 49 | 50 | --- 51 | #结构体成员作为返回值是怎么实现的? 52 | 返回结构体基地址+偏移量处的对应成员 53 | 54 | 55 | -------------------------------------------------------------------------------- /期末复习QA/QA12.md: -------------------------------------------------------------------------------- 1 | # Q&A12 2 | 3 | --- 4 | # Y86-64CPU的状态存储部件主要有哪些? 5 | RF:程序寄存器 15个64位 %rax,%rcx,%rdx.%rbx,%rsp,%rbp,%rsi,%rdi,%r8到%r14 6 | CC:条件码 3个1位 ZF SF OF 7 | Stat:程序状态 8 | PC:程序计数器 9 | DMEM:内存 10 | 11 | --- 12 | # Y86-64的寻址方式有哪几种? 13 | 指令寻址方式: 14 | 顺序寻址方式 PC 15 | 跳跃寻址方式 jXX指令 16 | 操作数寻址方式: 17 | 寄存器寻址 18 | 立即寻址 19 | 间接寻址 20 | 基址寻址 21 | 22 | --- 23 | # Y86-64指令系统的算逻操作的操作数与X86-64有什么区别? 24 | Y86-64仅允许在寄存器数据中应用算逻操作 25 | X86-64则允许立即数,寄存器和内存 26 | 27 | --- 28 | # Y86-64的Jxx跳转指令,寻址方式与X86-64(短/近)有什么区别? 29 | 对目的地址进行完整编码 30 | 和x86-64中的PC相对地址编码不一样 31 | 32 | --- 33 | # rrmovq rax,rbx的机器指令是? 34 | 2005 35 | 0010 0000 0000 0101 36 | 37 | --- 38 | # Y86-64支持过程调用的指令有哪几个? 39 | call 40 | ret 41 | 42 | --- 43 | # Y86-64标志位怎么改变-副作用,怎么利用? 44 | 算数和逻辑操作OPq设置条件码作为指令执行的副作用 45 | 条件传送指令,条件跳转指令以条件码的值为依据 46 | 47 | --- 48 | # 状态码是怎么产生的?X86为什么看起来没有呢? 49 | AOK(1)-正常操作 50 | HLT(2)-执行Halt 51 | ADR(3)-错误的指令或数据地址 52 | INS(4)-无效指令 53 | 对于Y86-64,当遇到这些异常的时候,我们就简单地让处理器停止执行指令。在更完整的设计中,处理器通常会调用一个异常处理程序,这个过程被指定用来处理遇到的某种类型的异常。就像在第8章中讲述的,异常处理程序可以被配置成不同的结果,例如,中止程序或者调用一个用户自定义的信号处理程序。 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /期末复习QA/QA13.md: -------------------------------------------------------------------------------- 1 | # Q&A 13 2 | 3 | --- 4 | ## Y86的SEQ分哪几个阶段?各阶段的主要过程是什么? 5 | 取指:从指令存储器读取指令 6 | 译码:读程序寄存器 7 | 执行:计算数值或地址 8 | 访存:读或写数据 9 | 写回:写程序寄存器 10 | 更新PC:更新程序计数器 11 | 12 | --- 13 | ## 对Y86的SEQ阶段分析,12条指令的微操作实现的共同点有哪些? 14 | 所有的指令有相同的格式,都按照6个阶段顺序执行 15 | 每一阶段的微操作,按照顺序有3-8种 16 | 每一条指令在各阶段执行时,根据指令类型按顺序执行不同的微操作 17 | 18 | --- 19 | ## Y86的硬件结构包括哪些? 20 | 硬件单元(组合、时序逻辑)、控制逻辑、信号连接线 21 | 组合逻辑:硬件单元间传播,不用CLK,有延迟 22 | 计算逻辑(ALU、PCINC) 23 | 读逻辑( PC、CC、RF、imem、dmem) 24 | 硬件单元的时序逻辑-状态单元:都在CLK上升沿时更新。控制阻断 25 | 时钟寄存器:PC、CC、STAT的更新 26 | 随机访问存储器:RF写、dmem写 27 | 28 | --- 29 | ## 怎么理解SEQ原则-从不回读? 30 | 所谓CPU顺序实现SEQ:一个时钟变化,引发一个经过组合逻辑的流,从而执行整个指令 31 | 一条新指令的执行,即一个CLK是把上一条指令计算结果-状态单元更新,然后其值进行组合逻辑(新指令的计算)的传播 32 | 要控制CPU中活动的顺序,只需要用CLK控制寄存器和内存 33 | 所有6个阶段的所有步骤的状态更新同时发生(逐级延迟) 34 | 且只在时钟上升开始下一个周期时。 35 | 当前时钟/指令n结束,下一时钟/指令n+1没有到上升沿前,状态单元的数据-状态并不是本指令/时钟n执行的结果,仍然是上一时钟/指令n-1的结果 36 | 指令n的执行结果,到下一指令n+1的上升沿才更新到转状态单元 37 | 38 | --- 39 | ## HALT、NOP的微操作?新增C0指令Iraddq V,rB的微操作? 40 | $ $|halt 41 | -|- 42 | 取指|icode:ifun$\leftarrow$M1[PC]读指令字节 43 | 译码| 44 | 执行|Stat$\leftarrow$HLT 45 | 访存| 46 | 写回| 47 | 更新PC| 48 | 49 | $ $|nop 50 | -|- 51 | 取指|icode:ifun$\leftarrow$M1[PC]读指令字节
valP$\leftarrow$PC+1计算下一个PC 52 | 译码| 53 | 执行| 54 | 访存| 55 | 写回| 56 | 更新PC|PC$\leftarrow$valP 57 | 58 | $ $|iraddq V,rB 59 | -|- 60 | 取指|icode:ifun$\leftarrow$M1[PC]读指令字节
rA:rB$\leftarrow M_{1}$[PC+1]读寄存器字节
valC$\leftarrow M_{8}$[PC+2]读立即数8个字节
valP$\leftarrow$PC+10计算下一个PC 61 | 译码|valB$\leftarrow$R[rB]读操作数B 62 | 执行|valE$\leftarrow$valB+valC
Set CC 63 | 访存| 64 | 写回|R[rB]$\leftarrow$valE 65 | 更新PC|PC$\leftarrow$valP 66 | 注意:execute中的运算是valB OP valA,而不是valA OP valB 67 | M1[PC] 表示从PC开始的内存中读取一个字节的数据 68 | V=====valC,指令中V是加数,是ALUA的一个输入 69 | 这些微操作都有现成的硬件结构,所以不需要改动状态单元,但控制逻辑需要稍微调整 70 | 71 | --- 72 | ## 取指逻辑的need_valC的HCL 73 | need_valC:这个指令包括一个常数字吗? 74 | bool need_valC = icode in { IIRMOVQ,IRMMOVQ,IMRMOVQ,IJXX,ICALL }; 75 | 76 | --- 77 | 硬件单元: 78 | 运算逻辑: ALU、PCINC、 79 | 时钟寄存器:PC、CC、STAT、 80 | 随机访问存储器:RF、IMEM、DMEM 81 | 读操作-看成组合逻辑-有地址就有输出 82 | 随机访问存储器:RF、imem/dmem可以用特殊的时钟电路来模拟 83 | 时钟寄存器:PC、CC、STAT、 84 | 写操作:需要时钟信号控制,控制阻断 85 | RF、DMEM、PC、CC、STAT 86 | 控制逻辑:每一阶段每一步骤的微操作,在硬件上的映射 87 | 在不同硬件单元之间传送数据 88 | 操作硬件单元,使得对不同指令执行指定的微操作 89 | 组合逻辑:不需要任何时序或控制,只要输入变化了,值就通过逻辑门网络传播 90 | 时序逻辑:需要时钟信号控制,进行更新-把输入锁存到输出 91 | 92 | -------------------------------------------------------------------------------- /期末复习QA/QA14.md: -------------------------------------------------------------------------------- 1 | # Q&A 14 2 | 3 | --- 4 | ## Y86-64的SEQ顺序结构实现的缺点是什么? 5 | 实际使用起来太慢 6 | 信号必须能在一个周期内传播所有的阶段,其中要经过指令内存、寄存器文件、ALU以及数据内存等 7 | 时钟必须非常慢 8 | 硬件单元只在时钟周期的一部分时间内被使用 9 | 10 | -------------------------------------------------------------------------------- /期末复习QA/QA15 - 21.md: -------------------------------------------------------------------------------- 1 | *** 2 | 3 | *** 4 | # QA15 5 | 6 | ## 为什么用连接器? 7 | * 链接器可以使得分离编译成为可能:我们可以把一个大的应用程序分解成许多小的,更好管理的模块,单独编译每一个模块。 8 | * 在修改的时候,只用编译被修改的模块,重新链接就行。能节省大量时间。 9 | *** 10 | ## 连接器工作内容与步骤? 11 | 1. 符号解析 12 | 2. 重定位 13 | *** 14 | ## **局部变量** 链接时按照 **本地符号** 进行解析 15 | ## **全局变量** 按照 **全局符号** 进行解析 16 | *** 17 | ## 链接器对局部变量、参数等符号怎么解析? 18 | * 对符号的解析: 19 | * 规则 1:不允许多个同名的强符号 20 | * 规则 2:若有一个强符号和多个弱符号同名,则选择强符号 21 | * 规则 3:如果有多个弱符号,选择任意一个 22 | *** 23 | ## goto L0 的地址符号的强弱?链接时怎么处理? 24 | * 25 | *** 26 | ## 有哪几种目标文件?怎么查看目标文件的各节信息? 27 | 28 | * 目标文件: 29 | * 可重定位目标文件:包含数据和代码,编译时多个可重定位目标文件生成可执行目标文件。 30 | * 可执行目标文件:包含数据和代码,可以被直接加载运行。 31 | * 共享目标文件:特殊的目标文件,可以在加载或运行时被动态的加载进内存并链接。(这个用于动态链接) 32 | * 查看目标文件的各节信息 33 | * objdump 34 | *** 35 | ## 可执行目标文件中的重定位主要完成哪些工作? 36 | 37 | * 将多个单独的代码节(sections)和数据节合并为单个 38 | 节。 39 | * 将符号从它们在.o文件中的相对位置重新定位到可 40 | 执行文件中的最终绝对内存位置。 41 | * 用它们的新位置,更新所有对这些符号的引用。 42 | *** 43 | ## 目标文件中的数据节有哪几种,分别有什么区别? 44 | * ELF头 45 | * 段头表/程序头表 46 | * .text节 47 | * .rodata节(只读数据) 48 | * .data节(数据/可读写) 49 | * .bss节(未初始化全局变量) 50 | * .symtab节(符号表) 51 | * .rel.text节(可重定位代码) 52 | * .debug节(调试信息表) 53 | * 节头表Section header table(每个节的偏移量、大小) 54 | *** 55 | ## 可执行目标文件在执行时由操作系统把各节按大小在内存分配,并拷贝相应内容到内存区 56 | *** 57 | 58 | *** 59 | # QA16 60 | 61 | ## 按照从低到高地址,Linux下内存分成哪几个区? 62 | * text段(代码段):存放程序代码,通常只读 63 | * data段(数据段):存储已经初始化的全局变量和静态变量,可读可写。 64 | * bss段(数据段):存储未初始化的全局变量和静态变量 65 | * rodate(数据段):存储数据常量,只读。 66 | * 栈stack(数据段):存储参数变量和局部变量,从高向低地址生长。 67 | * 堆heap:被动态分配的内存段,从低向高生长。 68 | *** 69 | ## 全局变量怎么实现重定位(32位/64位)? 70 | * 未解答 71 | *** 72 | ## 子程序怎么实现重定位? 73 | * 未解答 74 | *** 75 | ## Linux下归档器/库管理程序是什么? 76 | * Idconfig(不确定) 77 | *** 78 | ## 库管理中每个函数可以生成单独.o也可以多个函数用一个.o,对应的源程序也是这样的。 79 | *** 80 | ## 静态连接时的目标文件生成与库文件的顺序无关。 81 | *** 82 | ## 共享库的动态连接有哪几种方法? 83 | * 加载时链接:当可执行文件首次加载和运行时进行动态链接 84 | * 运行时链接:在程序开始运行后(通过编程指令)进行动态链接 85 | *** 86 | ## 库打桩的应用有哪几个方面? 87 | * 安全 88 | * 监禁confinement (沙箱sandboxing) 89 | * 幕后加密 90 | * 调试 91 | * 监控和性能分析 92 | * 计算函数调用的次数 93 | * 刻画函数的调用位置(sites)和参数 94 | * Malloc 跟踪 95 | * 检测内存泄露 96 | * 生成地址痕迹(traces) 97 | *** 98 | ## 在什么时候进行库打桩? 99 | * 打桩可出现在: 100 | * 编译时:源代码被编译时 101 | * 链接时间:当可重定位目标文件被静态链接来形成一个可执行目标文件时 102 | * 加载/运行时:当一个可执行目标文件被加载到内存中, 动态链接,然后执行时 103 | *** 104 | *** 105 | # QA17 106 | 107 | ## 从哪几方面优化程序?在底层与核心技术方面、上层与应用方面、算法与UI方面等优化重点有何区别? 108 | * 多个层次进行优化: 109 | * 算法 110 | * 数据表示 111 | * 过程 112 | * 循环 113 | * 区别: 114 | * 不知道 115 | ## 程序的性能优化从哪几方面解决?优化性能为何一定要理解“系统”? 116 | 117 | * 程序是怎样被编译和执行的 118 | * 现代处理器+存储系统是怎么运作的 119 | * 怎样测量程序性能,确定“瓶颈” 120 | * 如何在不破坏代码模块性和通用性的前提下提高性能 121 | *** 122 | 123 | ## 编译器能做的程序员也通常采用的一般有用的优化有哪几种? 124 | 125 | * 代码移动 126 | * 减少计算执行的频率 127 | * 复杂运算简化 128 | * 用更简单的方法替换昂贵的操作 129 | * 移位、加,替代乘法/除法 130 | * 识别乘积的顺序 131 | * 共享公用子表达式 132 | * 重用表达式的一部分 133 | * GCC 使用 –O1 选项实现这个优化 134 | * 去掉不必要的过程调用 135 | *** 136 | 137 | ## 妨碍编译器优化的因素有哪些?为什么编译器不能优化呢? 138 | * 函数调用 139 | * 函数可能有副作用 140 | * 对于给定的参数,函数可能返回不同的值 141 | * 内存别名使用 142 | *** 143 | 144 | ## 妨碍编译器优化的函数调用怎么进行补救或处理? 145 | 146 | * 使用inline内联函数 147 | * 用-O1是GCC这样做,但局限于单个文件之内 148 | * 自己做代码移动 149 | *** 150 | 151 | ## 妨碍编译器优化的内存别名怎么进行补救或处理? 152 | * 移除内存别名,不需要存储中间结果。 153 | *** 154 | 155 | ## 编译器优化的其他方法与策略? 156 | * 减少函数调用 157 | * 用临时/局部变量累计结果 158 | * 避免每个循环的边界检查 159 | * 使用指令级并行 160 | *** 161 | # QA17.5 其他编译相关优化技术 162 | 163 | * 寄存器比内存快 164 | * 用快的指令 165 | * 用快的寻址方式 166 | * 位操作比算术运算快 167 | * 整数比浮点运算块 168 | * 宏比函数快 169 | * 简单的结构比复杂结构快 170 | * 算法很重要 171 | * C嵌入汇编快 172 | * 并行比串行快 173 | *** 174 | *** 175 | # QA18 176 | 177 | ## 简述现代超标量CPU的结构与执行程序特点 178 | 179 | * 一个周期执行多条指令。 180 | * 这些指令是从一个连续的指令流获取的,通常被动态调度的。 181 | *** 182 | ## 什么容量、延迟界限、发射时间,他们之间有什么关系? 183 | 184 | * 容量:表示能够执行该运算的功能单元的数量 185 | * 发射时间:表示两个连续的同类型的运算之间需要的最小时钟周期数 186 | * 延迟界限:简单理解就是延迟,整数加法的延迟界限为1,整数乘法的为3,浮点加法为3,浮点乘法为5 187 | 188 | * 关系: 189 | * 同一组指令,运算单元容量越大,发射时间越短,延迟界限越短 190 | * 单元容量一定,延迟界限越大,发射时间越长。 191 | *** 192 | ## x = (x OP d[i]) OP d[i+1] 与x = x OP (d[i] OP d[i+1])哪一个并行性好? 193 | * x = x OP (d[i] OP d[i+1]) 的并行性更好 194 | * 下一个循环的操作可以早一些开始,对之前的数据没有依赖。 195 | *** 196 | ## 采用x0 = x0 OP d[i]; x1 = x1 OP d[i+1]; 的分离累加器方法与x = x OP (d[i] OP d[i+1])的在流水线利用方面有何区别? 197 | 198 | * 第一种情况为两条独立的流水,第二种情况的运算全部在同一条流水线中进行。 199 | *** 200 | 201 | ## 什么是SIMD?为什么会提高整数运算的速度? 202 | * SIMD全称Single Instruction Multiple Data,单指令多数据流,能够复制多个操作数,并把它们打包在大型寄存器的一组指令集。 203 | * 以同步方式,在同一时间内执行同一条指令。(百度答案我感觉不太对) 204 | ## 对CPU分支预测怎么处理的?程序员与编译器怎么应对? 205 | * 对分支预测的处理: 206 | * 静态预测: 207 | * 预测分支永远不会发生(或永远会发生),因此总是顺序取下一条指令推测执行。仅当条件跳转指令被求值确实发生了跳转,则非顺序的代码地址被加载执行。 208 | * 动态预测 209 | * 避免不可预测的分支 210 | *** 211 | *** 212 | # QA19 213 | 214 | ## 以指令执行为例,说明CPU、内存、外存在数据访问与处理上的区别。 215 | 216 | ## 内存MM除了存储单元外,还有哪些硬件? 217 | 218 | ## 内存中的行缓冲器通常采用SRAM还是DRAM? 219 | * DRAM(百度没有答案,谷歌上的一本书说是RDRAM,RDRAM是一种SDRAM) 220 | --- 221 | ## SRAM与DRAM物理构成有何区别? 222 | * SRAM存储1位需要4或6个晶体管(双稳态触发器),DRAM需要一个1个电容和1个晶体管(电容的电荷)。 223 | --- 224 | ## DRAM芯片访问时地址信号线是怎么复用的? 225 | 226 | * CPU将地址A放到总线上 227 | * 总存储器从总线上读地址A,取出字x,然后将x放到总线上 228 | * CPU从总线上读入字x,并将其放入寄存器%rax 229 | * CPu将地址A放到总线上,主存储器读地址A并等待数据到来 230 | *** 231 | 232 | ## 内存是4G的,此地址空间不包括BIOS的ROM存储器 233 | --- 234 | 235 | ## 内存提高访问速度的方法有哪几种? 236 | 237 | --- 238 | ## 为什么每出现新一代DRAM芯片,容量至少提高4倍? 239 | * 行地址和列地址分时复用, 每出现新一代存储器芯片,至少要增加一根地址线。每加一根地址线,则行地址和列地址各增加一位,所以行数和列数各增加一倍,因而容量至少提高到4倍。 240 | --- 241 | ## 操作系统对硬盘访问采用逻辑方式(簇),由谁来实现此块号与物理磁面号/道号/扇区号的访问呢? 242 | * 由磁盘控制器维护。 243 | --- 244 | ## 内存与硬盘间传输的块方式采用什么硬件实现? 245 | * DMA 246 | 247 | *** 248 | *** 249 | # QA20 250 | 251 | ## 存储访问局部性原理是什么意思? 252 | 253 | * 程序倾向于使用距离最近用过的指令/数据地址相近或相等的指令/数据。 254 | *** 255 | 256 | ## Cache访问不命中的种类? 257 | 258 | * 冷不命中(强制不命中):当缓存为空时,对任何数据的请求都会不命中 259 | 260 | * 冲突不命中:当缓存足够大,但是被引用的对象都映射到同一缓存块中,此种不命中称为冲突不命中 261 | 262 | * 容量不命中:当工作集(working set)的大小超过缓存的大小时,会发生容量不命中 263 | *** 264 | ## Cache怎么组织,其大小怎么计算? 265 | 266 | * Cache共有S(2* *s)组,每个组有E(2** 267 | e)行,每行有1个有效位,t个标记为,有B(2**b)字节 268 | * cache大小:C = S * E * B数据字节 269 | *** 270 | 271 | ## 程序怎么利用高速缓存访问存储器? 272 | 273 | * 程序会先到高速缓存中寻找所需数据。如果能够找到,直接从高速缓存中调用,不再访问内存。当cache中对应位置为空时,发生冷不命中,将从下一级存储中寻找数据并将其放入本级存储。当对应位置不为空但标记位不同是,发生冲突不命中,将从下一级存储中寻找数据并替换此处数据。 274 | *** 275 | 276 | ## 空间局部性好的程序也常会发生Cache抖动,导致性能大降,是怎么回事?怎么解决? 277 | * 可能是因为cache的容量小或每组内的行数较少,关联度低,导致冲突不命中的数量增多。(不确定) 278 | --- 279 | ## 为什么用中间位不用高位作为组/索引? 280 | * 如果用高位做索引,连续的存储器块会映射到相同的高速缓存块中,具有良好空间局部性的程序顺序扫描一个数组的元素时,高速缓存总保存着一个块大小的数组原内容,高速缓存使用的效率很低。 281 | * 用中间为作为组索引,相邻的块总是映射到不同的高速缓存行,提高高速缓存的效率。 282 | --- 283 | ## 你的计算机CPU各级Cache多大?是哪一种?C、S、E、B各是多少? 284 | 285 | --- 286 | 287 | ## LRU的算法实现?命中行Counter=0,其他行的加1,淘汰计数器最大的-请设计控制逻辑(8路)。 288 | * 算法实现: 289 | --- 290 | 291 | ## I7的CPU,L2Cache为8路的2M容量,则其B= S= E= 292 | * 8路:8路组相连,每组有8行,E = 8 293 | * I7:每块64字节,B = 64 294 | * 总容量2M,S= 2**20 / 64 / 8 = 2 ** 11 = 2048 295 | --- 296 | ## 全相联Cache有冲突不命中吗?为什么 297 | * 全相连Cache没有冲突不命中,只有容量不命中 298 | * 下一级存储器中的数据可对应本级cache中的任意一行,当本级cache未装满时,数据将被装入空白的行,而不会选择替换别的行。 299 | --- 300 | ## 访问高速缓存的地址比内存的地址要短,是吗? 301 | * 是的 302 | * 以I7为例,高速缓存地址为6(块索引)+6(组索引)+35(tag)= 47位,内存地址为64位。 303 | 304 | # QA21 305 | 306 | ## Cache的写命中策略有哪些? 307 | 308 | * 直写(write-through):当cache写命中时,处理器对Cache写入的同时,将数据写入到内存中,内存的数据和Cache中的数据都是同步的。(立即写入存储器) 309 | * 写回(write-back):当CPU对cache写命中时,只修改cache的内容不立即写入主存,只当此行被换出时才写回主存。(需要一个修改位(脏位),与内存不同时为1,表示被修改) 310 | --- 311 | ## Cache的写不命中策略有哪些? 312 | 313 | * 写分配(Write Allocate):将主存装入 Cache 中,然后更新相应单元。(好处是更多的写遵循局部性) 314 | * 非写分配(Not Write Allocate):直接写到主存中,不加载到缓存中。 315 | --- 316 | ## 为什么高速缓存用不命中率而不是命中率来衡量性能? 317 | 318 | * 命中率相差较少时,实际性能相差很多。 319 | * 命中率为97%和99%的性能可相差两倍。 320 | --- 321 | ## 怎么编写高速缓存友好的代码? 322 | 323 | 1. 加快经常性事件:专注在核心函数和内循环上 324 | 325 | 2. 使用内层循环的缓存不命中数量降到最低 326 | * 反复引用变量更好(时间局部性) 327 | * 步长为1的数据引用模式更好(空间局部性) 328 | 329 | * 关键思想:程序的局部性的这个定性概念通过对缓冲存储器的理解而量化了 330 | --- 331 | ## 重新排列以提升程序的_____局部性 332 | 333 | * 重新排列以提升程序的 **空间** 局部性 334 | --- 335 | ## 使用分块以提升程序的_____局部性 336 | 337 | * 使用块来提高 **时间** 局部性 338 | --- 339 | ## 使用分块提高缓冲器命中率的基本思想与算法 340 | 341 | * 充分利用数据的时间局部性。 342 | --- 343 | ## 新的计算机cache技术:指令Cache->微指令Cache、追踪缓存TraceCache等, TLB等 344 | 345 | * 指令Cache->微指令Cache 346 | * 追踪缓存TraceCache: 347 | * 一般一级缓存中的指令缓存都是即时解码:而追踪缓存无须每次都进行解码指令,直接做解码,这些指令称为微指令(micro-ops),12K容量能存储12000个微指令。相比可以有效地增加在高速时脉下对指令的解码能力。 348 | * TLB: 349 | * 转译后备缓冲器,也被翻译为页表缓存、转址旁路缓存,为CPU的一种缓存,由存储器管理单元用于改进虚拟地址到物理地址的转译速度。当前所有的桌面型及服务器型处理器(如 x86)皆使用TLB。TLB具有固定数目的空间槽,用于存放将虚拟地址映射至物理地址的标签页表条目。 350 | --- 351 | 352 | 353 | -------------------------------------------------------------------------------- /期末复习QA/QA22.md: -------------------------------------------------------------------------------- 1 | # Q&A 22 2 | --- 3 | ## **计算机系统的各层发生哪些异常控制?** 4 | 5 | **在硬件层**,硬件检测到事件会触发控制突然转移到异常处理程序。 6 | 7 | **在操作系统层**,内核通过上下文切换,将控制从一个用户进程转移到另一个用户进程。 8 | 9 | **在应用层**,一个进程可以发送信号到另一个进程,而接收者会将控制信号突然转移到他的一个信号处理程序。 10 | 11 | **一个程序**可以通过回避通常的栈规则,并执行其他程序中任意位置的非本地跳转来对错误作出反应。 12 | 13 | --- 14 | 15 | ## **异常处理后有哪三种返回机制?** 16 | 17 | 1)处理程序将控制返回给事件发生时正在执行的当前指令 18 | 19 | 2)处理程序将控制返回给没有发生异常将会执行的下一条指令 20 | 21 | 3)处理程序终止被中断的程序 22 | 23 | --- 24 | 25 | ## **简述异常、异常号、异常处理程序、异常表的关系?** 26 | 27 | 28 | 异常在跳转到“异常处理程序”时,是通过一张“异常表”来跳转的,每一种异常都有一个异常号(非负整数),异常表会把不同的异常号关联到不同的异常处理程序,异常表在系统启动时由操作系统初始化。 29 | 30 | #### ps: 31 | 异常是异常控制流的一种形式,是控制流中的突变,用来响应处理器状态中的某些变化,由硬件和操作系统实现 32 | 33 | 异常表:当处理器检测到有事件发生时,它会通过跳转表,进行一个间接过程调用(异常),到异常处理程序。系统启动时操作系统分配和初始化一张异常表。 34 | 35 | 异常号:系统中可能的某种类型的异常都分配了一个唯一的非负整数的异常号。异常号是到异常表中的索引,异常表的起始地址放在一个叫做异常表基址寄存器的特殊CPU寄存器里。* 36 | 37 | 38 | --- 39 | ## 一些其他知识点: 40 | 41 | 在C中通过scanf等IO函数处理IO异常。 42 | 43 | Linux系统调用中的功能号n就是异常号n 44 | 45 | 系统调用是一个特殊的异常处理子程序 46 | 47 | 异步异常需要/只需要CPU执行异常处理子程序,只需要操作系统处理即可。 48 | 49 | **浮点**除以0产生异常,导致程序终止Abort退出。 50 | 51 | **浮点数**除以0,不产生异常,程序正常执行。 52 | 任何数除以0产生异常,导致程序终止Abort退出。 53 | 每个进程的地址空间是整个内存空间的一部分 54 | -------------------------------------------------------------------------------- /期末复习QA/QA23.md: -------------------------------------------------------------------------------- 1 | # QA23 2 | 3 | 4 | 5 | 6 | 7 | ## **fork函数调用一次返回两次,怎么可能呀?** 8 | 9 | 当程序执行到下面的语句: pid=fork(); 的时候会复制父进程的堆栈段,所以两个进程都停留在fork函数中,等待返回。因此fork函数会返回两次,一次是在父进程中返回,另一次是在子进程中返回,这两次的返回值是不一样的。通过fork返回的值来判断当前进程是子进程还是父进程 10 | 11 |   1)在父进程中,fork返回新创建子进程的进程ID; 12 | 2)在子进程中,fork返回0; 13 | 3)如果出现错误,fork返回一个负值。 14 | 15 | --- 16 | 17 | 18 | 19 | ## 进程状态有哪几种?进程终止有几种方法? 20 | 21 | 22 | 23 | ##### linux上进程有5种状态: 24 | 25 | 26 | 27 | 1. 运行(正在运行或在运行队列中等待) 28 | 29 | 30 | 31 | 2. 中断(休眠中, 受阻, 在等待某个条件的形成或接受到信号) 32 | 33 | 34 | 35 | 3. 不可中断(收到信号不唤醒和不可运行, 进程必须等待直到有中断发生) 36 | 37 | 38 | 39 | 4. 僵死(进程已终止, 但进程描述符存在, 直到父进程调用wait4()系统调用后释放) 40 | 41 | 42 | 43 | 5. 停止(进程收到SIGSTOP, SIGSTP, SIGTIN, SIGTOU信号后停止运行运行) 44 | 45 | 46 | 47 | ##### 进程终止有8种 48 | 49 | 正常终止五种: 50 | 51 | 1.从main返回。 52 | 2.调用exit。 53 | 3.调用_exit或_Exit。 54 | 4.最后一个线程从其启动例程返回。 55 | 5.最后一个线程调用pthread_exit。 56 | 三种异常终止: 57 | 6.调用abort()。 58 | 7.接到一个信号并终止。 59 | 8.最后一个线程对取消请求作出响应。 60 | 61 | 62 | 63 | --- 64 | 65 | ## kill—杀死是终止进程吗?kill某进程是怎么实现的? 66 | 67 | kill 从字面来看,就是用来杀死进程的命令,但事实上,这个或多或少带有一定的误导性。从本质上讲,kill 命令只是用来向进程发送一个信号,至于这个信号是什么,是用户指定的。 68 | 69 | 也就是说,kill 命令的执行原理是这样的,kill 命令会向操作系统内核发送一个信号(多是终止信号)和目标进程的 PID,然后系统内核根据收到的信号类型,对指定进程进行相应的操作。 70 | 71 | kill 命令的基本格式如下: 72 | 73 | ```c 74 | [root@localhost ~]# kill [信号] PID 75 | ``` 76 | 77 | kill 命令是按照 PID 来确定进程的,所以 kill 命令只能识别 PID,而不能识别进程名 78 | 79 | kill 命令是按照 PID 来确定进程的,所以 kill 命令只能识别 PID,而不能识别进程名。[Linux](http://c.biancheng.net/linux_tutorial/) 定义了几十种不同类型的信号,读者可以使用 kill -l 命令查看所有信号及其编号,这里仅列出几个常用的信号,如下表所示。 80 | 81 | | 信号编号 | 信号名 | 含义 | 82 | | -------- | ------ | ------------------------------------------------------------ | 83 | | 0 | EXIT | 程序退出时收到该信息。 | 84 | | 1 | HUP | 挂掉电话线或终端连接的挂起信号,这个信号也会造成某些进程在没有终止的情况下重新初始化。 | 85 | | 2 | INT | 表示结束进程,但并不是强制性的,常用的 "Ctrl+C" 组合键发出就是一个 kill -2 的信号。 | 86 | | 3 | QUIT | 退出。 | 87 | | 9 | KILL | 杀死进程,即强制结束进程。 | 88 | | 11 | SEGV | 段错误。 | 89 | | 15 | TERM | 正常结束进程,是 kill 命令的默认信号。 | 90 | 91 | *需要注意的是,表中省略了各个信号名称的前缀 SIG,也就是说,SIGTERM 和 TERM 这两种写法都对,kill 命令都可以理解。* 92 | 93 | --- 94 | 95 | 96 | 97 | ## (*)Kill是终止进程并回收进程吗? 98 | 99 | kill命令可以加许多参数(如上面的表格),其中-2 -9 -15和不添加参数的kill是不一样的 100 | 101 | kill不带参数 即是普通的杀死进程,回收资源,- 102 | 103 | 2参数是低级别,可以被某些程序忽略,造成无法杀死进程。 104 | 105 | -9参数是强制行为,不回收资源,可能造成资源浪费。例如父进程无法被回收 106 | 107 | -15就是强制杀死进程,回收资源。 108 | 109 | ## fork的子进程与其父进程有什么相同与区别? 110 | 111 | fork之后,操作系统会复制一个与父进程完全相同的子进程,虽说是父子关系,但是在操作系统看来,他们更像兄弟关系,这2个进程共享代码空间,但是数据空间是互相独立的,子进程数据空间中的内容是父进程的完整拷贝,指令指针也完全相同,子进程拥有父进程当前运行到的位置(两进程的程序计数器pc值相同,也就是说,子进程是从fork返回处开始执行的),但有一点不同,如果fork成功,子进程中fork的返回值是0,父进程中fork的返回值是子进程的进程号,如果fork不成功,父进程会返回错误。 112 | 113 | fork出来的子进程,基本上除了进程号之外父进程的所有东西都有一份拷贝,基本就意味着不是全部,下面我们要说的是子进程从父进程那里继承了什么东西,什么东西没有继承。还有一点需要注意,子进程得到的只是父进程的拷贝,而不是父进程资源的本身。 114 | 115 | > **由子进程自父进程继承到:** 116 | > 1.进程的资格(真实(real)/有效(effective)/已保存(saved)用户号(UIDs)和组号(GIDs)) 117 | > 2.环境(environment) 118 | > 3.堆栈 119 | > 4.内存 120 | > 5.打开文件的描述符(注意对应的文件的位置由父子进程共享,这会引起含糊情况) 121 | > 6.执行时关闭(close-on-exec) 标志 (译者注:close-on-exec标志可通过fnctl()对文件描述符设置,POSIX.1要求所有目录流都必须在exec函数调用时关闭。更详细说明,参见《UNIX环境高级编程》 W. R. Stevens, 1993, 尤晋元等译(以下简称《高级编程》), 3.13节和8.9节) 122 | > 7.信号(signal)控制设定 123 | > 8.nice值 (译者注:nice值由nice函数设定,该值表示进程的优先级,数值越小,优先级越高) 124 | > 进程调度类别(scheduler class)(译者注:进程调度类别指进程在系统中被调度时所属的类别,不同类别有不同优先级,根据进程调度类别和nice值,进程调度程序可计算出每个进程的全局优先级(Global process prority),优先级高的进程优先执行) 125 | > 8.进程组号 126 | > 9.对话期ID(Session ID) (译者注:译文取自《高级编程》,指:进程所属的对话期(session)ID, 一个对话期包括一个或多个进程组, 更详细说明参见《高级编程》9.5节) 127 | > 10.当前工作目录 128 | > 11.根目录 (译者注:根目录不一定是“/”,它可由chroot函数改变) 129 | > 12.文件方式创建屏蔽字(file mode creation mask (umask))(译者注:译文取自《高级编程》,指:创建新文件的缺省屏蔽字) 130 | > 13.资源限制 131 | > 14.控制终端 132 | 133 | > **子进程所独有:** 134 | > 进程号 135 | > 1.不同的父进程号(译者注:即子进程的父进程号与父进程的父进程号不同, 父进程号可由getppid函数得到) 136 | > 2.自己的文件描述符和目录流的拷贝(译者注:目录流由opendir函数创建,因其为顺序读取,顾称“目录流”) 137 | > 3.子进程不继承父进程的进程,正文(text), 数据和其它锁定内存(memory locks)(译者注:锁定内存指被锁定的虚拟内存页,锁定后,4.不允许内核将其在必要时换出(page out),详细说明参见《The GNU C Library Reference Manual》 2.2版, 1999, 3.4.2节) 138 | > 5.在tms结构中的系统时间(译者注:tms结构可由times函数获得,它保存四个数据用于记录进程使用中央处理器 (CPU:Central Processing Unit)的时间,包括:用户时间,系统时间, 用户各子进程合计时间,系统各子进程合计时间) 139 | > 6.资源使用(resource utilizations)设定为0 140 | > 8.阻塞信号集初始化为空集(译者注:原文此处不明确,译文根据fork函数手册页稍做修改) 141 | > 9.不继承由timer_create函数创建的计时器 142 | > 10.不继承异步输入和输出 143 | > 144 | 145 | ## fork的子进程与其父进程同名的全局变量对应同一物理地址? 146 | 147 | - fork时子进程获得父进程数据空间、堆和栈的复制,所以变量的地址(当然是虚拟地址)也是一样的。 148 | - 每个进程都有自己的虚拟地址空间,不同进程的相同的虚拟地址显然可以对应不同的物理地址。因此地址相同(虚拟地址)而值不同没什么奇怪。 149 | 150 | fork子进程完全复制父进程的栈空间,也复制了页表,但没有复制物理页面,所以这时虚拟地址相同,物理地址也相同,但是会把父子共享的页面标记为“只读”,如果父子进程一直对这个页面是同一个页面,知道其中任何一个进程要对共享的页面“写操作”,这时内核会复制一个物理页面给这个进程使用,同时修改页表。而把原来的只读页面标记为“可写”,留给另外一个进程使用。 151 | 152 | 假定父进程malloc的指针指向0x12345678, fork 后,子进程中的指针也是指向0x12345678,但是这两个地址都是虚拟内存地址 (virtual memory),经过内存地址转换后所对应的 物理地址是不一样的。所以两个进城中的这两个地址相互之间没有任何关系。 153 | 154 | 155 | 156 | 就是说: 157 | 158 | **子进程继承父进程的全局变量。** 159 | 160 | **子进程创建以后,可以读取原来父进程的全局变量的值。** 161 | 162 | **但是创建以后,子进程修改了变量,或者是父进程修改了变量值,互相都不影响了。** 163 | 164 | 165 | 166 | **无论是什么类型的变量,fork后父子进程中都是一样的,但两者之间没有关系,任何一个进程修改变量后,在另一个进程中都不能知道,更不能访问另一个进程中的变量,也不会对应相同的物理地址了** 167 | 168 | 169 | 170 | *但实际上,linux为了提高 fork 的效率,采用了 copy-on-write 技术,fork后,这两个虚拟地址实际上指向相同的物理地址(内存页),只有任何一个进程试图修改这个虚拟地址里的内容前,两个虚拟地址才会指向不同的物理地址(新的物理地址的内容从原物理地址中复制得到)* 171 | 172 | --- 173 | 174 | 175 | 176 | ## (*)fork生成副本太浪费空间了,能不能先与父进程共享物理内存?特别是代码段!而且fork后还得execve覆盖掉。 177 | 178 | 早期的UNIX的fork()实现时,就是原汁原味的复制,和它的表像一样,但是很明显,这种方法效率太低,而且造成了很大的浪费,现在大部分的UNIX实现采用了如下俩种方法来规避这种浪费 179 | 180 | **(1)首先我们可以确定父子进程的代码段是相同的,所以代码段是没必要复制的,因此内核将代码段标记为只读,这样父子进程就可以安全的共享此代码段了。fork之后在进程创建代码段时,新子进程的进程级页表项都指向和父进程相同的物理页帧** 181 | 182 | **(2)而对于父进程的数据段,堆段,栈段中的各页,由于父子进程要相互独立,所以我们采用写实复制的技术,来最大化的提高内存以及内核的利用率。刚开始,内核做了一些设置,令这些段的页表项指向父进程相同的物理内存页。调用fork之后,内核会捕获所有父进程或子进程针对这些页面的修改企图(说明此时还没修改)并为将要修改的页面创建拷贝。系统将新的页面拷贝分配给被内核捕获的进程,还会对子进程的相应页表项做适当的调整,现在父子进程就可以分别修改各自的上述段,不再互相影响了** 183 | 184 | 185 | 186 | fork()后产生的子进程与父进程行为是相互独立的,变量也需要是独立的,所以说物理内存中需要有两份空间,不可以共享全部内存。 187 | 188 | ## (*)fork7去掉while(1),执行后再ps,父与子进程能看到哪一个? 189 | 190 | ## (*)fork8父进程exit为什么ps看不出父进程defunct? 191 | 192 | 193 | 194 | ## 一些知识点: 195 | 196 | **并发进程是并行执行的,控制流物理上是不相交的。** 197 | 198 | **任务管理器中的进程内是串行、进程间是并行** -------------------------------------------------------------------------------- /期末复习QA/QA24.md: -------------------------------------------------------------------------------- 1 | # QA24 2 | 3 | ### 简述hello从命令行到运行的过程 4 | 5 | 1.fork 6 | 7 | 2.execve 8 | 9 | 3.load 10 | 11 | 4.dl_init setjmp等到 _start-(所有C程序的入口点,在ctrl.o中) 12 | 13 | 5.libc_start_main(系统启动函数,在libc.so,初始化执行环境 调用main执行,处理main返回值,控制返回给内核) 14 | 15 | 6.main 16 | 17 | -->exit(0) 或 return 0 终止 18 | 19 | --->bash回收 20 | 21 | --- 22 | 23 | ### 信号与异常的区别与联系 ### 24 | 25 | **1.异常** 26 | 27 | 定义:异常就是控制流中的突变,用来响应处理器状态的某些变化 28 | 29 | **2.信号** 30 | 31 | 定义:信号是一条小消息,通知进程系统中发生了一个某种类型的时间 32 | 33 | **区别** 34 | 35 | 信号是软件上的概念,指的是由内核 产生,并由用户态程序响应的事件。 36 | 37 | 异常是硬件上的概念,指的是 CPU 因执行某些指令而产生的事件,比如常见的除零错误。另外,相对于中断,异常是同步的,中断是异步的;异常是由执行指令产生的,中断则是由硬件产生的。 38 | 39 | 信号是异步的进程间通讯机制,是在软件层次上对中断机制的一种模拟 40 | 41 | **联系** 42 | 43 | 1.硬件异常也能产生信号,例如被零除、无效内存引用等。这些条件通常先由内核硬件检测到,然后通知内核。内核将决定产生什么样的信号。 44 | 45 | 46 | 47 | 48 | 49 | --- 50 | 51 | ### 信号的处理方法有哪几种? 52 | 53 | 一般有**三种** 54 | 55 | 1.忽略此信号 56 | 57 | 2.执行该信号的默认处理动作 58 | 每个信号类型都有一个预定义的默认行为 59 | 是下面中的一种: 60 | 61 | 1. 进程终止 62 | 2. 进程终止并转储内存 63 | 3. 进程停止(挂起)直到被SIGCONT信号重启 64 | 4. 进程忽略该信号 65 | 66 | 3.提供一个信号处理函数,要求内核处理该信号时切换到用户执行这个处理函数,这种方式称为捕捉一个信号. 67 | 68 | *ps:信号机制* 69 | 70 | (1)发送信号。发送信号的程序用系统调用kill( )实现; 71 | (2)预置对信号的处理方式。接收信号的程序用signal( )来实现对处理方式的预置; 72 | (3)收受信号的进程按事先的规定完成对相应事件的处理。 73 | 74 | 75 | 76 | *ps2:信号的产生条件* 77 | 78 | 1. 用户在终端 按下某些键时,终端驱动程序会发送信号给前台进程,例如Ctrl-C产生SIGINT信 号,Ctrl-/产生SIGQUIT信号,Ctrl-Z产生SIGTSTP信号。 79 | 80 | 2. 硬件异常产生信号,这些条件由硬件检测到并通知内核,然后内核向当前进程发送适当的信号。例如当前进程执行了 除以0的指令,CPU的运算单元会产生异常,内核将这个异常解释为SIGFPE信号发送给进 程。再比如当前进程访问了非法内存地址,,MMU会产生异常,内核将这个异常解释为SIGSEGV信 号发送给进程。 81 | 82 | 3. 一个进程调用kill(2)函数可以发送信 号给另一个进程。 83 | 84 | 4. 可以用kill(1)命令发送信号给某个 进程,kill(1)命令也是调用kill(2)函 数实现的,如果不明确指定信号则发送SIGTERM信号,该信号的默认处理动作是终止进程。 85 | 86 | 5. 当 内核检测到某种软件条件发生时也可以通过信号通知进程,例如闹钟超时产生SIGALRM信 号,向读端已关闭的管道写数据时产生SIGPIPE信号。 87 | 88 | **信号的捕获过程** 89 | 90 | ![img](https://images2015.cnblogs.com/blog/899685/201701/899685-20170112142603666-965135331.png) 91 | 92 | --- 93 | 94 | ### 批处理、作业、任务、程序、进程? 95 | 96 | **批处理(**Batch),也称为批处理[脚本](https://baike.baidu.com/item/脚本)。[顾名思义](https://baike.baidu.com/item/顾名思义/1481950),批处理就是对某对象进行批量的处理,通常被认为是一种简化的[脚本语言](https://baike.baidu.com/item/脚本语言/1379708),它应用于[DOS](https://baike.baidu.com/item/DOS)和[Windows](https://baike.baidu.com/item/Windows)系统中。 97 | 98 | **作业(job)**是计算机操作者(或是一个叫做作业调度器的程序)交给操作系统的执行单位 99 | 100 | **任务**:在多道程序或多进程环境中,要由计算机来完成的基本工作元,它是由控制程序处理的一 个或多个指令序列。 101 | 102 | **程序**是一组计算机能识别和执行的指令, 103 | 104 | **进程:**是程序的一次执行,进程是可以和别的计算并行执行进程是程序在一个数据集合上运行的过程,它是系统进行资源分配和调度的一个独立单位 105 | 106 | **程序与进程之间的区别:** 107 | 108 | (1)进程更能真实地描述并发,而程序不能。 109 | (2)进程由程序和数据两部分组成,进程是竞争计算机系统有限资源的基本单位,也是进程处理机调度的基本单位。 110 | (3)程序是静态的概念;进程是程序在处理机上一次执行的过程,是动态的概念。 111 | (4)进程有生存周期,有诞生有消亡。是短暂的;而程序是相对长久的。 112 | (5)一个程序可以作为多个进程的运行程序;一个进程也可以运行多个程序。 113 | (6)进程具有创建其他进程的功能;而程序没有。 114 | 115 | **作业与进程的区别:** 116 | 117 | ​ 一个进程是一个程序对某个数据集的执行过程,是分配资源的基本单位。作业是用户需要计算机完成的某项任务,是要求计算机所做工作的集合。一个作业的完成要经过作业提交、作业收容、作业执行和作业完成4个阶段。而进程是对已提交完毕的程序所执行过程的描述,是资源分配的基本单位。其主要区别如下。 118 | (1)作业是用户向计算机提交任务的任务实体。在用户向计算机提交作业后,系统将它放入外存中的作业等待队列中等待执行。而进程则是完成用户任务的执行实体,是向系统申请分配资源的基本单位。任一进程,只要它被创建,总有相应的部分存在于内存中。 119 | (2)一个作业可由多个进程组成,且必须至少由一个进程组成,反过来则不成立。 120 | (3)作业的概念主要用在批处理系统中,像UNIX这样的分时系统中就没有作业的概念。而进程的概念则用在几乎所有的多道程序系统中。 121 | 122 | **作业、进程和程序之间的联系:** 123 | 124 | ​ 一个作业通常包括程序、数据和操作说明书3部分。每一个进程由PCB、程序和数据集合组成。这说明程序是进程的一部分,是进程的实体。因此,一个作业可划分为若干个进程来完成,而每一个进程有其实体————程序和数据集合。 125 | 126 | --- 127 | 128 | ### 异步异常/中断与信号的处理有何不同? 129 | 130 | **异常处理** 131 | 132 | 1.当发生异常时,CPU控制单元产生一个硬件出错码。 133 | 134 | 2.CPU根据该中断吗找到中断向量表内的对应向量,根据该向量转到中断处理程序。 135 | 136 | 3.中断处理程序处理完之后向当前进程发送一个SIG***信号。 137 | 138 | 4. 若进程定义了相应的信号处理程序则转移到相应的程序执行,若没有,则执行内核定义的操作。 139 | 140 | **中断处理** 141 | 142 | 1.设备产生中断 143 | 144 | 2.PIC(可编程中断控制器)会产生一个对应的中断向量 145 | 146 | 3.和中断向量表中的每一个中断向量进行比较,转到对应的中断处理程序 147 | 148 | 4.中断处理程序进行保存现场,做相关处理,恢复现场 149 | 150 | 5.内核调度,返回用户进程 151 | 152 | **信号与中断的相似点** 153 | 154 | (1)采用了相同的异步通信方式; 155 | (2)当检测出有信号或中断请求时,都暂停正在执行的程序而转去执行相应的处理程序; 156 | (3)都在处理完毕后返回到原来的断点; 157 | (4)对信号或中断都可进行屏蔽。 158 | 159 | **信号与中断的区别** 160 | 161 | (1)中断有优先级,而信号没有优先级,所有的信号都是平等的; 162 | (2)信号处理程序是在用户态下运行的,而中断处理程序是在核心态下运行; 163 | (3)中断响应是及时的,而信号响应通常都有较大的时间延迟。 164 | 165 | --- 166 | 167 | ### 有的中断/异步异常会产生信号? 168 | 169 | 硬件异常也能产生信号,例如被零除、无效内存引用等。这些条件通常先由内核硬件检测到,然后通知内核。内核将决定产生什么样的信号。 170 | **下图为异常和函数产生信号的方式以及进程的处理过程** 171 | 172 | ![](https://images2015.cnblogs.com/blog/899685/201701/899685-20170112142603666-965135331.png) 173 | 174 | --- 175 | 176 | ### 信号处理程序与中断处理程序可以各自嵌套与互相嵌套?同一信号/中断同时多次 发生呢?缺省信号可嵌套中断不嵌套 177 | 178 | 外部中断不支持嵌套处理,内部异常支持嵌套处理,原因是:内部异常程序也是有可能发生越界、越权、缺页的 179 | 180 | --- 181 | 182 | 183 | 184 | ### 一些知识点: 185 | 186 | 一般的故障类异常-内中断的处理是通过向发生异常的进程发送信号的机制实现异 常处理,可尽快完成在内核态的异常处理过程,因为异常处理过程越长,嵌套执 行异常的可能性越大,而异常嵌套执行会付出较大的代价。但缺页故障不是 187 | 188 | Linux用两个32/64位数表示信号与其阻塞 kill函数是终止进程并回收进程 189 | 190 | Linux处理“除法错”异常#DE(类型0)发送SIGFPE信号 Linux用信号处理同步异常,用中断处理异步异常(pushf) 191 | 192 | ### (*) 延时过程调用与中断服务例程 193 | 194 | -------------------------------------------------------------------------------- /期末复习QA/QA25.md: -------------------------------------------------------------------------------- 1 | # QA 25 # 2 | 3 | ## 计算机的地址空间有哪几种?怎么进行变换? 4 | 计算机的地址空间分为物理空间和虚拟空间 5 | 6 | **物理地址 (physical address):** 放在寻址总线上的地址。放在寻址总线上,如果是读,电路根据这个地址每位的值就将相应地址的物理内存中的数据放到数据总线中传输。如果是写,电路根据这个地址每位的值就将相应地址的物理内存中放入数据总线上的内容。物理内存是以字节(8位)为单位编址的。 7 | **虚拟地址 (virtual address):** CPU启动保护模式后,程序运行在虚拟地址空间中。注意,并不是所有的“程序”都是运行在虚拟地址中。CPU在启动的时候是运行在实模式的,内核在初始化页表之前并不使用虚拟地址,而是直接使用物理地址的。 8 | 9 | 通过MMU进行转换 10 | 11 | 大致过程: 12 | 1)根据VA和C2找到一级页表条目 13 | 14 | 2)若是段描述符,返回物理地址 15 | 16 | 3)若该条目是二级页表描述符,继续根据虚拟地址找下一个条目 17 | 18 | 4)找到第二个页描述符,返回物理地址 19 | 20 | ## MMU怎么将[EBP+ESI*8+4]变换成虚拟地址的? 21 | 取出%EBP,%ESI寄存器中的内容,再通过运算获取虚拟地址 22 | 23 | ##Linux是怎么处理Intel的分段管理机制的? 24 | **为什么要分段管理?** 25 | 26 | CPU内部的数据总线和段寄存器都是16位的,用他们做地址寄存器智能寻址64KB单元,但是8086的实际物理地址有1MB,如何解决这个问题呢?采用分段技术。 27 | 28 | **什么是分段技术?** 29 | 30 | 将1MB的内存空间分成若干段,每一段的第一个地址称为段首址,并且用段首址来表示这一段的内存地址。段首址必须要能被16整除,为什么呢?这样的话,段首址就可以用16位来保存,低4位全部为零,高16位可放在段寄存器中。 31 | 32 | **Linux如何进行分段?** 33 | 34 | Linux的段式管理,事实上只是“哄骗”了一下硬件而已。从2.2版开始,Linux让所有的进程(或叫任务)都使用相同的逻辑地址空间,因此就没有必要使用局部描述符表LDT。但内核中也用到LDT,那只是在VM86模式中运行Wine,因为就是说在Linux上模拟运行Winodws软件或DOS软件的程序时才使用。 35 | 36 | Intel微处理器的段机制是从8086开始提出的, 那时引入的段机制解决了从CPU内部16位地址到20位实地址的转换。为了保持这种兼容性,386仍然使用段机制,但比以前复杂得多。因此,Linux内核的设计并没有全部采用Intel所提供的段方案,仅仅有限度地使用了一下分段机制。这不仅简化了Linux内核的设计,而且为把Linux移植到其他平台创造了条件,因为很多RISC处理器并不支持段机制。但是,对段机制相关知识的了解是进入Linux内核的必经之路。 37 | 38 | 在 IA32 上任意给出的地址都是一个虚拟地址,即任意一个地址都是通过“选择符:偏移量”的方式给出的,这是段机制存访问模式的基本特点。所以在IA32上设计操作系统时无法回避使用段机制。一个虚拟地址最终会通过“段基地址+偏移量”的方式转化为一个线性地址。 但是,由于绝大多数硬件平台都不支持段机制,只支持分页机制,所以为了让 Linux 具有更好的可移植性,我们需要去掉段机制而只使用分页机制。但不幸的是,IA32规定段机制是不可禁止的,因此不可能绕过它直接给出线性地址空间的地址。万般无奈之下,Linux的设计人员干脆让段的基地址为0,而段的界限为4GB,这时任意给出一个偏移量,则等式为“0+偏移量=线性地址”,也就是说“偏移量=线性地址”。另外由于段机制规定“偏移量<4GB”,所以偏移量的范围为0H~FFFFFFFFH,这恰好是线性地址空间范围,也就是说虚拟地址直接映射到了线性地址,我们以后所提到的虚拟地址和线性地址指的也就是同一地址。看来,Linux在没有回避段机制的情况下巧妙地把段机制给绕过去了。 39 | 40 | IA32的内存寻址机制完成从逻辑地址–线性地址–物理地址的转换。其中,逻辑地址的段寄存器中的值提供段描述符,然后从段描述符中得到段基址和段界限,然后加上逻辑地址的偏移量,就得到了线性地址,线性地址通过分页机制得到物理地址。 41 | 首先,我们要明确,分段机制是IA32提供的寻址方式,这是硬件层面的。就是说,不管你是windows还是linux,只要使用IA32的CPU访问内存,都要经过MMU的转换流程才能得到物理地址,也就是说必须经过逻辑地址–线性地址–物理地址的转换。 42 | 43 | ##虚拟页面有哪几种状态? 44 | ●未分配的:VM系统还未分配(或者创建)的页。未分配的块没有任何数据和它们相关联,因此也就不占用任何磁盘空间。 45 | 46 | ●缓存的:当前已缓存在物理内存中的已分配页。 47 | 48 | ●未缓存的:未缓存在物理内存中的已分配页。 49 | 50 | ##MMU怎么分配一个新的页面(VM/PM/PTE)? 51 | 当操作系统分配一个新的虚拟内存页时,调用malloc的结果。分配过程即为在磁盘上创建空间并更新PTE中对应的页表,使它指向磁盘上这个新创建的页面 52 | 53 | ##多个进程的PTE项目可能存储同一内容? 54 | 55 | 多个虚拟地址可能映射同一个物理地址,所以可能储存同一内容 56 | 57 | ##VM是怎么支持链接与进程创建、加载运行的? 58 | 59 | **虚拟内存对链接的影响** 60 | 61 | VM能够简化链接。独立的地址空间允许每个进程的内存映像使用相同的基本格式,而不管代码和数据实际存放在物理内存的何处。一个给定的Linux 系统上的每个进程都使用类似的内存格式。对于64位地址空间,代码段总是从虚拟地址0x400000开始。数据段跟在代码段之后,中间有一段符合要求的对齐空白。栈占据用户进程地址空间最高的部分,并向下生长。这样的一致性极大地简化了链接器的设计和实现,允许链接器生成完全链接的可执行文件,这些可执行文件是独立于物理内存中代码和数据的最终位置的。 62 | 63 | **虚拟内存对fork()函数** 64 | 65 | 既然我们理解了虛拟内存和内存映射,那么我们可以清晰地知道fork函数是如何创建一个带有自己独立虚拟地址空间的新进程的。 66 | 67 | 当fork 函数被当前进程调用时,内核为新进程创建各种数据结构,并分配给它一个唯一的PID。为了给这个新进程创建虚拟内存,它创建了当前进程的mm_struct、区域结构和页表的原样副本。它将两个进程中的每个面都标记为只读,并将两个进程中的每个区域结构都标记为私有的写时复制。 68 | 69 | 当fork在新进程中返回时,新进程现在的虚拟内存刚好和调用fork 时存在的虚拟内存相同。当这两个进程中的任一个后来进行写操作时,写时复制机制就会创建新页面,因此,也就为每个进程保持了私有地址空间的抽象概念。 70 | 71 | **虚拟内存对exeve()函数:** 72 | 73 | 74 | 虚拟内存和内存映射在将程序加载到内存的过程中也扮演着关键的角色。既然已经理解了这些概念,我们就能够理解execve函数实际上是如何加载和执行程序的。假设运行在当前进程中的程序执行了如下的execve调用: 75 | 76 | execve("a. out", NULL, NULL); 77 | 78 | 正如在第8章中学到的,execve 函数在当前进程中加载并运行包含在可执行目标文件a.out 79 | 中的程序,用a.out程序有效地替代了当前程序。加载并运行a.out需要以下几个步骤: 80 | 81 | 删除已存在的用户区域。删除当前进程虚拟地址的用户部分中的已存在的区域结构。 82 | 83 | 映射私有区域。为新程序的代码、数据、bss和栈区域创建新的区域结构。所有这些 84 | 新的区域都是私有的、写时复制的。代码和数据区域被映射为a.out文件中的. text和.data区。bss 区域是请求二进制零的,映射到匿名文件,其大小包含在a.out中。栈和堆区域也是请求二进制零的,初始长度为零。图9-31概括了私有区域的不同映射。 85 | 86 | 映射共享区城。如果a.out程序与共享对象(或目标)链接,比如标准C库libc.so,那么这些对象都是动态链接到这个程序的,然后再映射到用户虚拟地址空间中 87 | 的共享区域内。 88 | 89 | 设置程序计数器(PC)。execve做的最后- -件事情就是设置当前进程上下文中的程 90 | 序计数器,使之指向代码区域的入口点。 91 | 下一次调度这个进程时, 它将从这个人口点开始执行。Linux将根据需要换人代码和数据页面。 92 | 93 | 94 | 95 | ## TLB的Block是什么内容? 96 | PPN(物理页号) 97 | 98 | ## 32/64位系统为多少字节? 99 | 这里不介绍32/64位系统各种变量到底占多少个字节 100 | 101 | 32位系统内存上限: 2^32=4G Bytes 102 | 64位系统内存上限同理可以计算为128GBytes 103 | 104 | 105 | ## TLB怎么实现VA中VP的访问(VPN/PPN/TLBI/TLBT/VPO) 106 | **如图所示** 107 | ![](https://imgsa.baidu.com/forum/w%3D580/sign=572a51db6a380cd7e61ea2e59145ad14/a75abcf0f736afc39e65910ebc19ebc4b7451202.jpg) 108 | 109 | 110 | ## 多级页表中一级页表与末级页表的表项分别是什么,每张表是不是都必须是1K个元素(32位)? 111 | 112 | 一级页表指向二级页表 113 | 末级页表中表项即为PPN 114 | 115 | 不一定 116 | ## 执行MOV EAX,[EBP+ESI*8+4]最多访存几次(TLB2级Cache3级)? 117 | 七次 118 | 119 | ## 一些知识点 120 | 121 | Intel没有只采用物理寻址的计算机系统。 122 | -------------------------------------------------------------------------------- /期末复习QA/QA26.md: -------------------------------------------------------------------------------- 1 | #QA26 2 | 3 | ## 关于Intel core i7处理器 4 | I7 5 | CPU的VA:*48*位 6 | 7 | PA:*52*位, 8 | 9 | VPO:*12*位 10 | 11 | PPO:*12*位 12 | 13 | VPN:*36*位 14 | 15 | PPN:*40*位 16 | 17 | 采用4级页表 18 | 19 | 其页表项数依次为 20 | 21 | L1 PT:每个条目512GB区域 22 | 23 | L2 PT:每个条目1GB区域 24 | 25 | L3 PT:每个条目2MB区域 26 | 27 | L4 PT:每个条目512GB区域 28 | 29 | 页表空间为5*512字节 30 | 31 | 每一页表项占5字节(40位)。 32 | 33 | TLB采用Cache类型16组,TLB-D1为4路64条, 34 | 35 | 则其TLBI*4* 位,TLBT*32*位。 36 | 37 | CacheD1为8路32K,则其64组,Block为64字节,tag为40位。 38 | 39 | PA中CT/CI/CO依次为40、6、6位。 40 | 41 | 页表物理基地址为40位,5byte 42 | 43 | ## Linux的VM机制对私有写时复制对象是怎么处理的? 44 | 45 | 由于内存映射机制,所以一个磁盘文件对象可以被多个进程共享访问,也可以被多个进程对象私有访问。如果是共享访问,那么一个进程对这个对象的修改会显示到其他进程。如果是私有访问,内核会采用写时拷贝copy on write的方式,如果一个进程要修改一个私有的写时拷贝的对象,会产生一个保护故障,内核会拷贝这个私有对象,写进程会在新的私有对象上修改,其他进程仍指向原来的私有对象。 46 | 47 | ## execve时当前进程哪些区域是请求二进制零的匿名文件映射? 48 | execve函数在当前的进程中加载并运行包含在可执行目标文件a.out中的程序,用a.out程序有效的替代了当前程序。加载并运行a.out需要以下几个步骤: 49 | 50 | 删除已存在的用户区域:删除当前进程虚拟地址的用户部分中的已存在的区域结构。 51 | 映射私有区域:为新程序的文本,数据,bss,和栈区创建新的区域结构。所有这些新的区域都是私有的,写时拷贝的。文本和数据区域被映射为a.out文件中的文本和数据区。**bss区域是请求二进制零的,映射到匿名文件,其大小包含在a.out中**。栈和堆区域也是请求二进制零的,初始长度为0. 52 | 53 | 映射共享区域:如果a.out程序和共享对象(或目标)链接,比如标准C库libc.so,那么这些对象都是动态链接到这个程序的,然后在映射到用户虚拟地址空间中的共享区域内。 54 | 设置程序计数器(PC):exevce做的最后一件事情就是设置当前进程上下文中的程序计数器,使之指向文本区域的入口点。 55 | -------------------------------------------------------------------------------- /期末复习QA/QA28.md: -------------------------------------------------------------------------------- 1 | # QA28 2 | 3 | # 垃圾回收时堆使用的内存有向图的根节点有哪几种类型? 4 | 5 | 根节点对应于这样一种不在堆中的位置,它们中包含指向堆中的指针。这些位置可以是寄存器、栈里的变量,或者是虚拟内存中读写数据区域内的全局变量。 6 | 7 | # 按照有向图与标记清除方法,所有的垃圾都可以回收?举例 8 | 9 | 非也 10 | 11 | C程序的Mark&Sweep收集器必须是保守的,其根本原因是C语言不会用类型信息来标记内存位置。因此,像int或者float 这样的标量可以伪装成指针。例如,假设某个可达的已分配块在它的有效载荷中包含一个int,其值碰巧对应于某个其他已分配块b的有效载荷中的一个地址。对收集器而言,是没有办法推断出这个数据实际上是int而不是指针。因此,分配器必须保守地将块b标记为可达,尽管事实上它可能是不可达的。 12 | 13 | # 除了CPU与RAM,计算机还有啥呀? 14 | 主板、电源、硬盘(ROM)、光驱、显卡(GPU)、网卡(网络设备)、声卡、调制解调器、软驱(老式电脑)、散热器、显示器、键盘、鼠标(输入输出设备) 15 | 16 | 17 | # CPU、存储器、所有的接口等是否都可以集成到一个芯片里? 18 | 单片机可以把微处理器、存储器和I/O接口电路等制作在一块集成电路芯片上 19 | # 删去了有关CS:APP第十章的内容 20 | -------------------------------------------------------------------------------- /期末复习QA/QA9.md: -------------------------------------------------------------------------------- 1 | # Q&A 9 2 | 3 | --- 4 | ## int a[100]; 在作为全局变量、局部变量、参数时是怎么实现对a[i]元素的访问的? 5 | 全局变量:利用%rip+偏移量访问 6 | 局部变量:利用%rbp+偏移量访问 7 | 参数:先通过%rbp+偏移量得到栈中作为参数传递的a的首地址,然后利用此地址+偏移量访问 8 | 9 | --- 10 | ## int *p;*(p++)与(*p)++实现有什么不同? 11 | 对于*(p++),是取得p+sizeof(int)地址处的值 12 | mov -0x8(%rbp),%rax 13 | add $0x4,%rax 14 | mov %rax,-0x8(%rbp) 15 | 对于(*p)++,是将p地址处的值自增1 16 | mov -0x8(%rbp),%rax 17 | mov (%rax),%eax 18 | lea 0x1(%rax),%edx 19 | mov -0x8(%rbp),%rax 20 | mov %edx,(%rax) 21 | 22 | --- 23 | ## if 分支语句的汇编语言实现与C源程序的条件判断之间是 *互补* 关系。 24 | if(y>100) 25 | 0x5555555548c9 mov -0x20(%rbp),%eax 26 | 0x5555555548cc cmp $0x64,%eax 27 | 0x5555555548cf 77 _ jle 0x5555555548d8 28 | 0x5555555548d1 movl $0xffffffff,-0x20(%rbp) 29 | 30 | --- 31 | ## switch多分支,case条件码不规律时是怎么实现的? 32 | 稀疏的Switch语句 使用决策树 (if-elseif-elseif-else) 33 | 34 | -------------------------------------------------------------------------------- /期末复习QA/qa2019.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/期末复习QA/qa2019.pdf -------------------------------------------------------------------------------- /期末考试/ics2017aa .doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/期末考试/ics2017aa .doc -------------------------------------------------------------------------------- /期末考试/ics2018aa.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/期末考试/ics2018aa.docx -------------------------------------------------------------------------------- /期末考试/计算机系统2019A参考答案 .doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzn00417/2019Fall-CSAPP/3950749f53b50e7f18076e62d6bfed6cab016e1f/期末考试/计算机系统2019A参考答案 .doc --------------------------------------------------------------------------------