MEMORY MANAGEMENT -UNIT 4.pdf
Document Details
Related
- PCSII Depression/Anxiety/Strong Emotions 2024 Document
- A Concise History of the World: A New World of Connections (1500-1800)
- Human Bio Test PDF
- Vertebrate Pest Management PDF
- Lg 5 International Environmental Laws, Treaties, Protocols, and Conventions
- Fármacos Anticoncepcionais e Disfunção Erétil PDF
Full Transcript
UNIT-4 MEMORY MANAGEMENT MEMORY MANAGEMENT is the function of an operating system which handles or manages primary memory and moves processes back and forth between main memory and secondary memory during execution. Memory management keeps track of each and every memory location...
UNIT-4 MEMORY MANAGEMENT MEMORY MANAGEMENT is the function of an operating system which handles or manages primary memory and moves processes back and forth between main memory and secondary memory during execution. Memory management keeps track of each and every memory location, Regard less of either it is allocated to some process or it is free. It checks how much memory is to be allocated to processes. It decides which process will get memory at what time. It tracks whenever some memory gets freed or unallocated and Correspondingly it updates the status. Swap in: The procedure by which any process gets removed from the hard disk and placed in the main memory or RAM commonly known as Swap In. Swap out: Swap Out is the method of removing a process from the main memory or RAM and then adding it to the Hard Disk. The swapping consists of two concepts, Swapping in OS Swapping is a simple memory management method used by the operating system (O.S). This method is dedicated to increase the utilization of the process or by moving blocks of data in and out of the main memory. The data and information are swapped between the main and the secondary memory to optimize memory utilization and increase the processing power. Advantages of Swapping The process helps the CPU to manage multiple processes within the same main memory. The method helps to create and use Virtual Memory. The method is economical. Swapping makes a CPU perform several tasks simultaneously. Hence, processes do not have to wait for too long before they are executed. Memory Allocation The main memory has to accommodate both the operating system and user space. Here the user space has to accommodate various user processes. We also want these several user processes must reside in the main memory at the same time. Allocating a memory space to any process into main memory :- Memory Management Techniques are basically classified into two categories: (i) Contiguous (ii) Non-contiguous Making partitions and allocate one single contiguous block of memory to a single process Contiguous Memory Allocation Main Memory -In the Contiguous Memory Allocation, each process is contained in a single contiguous section of memory. -Whenever there is a request by the user process for the memory then a single section of the contiguous memory block is given to that process according to its requirement. Fixed-size Partition This technique is also known as Static partitioning. In this scheme, the system divides the memory into fixed- sizepartitions.Thepartitionsmayormaynotbethesamesize.Butthesizeofeachpartitionisfixedasindicatedbythenameofthetechniqueanditcannotbechanged. It is important to note that these partitions are allocated to the processes as they arrive and the partition that is allocated to the arrived process basically depends on the algorithm followed. Example Let's take an example of fixed size partitioning scheme, we will divide a memory size of 15KB into fixed-size partitions: -In this partition scheme, each partition may contain exactly one process. -The number of partitions will basically decide the number of processes. -Whenever any process terminates the n the partition becomes available for another process. Fragmentation:- The wastage of memory during memory allocation is termed as fragmentation 1. INTERNAL FRAGMENTATION 2. EXTERNAME FRAGMENTATION Suppose the size of the process is lesser than the size of the partition in that case some size of the partition gets wasted and remains unused. This wastage inside the memory (allocated block) is generally termed as Internal fragmentation. As shown in the diagram the 60 KB partition is used to load a process of 40 KB so the remaining 20 KB got wasted. External Fragmentation It is another drawback of the fixed-size partition scheme. Waste of memory due to unallocated partitions is called external fragmentation. Considering the fig, if new process arrives whose size is 12KB Then this process has to wait because there is exact 12KB unallocated memory space. Even though there are two 10KB memory space is unallocated new process cant be loaded because of contiguous nature. So, waste of memory due to unallocated partitions is called external fragmentation Advantages Simple and is easy to implement Supports multiprogramming as multiple processes can be stored inside the main memory. Disadvantages Internal Fragmentation Limitation on the size of the process If in a case size of a process is more than that of a maximum-sized partition then that process cannot be loaded into the memory. External Fragmentation Degree of multiprogramming is less As the size of the partition cannot change according to the size of the process. Thus the degree of multiprogramming is very less and is fixed. Variable-size Partition This partitioning is also known as Dynamic partitioning and is came into existence to overcome the drawback i.e. internal fragmentation that is caused by Static partitioning. In this partitioning, scheme allocation is done dynamically. The size of the partition is not declared initially. Whenever any process arrives, a partition of size equal to the size of the process is created and then allocated to the process. Thus the size of each partition is equal to the size of the process. As partition size varies according to the need of the process so in this partition scheme there is no internal fragmentation. Advantages No Internal Fragmentation As in this partition scheme space in the main memory is allocated strictly according to the requirement of the process thus there is no chance of internal fragmentation. Also, there will be no unused space left in the partition. Degree of Multiprogramming is Dynamic-As there is no internal fragmentation in this partition scheme due to which there is no unused space in the memory. Thus more processes can be loaded into the memory at the same time. No Limitation on the Size of Process - In this partition scheme as the partition is allocated to the process dynamically thus the size of the process cannot be restricted because the partition size is decided according to the process size. Disadvantages External Fragmentation In the diagram-process P1(3MB) and process P3(8MB) completed their execution. Hence there are two spaces left i.e. 3MB and 8MB. Let’s there is a Process P4 of size 15 MB comes. But the empty space in memory cannot be allocated as no spanning is allowed in contiguous allocation. Because the rule says that process must be continuously present in the main memory in order to get executed. Thus it results in External Fragmentation. Difficult Implementation The implementation of this partition scheme is difficult as compared to the Fixed Partitioning scheme as it involves the allocation of memory at run-time rather than during the system configuration. Non-Contiguous Memory Allocation- Non-contiguous memory allocation is a memory allocation technique. It allows to store parts of a single process in a non-contiguous fashion. Thus, different parts of the same process can be stored at different places in the main memory. Paging- Paging is a fixed size partitioning scheme. In paging, secondary memory and main memory are divided into equal fixed size partitions. The partitions of secondary memory are called as pages. The partitions of main memory are called as n frames. Each process is divided into parts where size of each part is same as page size. The pages of process are stored in the frames of main memory depending upon their availability. Example- Consider a process is divided into 4 pages P0, P1, P2and P3. Depending upon the availability, these pages may be stored in the main memory frames in a non-contiguous fashion as shown- In Operating Systems, Paging is a storage mechanism used to retrieve processes from the secondary storage into the main memory in the form of pages. In paging, each process is divided in the form of pages. The main memory will also be divided in the form of frames. One page of the process is to be stored in one of the frames of the memory. Pages of the process are brought into the main memory only when they are required otherwise they reside in the secondary storage. Different operating system defines different frame sizes. The sizes of each frame must be equal. Considering the fact that the pages are mapped to the frames in Paging, page size needs to be as same as frame size. Paging In Operating Systems, Paging is a storage mechanism used to retrieve processes from the secondary storage into the main memory in the form of pages. In paging, each process is divided in the form of pages. The main memory will also be divided in the form of frames. One page of the process is to be stored in one of the frames of the memory. Pages of the process are brought into the main memory only when they are required otherwise they reside in the secondary storage. Different operating system defines different frame sizes. The sizes of each frame must be equal. Considering the fact that the pages are mapped to the frames in Paging, page size needs to be as same as frame size. Let us consider them a in memory size is 16Kb and Frame size is 1KB therefore the main memory will be divided into the collection of 16 frames of 1KB each. There are 4 processes in the system that is P1,P2, P3 and P4 of 4KB each. Each process is divided into pages of 1KB each so that one page can be stored in one frame. Initially, all the frames are empty the referee pages of the processes will get stored in the contiguous way. Let us consider that, P2 and P4 are moved to waiting state after some time. Now, 8 frames become empty and the refore other pages can be loaded in that empty place. The process P5 of size 8KB (8pages) is waiting inside the ready queue. Given the fact that, we have 8 noncontiguous frames available in the memory and paging provides the flexibility of storing the process at the different places. Therefore, we can load the pages of process P5 in the place of P2 and P4. Advantages and Disadvantages of Paging Paging reduces external fragmentation, but still suffer from internal fragmentation. Paging is simple to implement and assumed as an efficient memory management technique. Due to equal size of the page sand frames, swapping becomes very easy. Page table requires extra memory space, so may not be good for a system having small RAM. Segmentation:- In Operating Systems, Segmentation is a memory management technique in which, the memory is divided into the variable size parts. Each part is known as segment which can be allocated to a process. These segments need not be placed in contiguous memory. Thus segmentation does not create internal fragmentation but it may create external fragmentation. Characteristics- Segmentation is a variable size partitioning scheme. In segmentation, secondary memory and main memory are divided into partitions of unequal size. The size of partitions depend on the length of modules. The partitions of secondary memory are called as segments. Segment Table- Segment table is a table that stores the information about each segment of the process. It has two columns. First column stores the size or length of the segment. Second column stores the base address or starting address of the segment in the main memory. Segment table is stored as a separate segment in the main memory. Segment table base register (STBR) stores the base address of the segment table. Here, Limit indicates the length or size of the segment. Base indicates the base address or starting address of the segment in the main memory. A CPU generated logical address has two parts, namely, segment number and offset. The segment number is mapped to the segment table. The segment is compared with the offset. If the offset is less than the limit address only, then the address is valid. And if the address is valid, the base address of the segment is added to the offset to get the physical address. Translating Logical Address into Physical Address- A CPU generated logical address has two parts, namely, segment number and offset. The segment number is mapped to the segment table. The segment is compared with the offset. If the offset is less than the limit address only, then the address is valid. And if the address is valid, the base address of the segment is added to the offset to get the physical address. Advantages of Segmentation 1. No internal fragmentation 2. Average Segment Size is larger than the actual page size. 3. Less over head 4. It is easier to relocate segments than entire address space. 5.The segment table is of lesser size as compare to the page table in paging. Disadvantages of Segmentation 1. It can have external fragmentation. 2. it is difficult to allocate contiguous memory to variable sized partition. 3.Costly memory management algorithms. VIRTUAL MEMORY:- -Virtual Memory is a storage allocation scheme in which secondary memory can be addressed as though it were part of main memory. -Virtual memory uses both hardware and software to operate. -When an application is in use, data from that program is stored in a physical address using RAM. A memory management unit (MMU) maps the address to RAM and automatically translates addresses. The MMU, map a logical address space to a corresponding physical address. If, at any point, the RAM space is needed for something more urgent, data can be swapped out of RAM and into virtual memory. -The computer's memory manager is in charge of keeping track of the shifts between physical and virtual memory. If that data is needed again, the computer's MMU will use a context switch to resume execution. -While copying virtual memory into physical memory, the OS divides memory with a fixed number of addresses into either page files or swap files. Each page is stored on a disk, and when the page is needed, the OS copies it from the disk to main memory and translates the virtual addresses into real addresses. Demand Paging:- Demand Paging : The process of loading the page into memory on demand (whenever page fault occurs) is known as demand paging. The process includes the following steps: 1. If CPU try to refer a page that is currently not available in the main memory, it generates an interrupt indicating memory access fault. 2. The OS puts the interrupted process in a blocking state. For the execution to proceed the OS must bring the required page into the memory. 3. The OS will search for the required page in the logical address space. 4. The required page will be brought from logical address space to physical address space. The page replacement algorithms are used for the decision making of replacing the page in physical address space. 5. The page table will updated accordingly. 6. The signal will be sent to the CPU to continue the program execution and it will place the process back into ready state. Hence whenever a page fault occurs these steps are followed by the operating system and the required page is brought into memory. Page Replacement:- In an operating system that uses paging for memory management, a page replacement algorithm is needed to decide which page needs to be replaced when a new page comes in. Page Fault–A page fault happens when a running program accesses a memory page that is mapped into the virtual address space, but not loaded in physical memory. Since actual physical memory is much smaller than virtual memory, page faults happen. In case of page fault, the Operating System might have to replace one of the existing pages with the newly needed page. Different page replacement algorithms suggest different ways to decide which page to replace. The target for all algorithms is to reduce the number of page faults. Page Replacement Policies 1. First In First Out (FIFO) 2. Optimal Page Replacement 3. Least Recently Used (LRU) 4. Least Frequently Used (LFU) Page Replacement Policies -First In First Out (FIFO) –This is the simplest page replacement algorithm. In this algorithm, the operating system keeps track of all pages in the memory in a queue, the oldest page is in the front of the queue. When a page needs to be replaced, the page in the front of the queue is selected for removal. Page Replacement Policies -Optimal Page replacement –In this algorithm, pages are replaced which would not be used for the longest duration of time in the future. Optimal page replacement is perfect, but not possible in practice as the operating system cannot know future requests. The use of Optimal Page replacement is to set up a benchmark so that other replacement algorithms can be analyzed against it. Page Replacement Policies -Least Recently Used –In this algorithm page will be replaced with which is least recently used.