欢迎来到天天文库
浏览记录
ID:42648731
大小:227.84 KB
页数:8页
时间:2019-09-19
《剖析内存中的程序》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、AnatomyofaPrograminMemory剖析内存中的程序Memorymanagementistheheartofoperatingsystems;itiscrucialforbothprogrammingandsystemadministration.InthenextfewpostsI'llcovermemorywithaneyetowardspracticalaspects,butwithoutshyingawayfrominternals・Whiletheconceptsaregeneric,examplesaremostlyfromLinuxandWindowson
2、32-bitx86・Thisfirstpostdescribeshowprogramsarelaidoutinmemory.内存管理系统是操作系统的心脏;尤其对程序的运行和系统的管理至关重要。作者会在接下来的几篇文章中从实际的角度来概述下内存管理相关的内容。因为概念是相同的,所以例子大部分来自32位的X86的linux和windows系统。这篇文章主要描述一^程序在内存中的布局。Eachprocessinamulti-taskingOSrunsinitsownmemorysandbox.Thissandboxisthevirtualaddressspace,whichin32-bit
3、modeisalwaysa4GBblockofmemoryaddresses・Thesevirtualaddressesaremappedtophysicalmemorybypagetables,whicharemaintainedbytheoperatingsystemkernelandconsultedbytheprocessor.Eachprocesshasitsownsetofpagetables,butthereisacatch.Oncevirtualaddressesareenabled,theyapplytoallsoftwarerunninginthemachine,
4、includingthekernelitself.Thusaportionofthevirtualaddressspacemustbereservedtothekernel:在多任务的操作系统中,每个进程都运行在自己的内存沙箱内。这个沙箱就是虚拟内存地址空间,在32位模式下总是一个4GB的内存空间。这些虚拟地址通过页表对应到实际的物理内存,它们被操作系统的内核所维护并被处理器使用。每个进程都有它们自己的页表’但是会有一些限制。一旦虚拟地址被使用,它们会被所有运行在机器上的软件所使用,包括内核自身。因此有一块虚拟地址空间必须是留给内核专用(如下图所示,通常linux的默认配置是仁3,但
5、是,这是可配的,通常一些server,如大的关系数据库或者route可能会采取别的配比模式):LinuxUser/KernelWindowsdefaultMemorySplitmemorysplitKernelSpace(1GB)Oxffffffff0XC0000000KernelSpace(2GB)0x80000000UserModeSpace(3GB)UserModeSpace(2GB)Windowsbootedwith/3GBswitchKernelSpace(1GB)UserModeSpace(3GB)OnlyappliestoEXEsflaggedaslarge-addre
6、ssaware.Thisdoesnotmeanthekernelusesthatmuchphysicalmemory,onlythatithasthatportionofaddressspaceavailabletomapwhateverphysicalmemoryitwishes.Kernelspaceisflaggedinthepagetablesasexclusivetoprivilegedcode(ring2orlower),henceapagefaultistriggeredifuser-modeprogramstrytotouchit.InLinux,kernelspac
7、eisconstantlypresentandmapsthesamephysicalmemoryinallprocesses.Kernelcodeanddataarealwaysaddressable,readytohandleinterruptsorsystemcallsatanytime.Bycontrast,themappingfortheuser-modeportionoftheaddressspacechangeswheneveraprocess
此文档下载收益归作者所有