Memory Management in Operating Systems: Swapping, Paging, and Compaction, Slides of Operating Systems

Memory management deals with the ways or methods through which memory in a computer system is managed. The method or scheme of managing memory depends upon its hardware design. Memory is a large array of words or bytes with some addresses. Data read or written to the memory locations are memory address. An instruction execution cycle fetches an instruction from memory. This instruction is then decoded and may cause operands to be fetched from memory. When instruction is executed completely results may be stored back in the memory, so we will see how the operating system manages memory.

Typology: Slides

2023/2024

Available from 08/27/2024

muhammad-ali-ze0
muhammad-ali-ze0 🇵🇰

1 document

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Page 1 of 8
swap out
swap in
Backing store
User
space
OS
Process P2
Process P1
STORAGE MANAGEMENT
Memory Management
Memory management deals with the ways or methods through which memory in a
computer system is managed. The method or scheme of managing memory depends
upon its hardware design.
Memory is a large array of words or bytes with some addresses. Data read or
written to the memory locations are memory address. An instruction execution cycle
fetches an instruction from memory. This instruction is then decoded and may cause
operands to be fetched from memory. When instruction is executed completely results
may be stored back in the memory, so we will see how the operating system manages
memory.
Swapping
A process to be executed should be in the memory. A process in memory can
also be swapped out of memory temporarily to another storage e.g. hard disk and then
brought back into the memory for execution. When a time quantum expires in Round
Robin scheduling algorithm the memory manager swaps out a process and swaps in
another process.
Similarly in Priority based scheduling when a higher priority process arrives and
wants service, the memory manager swaps out the low priority process in order to load
and execute high priority process. When high priority process finishes execution, the
lower priority process will again be swapped in for execution. This is also called Roll
out/Roll-in.
Main memory
Swapping of two processes using a disk as a backing store
pf3
pf4
pf5
pf8

Partial preview of the text

Download Memory Management in Operating Systems: Swapping, Paging, and Compaction and more Slides Operating Systems in PDF only on Docsity!

swap out swap in Backing store User space OS Process P Process P

STORAGE MANAGEMENT

Memory Management

Memory management deals with the ways or methods through which memory in a computer system is managed. The method or scheme of managing memory depends upon its hardware design. Memory is a large array of words or bytes with some addresses. Data read or written to the memory locations are memory address. An instruction execution cycle fetches an instruction from memory. This instruction is then decoded and may cause operands to be fetched from memory. When instruction is executed completely results may be stored back in the memory, so we will see how the operating system manages memory.

Swapping

A process to be executed should be in the memory. A process in memory can also be swapped out of memory temporarily to another storage e.g. hard disk and then brought back into the memory for execution. When a time quantum expires in Round Robin scheduling algorithm the memory manager swaps out a process and swaps in another process. Similarly in Priority based scheduling when a higher priority process arrives and wants service, the memory manager swaps out the low priority process in order to load and execute high priority process. When high priority process finishes execution, the lower priority process will again be swapped in for execution. This is also called Roll out / Roll-in. Main memory Swapping of two processes using a disk as a backing store

Normally a swapped out process is swapped in the same memory space that it was using previously. Although, it depends upon address binding. If binding is done at assembly or load time then process, cannot be moved to different location. If binding is done at execution time, then it is possible to swap a process into different memory location. Swapping needs a backing store, that is commonly a disk etc. Disk should be large enough to hold copies of memory images of all users and should provide direct access to these memory images. The Operating System keeps information of all processes that are in memory or on the backing store. When the Scheduler decides to execute a process it calls the dispatcher. The dispatcher checks the memory regions to place the process in the memory, if there is no free memory region, the dispatcher swaps out the process currently in memory and swaps in the desired process.

Single-Partition Allocation

