Paging-propertiesVirtual PagePageoffsetaddressSelectedaddress20 bits12 bits1SelectedpagePageunchangedtableSize=4096bytes(or4KB)W20 bits12 bitsPhysical Pageaddress26
Paging - properties 26 20 bits 12 bits Page table 20 bits 12 bits unchanged Virtual Page address Page offset Size = 4096 bytes (or 4KB) Selected page Selected address Physical Page address
Paging-properties Adjacent virtual pages are not guaranteed to bemapped to adjacent physical pages.Virtual AddressPhysical PagesContiguous virtual0x12345000addressesmapto0x543210000x12345001non-contiguous0x54321001physical address.0x12345FFF0x54321FFF0x123460000x12346001Virtual addresses0x09394000within the same pagearealwaysmapped0x12346FFF0x09394001to the same physicalContinuousaddressespage.0x09394FFF27
Paging - properties • Adjacent virtual pages are not guaranteed to be mapped to adjacent physical pages. 27 0x12345 Virtual Address 000 0x12345 001 . . 0x12345 FFF 0x12346 000 0x12346 001 . . 0x12346 FFF 0x54321 000 0x54321 001 . . 0x54321 FFF 0x09394 000 0x09394 001 . . 0x09394 FFF Continuous addresses Virtual addresses within the same page are always mapped to the same physical page. Physical Pages Contiguous virtual addresses map to non-contiguous physical address
Paging-memory allocation How to do memory allocation with paging1char *prev_ptr = NULL;2char *ptr = NULL;34void handler(int sig)(5printf("Page size = %d bytesln"6(int)(ptr-prev_ptr)):7exit(o);879int main(int argc, char **argv){10char c;11signal(SIGSEGV,handler);12I/ find the heap's start.prev_ptr=ptr=sbrk(o);13sbrk(1);// increase heap by 1 byte?14while(1)15c=*(++ptr);1628
Paging – memory allocation • How to do memory allocation with paging 28 1 char *prev_ptr = NULL; 2 char *ptr = NULL; 3 4 void handler(int sig) { 5 printf("Page size = %d bytes\n", 6 (int) (ptr - prev_ptr)); 7 exit(0); 8 } 9 int main(int argc, char **argv) { 10 char c; 11 signal(SIGSEGV, handler); 12 prev_ptr = ptr = sbrk(0); // find the heap’s start. 13 sbrk(1); // increase heap by 1 byte? 14 while(1) 15 c = *(++ptr); 16 }
Paging-memoryallocation: A page is the basic unit of memory allocationHeapAllocation489683686432768The allocationis in a28672(sa)page-by-pagemanner2457628488Thesame casefor thegrowthofthestack.1638412288819248964096819212288163842848028672327683686448960S24576Request size (bytes)29
Paging – memory allocation • A page is the basic unit of memory allocation. 29 The allocation is in a page-by-page manner. The same case for the growth of the stack
Paging-memoryallocation· Problem???- The minimum allocation unit is 4,096 bytes.Allocated Stack- But, the process cannot use that much.grow- So, the rest of the page is unused.UnallocatedZoneInternal fragmentationgrowmeans space is avoidablyUnusedAllocated Heapwasted whenallocationisdone in a page-by-pageUsed HeapBSSmanner.DataCode + Constant30
Paging – memory allocation • Problem??? – The minimum allocation unit is 4,096 bytes. – But, the process cannot use that much. – So, the rest of the page is unused. 30 Code + Constant Data BSS Allocated Heap Allocated Stack Unallocated Zone grow grow Used Heap Unused Internal fragmentation means space is avoidably wasted when allocation is done in a page-by-page manner