
OperatingSystemsCh7Memory Managementfrom a Programmer's Perspective1
Operating Systems Ch7 Memory Management from a Programmer’s Perspective 1

Why weneed memory management. The running program code requires memory- Because the CPU needs to fetch the instructions fromthe memoryforexecution We must keep several processes in memory-Improveboth CPUutilization and responsivenessMultiprogrammingIt is required to efficientlymanagethememory2
Why we need memory management • The running program code requires memory – Because the CPU needs to fetch the instructions from the memory for execution • We must keep several processes in memory – Improve both CPU utilization and responsiveness – Multiprogramming 2 It is required to efficiently manage the memory

TopicsinCh7Fromaprogrammer's perspective:user-spacememory managementWhat is the address space of a process?Howare the program code and data stored in memory?Howtoallocate/freememory (malloc()+free())?How much memory can be used in a program?What are segmentation and segmentation fault?From the kernel's perspective:Howto managethe memoryWhat is virtual memory?How to realize address mapping (paging)?Howto support very large programs (demand paging)?How to do page replacement?Whatis TLB?What is memory-mapped file?3
Topics in Ch7 3 What is the address space of a process? How are the program code and data stored in memory? How to allocate/free memory (malloc() + free())? How much memory can be used in a program? What are segmentation and segmentation fault? From a programmer’s perspective: user-space memory management What is virtual memory? How to realize address mapping (paging)? How to support very large programs (demand paging)? How to do page replacement? What is TLB? What is memory-mapped file? From the kernel’s perspective: How to manage the memory

Part 1:User-space memoryLocal variableDo you rememberthis?Contentof aprocess(inuser-spacememory)Dynamically-allocatedmemoryHowdoes each part usethe memory?Fromaprogrammer'sperspectiveGlobal variableLet's forgetabout thekernel for amoment.We are goingto explore theCode +user-spacememoryfirst.constantsProcess
4 Part 1: User-space memory Global variable Local variable Dynamically-allocated memory Code + constants Process Do you remember this? - Content of a process (in user-space memory) How does each part use the memory? - From a programmer’s perspective Let’s forget about the kernel for a moment. We are going to explore the user-space memory first

User-space memory management. Address space;Code & constantsData segment;Stack;Heap;Segmentation fault5
5 User-space memory management - Address space; - Code & constants; - Data segment; - Stack; - Heap; - Segmentation fault;

AddressspaceHow does a programmerlook at the memory space?Stack - Local variablesAn array of bytes?Heap -DynamicallyMemoryofaprocessisallocated memorydivided into segmentsData Segment & BsSThis way of arrangingGlobal and staticmemoryis calledvariablessegmentationCode + Constant6
Address space 6 Data Segment & BSS – Global and static variables Heap – Dynamically allocated memory Code + Constant Stack - Local variables How does a programmer look at the memory space? - An array of bytes? - Memory of a process is divided into segments - This way of arranging memory is called segmentation

Addressspaceintmain(void)(int *malloc ptr=malloc(4);What is theprocess address space?char *constant ptr -"hello";printf("Localvariable=%15pln,&malloc_ptr);printf("malloc()space%15p/n,malloc_ptr);Increasingprintf("Global variable=%15pn",&global int):printf("Code &constant-%15pn",constant ptr);addressStack - Local variablesreturn o;$/addrHeap -DynamicallyLocal variable=0xbfa8938callocated memorymalloc()space0x915c008-Global variable=0x804a0200x8048550Code &constant=Data Segment &BssGlobal and staticvariablesNoteTheaddresses are not necessarilytheCode + Constantsamein different processes7
Address space 7 Data Segment & BSS – Global and static variables Heap – Dynamically allocated memory Code + Constant Stack - Local variables Increasing address $ ./addr Local variable = 0xbfa8938c malloc() space = 0x915c008 Global variable = 0x804a020 Code & constant = 0x8048550 $ _ Note The addresses are not necessarily the same in different processes What is the process address space?

AddressspaceHowlargeistheaddress space?oxf=111111bit+16'bitsIncreasingoxffffffff0x100000000addressStack - Local variables2^324GHeap-Dynamicallyallocated memoryIna32-bitsystem,OneaddressmapstoonebyteThe maximum amount of memoryDataSegment &Bssin a process is 4GB.Global and staticNotevariablesThisis the so called logical addressspaceCode + ConstantEachprocesshasitsownaddressspace, and it can reside in any partof the physical memory8
8 Address space Data Segment & BSS – Global and static variables Heap – Dynamically allocated memory Code + Constant Stack - Local variables 0xffffffff = 0x100000000 - 1 1 ‘1’ bit + 16 ‘0’ bits = 2^32 - 1 = 4G - 1 0xf = 1111 In a 32-bit system, - One address maps to one byte. - The maximum amount of memory in a process is 4GB. Increasing address Note - This is the so called logical address space - Each process has its own address space, and it can reside in any part of the physical memory How large is the address space?

User-space memory managementAddress space; Code & constants;Data segment;Stack;Heap;Segmentation fault:9
9 User-space memory management - Address space; - Code & constants; - Data segment; - Stack; - Heap; - Segmentation fault;

Program code&constants·A program is an executable fileStackAprocessisnotboundedtooneprogram codeHeap- Remember exec*() family?Data SegmentThe program code requires&BSSmemory space because...- The CPU needs to fetch theCodeConstantinstructions from the memoryforexecution.(intel)instructionCore"i710
Program code & constants • A program is an executable file • A process is not bounded to one program code. – Remember exec*() family? • The program code requires memory space because. – The CPU needs to fetch the instructions from the memory for execution. 10 Data Segment & BSS Heap Code + Constant Stack instruction