In Single-Partition Allocation scheme will divide memory into two partitions one is for the user and the other for the Operating System. Operating System is either kept in low or in high memory. Normally Operating System is kept in low memory. So when the Operating System is in low memory the user programs will be in high memory, and we’ll have to protect Operating System code from the user process. This protection is implemented using the Base and Limit Registers. The value of the Base should be fixed during the execution of the program, if the user addresses are bound to physical address by the use of base then it the base changes then these addresses will become invalid. So if the Base Address is changed, it should only be changed before the execution of the program. Sometimes the size of the Operating System changes, e.g. a device driver that is not in use has occupied the memory. So it is better to release that memory so that other programs can use the free space but removing the device driver from memory changes the size of operating system. 0 512K Single User System 0 512K Loading User Program into high memory OS User OS User

0 400K 1000K 2000K 2300K 2560K 0 400K 1000K P Terminates 2000K 2300K 2560K 0 400K 1000K Allocate P 1700K 2000K 2300K 2560K 0 400K P Terminates 1000K 1700K 2000K 2300K 2560K 0 400K Allocate P 900K 1000K 1700K 2000K 2300K 2560K Memory allocation using Multiple Partition Allocation Now we have a set of holes scattered throughout the memory. When a process needs memory, we search a hole in which the process can be fitted, if the hole is large than after storing the process in larger hole the remaining space is split to create a new hole. When the process terminates, it releases its memory and again a block is available where new processes can be stored again. This is the example of Dynamic-Storage Allocation i.e. how to assign/allocate memory to processes. In reality we use First-fit, Best-fit or Worst-fit algorithms to allocate free hole to processes. First-Fit Allocates the first hole that is big enough where the process that needs execution can be easily fitted. Best-Fit In Best-Fit all holes are kept in some order by size and entire list is checked to allocate the smallest hole that can be best fitted. Worst-Fit In worst fit all holes are kept in some order by size and entire list is checked to allocate the largest hole. The problem with these algorithms is that they suffer from External Fragmentation. Processes are loaded and removed from memory, and free space is broken in to pieces. External Fragmentation is that we have enough total free space to satisfy a request but it is not contiguous but it is fragmented into small holes. OS P P P OS P P OS P P P OS P P OS P P P

Compaction

Compaction is a solution for solving the problem caused by External Fragmentation. In Compaction, memory contents are shuffled to place all free memory together in one large block. Compaction is possible only if reallocation is dynamic and is done at execution time. If relocation is static and is done at load or assembly time then compaction will not be possible. Algorithms are developed that move process in order to form one large hole of memory. One technique is to move all processes towards one end of memory and move all holes in other directions to form one big hole. Another technique may be to move some processes at one end and some other at the other end to form one big hole of memory in the center. 0 300K 500K 600K 1000K 1200K 1500K 1900K 2100K 0 300K 500K 600K 800K 1200K 2100K 0 300K 500K 600K 1000K 1200K 2100K 0 300K 500K 600K 1500K 1900K 2100K original alllocaltion moved 600K moved 600K moved 600K Comparison of some different ways to compact memory OS P P 400K P 300K P 200K OS P P P P 900K OS P P P P 900K OS P P 900K P P

0 0 A 1 1 B 2 2 C 3 3 D Page Offset Page No. 1 2 3 Logical Memory 0 1 2 3 Page Table 0 0 4 Frame No. 1 L (^8) M N O P 2 12 3 16 4 (^20) A B C D 5 (^24) E F G H 6 28 7 K J I 0 5 6 1 2 0 1 4 5 E F 2 6 G 3 7 H 0 8 I 1 9 J 2 10 K 3 11 L 0 12 M 1 2 13 14 N O 3 15 P

Physical Memory Paging example for a 32-word memory with 4-word Pages We have a system that has page size of 4 words and a physical memory of 32 words (eight pages). As an example, we’ll see how the user’s view of memory can be mapped into physical memory. In the above mentioned diagram, Logical address 0 is page 0, offset 0. Indexing into the page table, we can check that page 0 is in frame 5. So, logical address 0 maps to physical address 20 that is ((5 x 4) + 0) = 20. Similarly, logical address 3 (page 0, offset 3) maps to physical address 23 i.e. (( x 4) + 3) = 23. Logical address 4 is page 1, offset 0. According to the page table, page 1 is mapped to frame 6. Logical address 4 maps to physical address 24 i.e. ((6 x 4) + 0) =

  1. Similarly, logical address 13 maps to physical address 9.