Lab Manual Operating System Lab Manual for Operating System Lab No. 11 Memory Management using Paging and Segmentation Objectives To understand basic concept, working and usage of memory management using paging and segmentation. 63 Department of Software Engineering Bahria University (Karachi Campus) Lab Manual Operating System LAB # 11 Memory Management using Paging and Segmentation Paging In computer operating systems, paging is one of the memory management schemes by which a computer stores and retrieves data from the secondary storage for use in main memory. In the paging memory-management scheme, the operating system retrieves data from secondary storage in samesize blocks called pages. Paging permits the physical address space of a process to be noncontiguous. The basic method for implementing paging involves breaking physical memory into fixed-sized blocks called frames and breaking logical memory into blocks of the same size called pages. When a process is to be executed, its pages are loaded into any available memory frames from their source. Algorithm 1. Input memory limit, page size, and number of processes. 2. Calculate the number of pages as memory limit/page size. a. Output number of pages. b. Initialize remaining pages with number of pages. 3. For every process a. Input the number of pages required by the process. b. If the requirement exceeds remaining pages, output “Not enough memory available.” c. Decrement remaining pages by number of pages required. d. Input page table for the process. 4. Input logical address (to find physical address), process number, page number, and offset. 5. If the process number exceeds number of processes, or the page number is greater than or equal to the allocated pages, or the offset is greater than or equal to the page size, output “Invalid entry.” 6. Else, calculate physical address by using the reference of the entry in page table and adding the offset to it, and output the physical address. Segmentation Segmentation is another memory management technique in which the logical memory is broken down into unequal sized blocks called segments. Similar to paging mechanism, segmentation allows allocation of non-contiguous memory blocks to a program. A consequence of using unequal sized blocks is that the relationship between logical and physical addresses no longer remains simple. Algorithm 1. Input memory limit, number of segments, limit and base address of each segment, and logical address. 2. If memory limit is less than base address, then output “Invalid memory limit or address.” 3. For each segment a. Input segment number and offset. b. Input values for segment table and create it. 4. If the segment number and offset are valid, computer the physical address, and output it. 64 Department of Software Engineering Bahria University (Karachi Campus) Lab Manual Operating System Memory Partitioning One of the simplest methods for memory allocation is to divide memory into several partitions. Each partition may contain exactly one process. In this multiple-partition method, when a partition is free, a process is loaded into the free partition. When the process terminates, the partition becomes available for another process. The operating system keeps a table indicating which parts of memory are available and which are occupied. When a process arrives, and needs memory, a memory section large enough for this process is provided. Placement Algorithms The OS designer must be clever in deciding how to assign processes to memory. When it is time to load a process into main memory, and if there is more than one free block of memory of sufficient size, then the operating system must decide which free block to allocate. Best-Fit Algorithm Best-fit chooses the block that is closest in size to the request. Sample Code #include<stdio.h> #define max 20 void main() { int f[max],p[max],i,j,nf,np,temp,lowest=10000; static int ff[max],pf[max]; printf("\nEnter the number of partitions:"); scanf("%d",&nf); printf("Enter the number of processes:"); scanf("%d",&np); printf("\nEnter the size of the partitions:-\n"); for(i=1;i<=nf;i++) { printf("Partition %d:",i); scanf("%d",&f[i]); } printf("Enter the size of the processes :-\n"); for(i=1;i<=np;i++) { printf("Process %d:",i); scanf("%d",&p[i]); } for(i=1;i<=np;i++) { for(j=1;j<=nf;j++) { if(ff[j]!=1) { temp=f[j]-p[i]; 65 Department of Software Engineering Bahria University (Karachi Campus) Lab Manual Operating System if(temp>=0) { if(lowest>temp) { pf[i]=j; lowest=temp; } } } } ff[pf[i]]=1; lowest=10000; } printf("\nProcess No\tProcess Size \tPartition Size"); for(i=1;i<=np && pf[i]!=0;i++) printf("\n%d\t\t%d\t\t%d \n",i,p[i],f[pf[i]]); } First-Fit Algorithm First-fit begins to scan memory from the beginning and chooses the first available block that is large enough. Next-Fit Algorithm Next-fit begins to scan memory from the location of the last placement and chooses the next available block that is large enough. Time Boxing Activity Name Login Systems + Setting up Linux Environment Walk through Theory & Tasks Implement Tasks Evaluation Time Activity Time 3 mints + 7 mints 60 mints 80 mints 30 mints Total Duration Total Time 10 mints 60 mints 80 mints 30 mints 180 mints Objectives/Outcomes • To learn segmentation and how to implement it. • To learn paging and how to implement it. • To learn page replacement algorithms. Lab Tasks/Practical Work 1. Following the code guideline of Best-Fit Placement algorithm, write the C language program for First-Fit and Next-Fit Placement algorithms. 66 Department of Software Engineering Bahria University (Karachi Campus)