Memory management is one of the most important functions of an operating system. The working of a computer system majorly depends on two components: Memory and CPU.
As far as the memory of a system is concerned there are three important criteria which an operating system has to take care.
- Size– More the memory is available more it is easy to solve complex problems.
- Access time– Less access time is desirable to execute the programs.
- Per unit Cost-Per unit cost of the memory should be less.
Practically we cannot have large memory size with less access time and less per unit cost because more search space means more time it takes to search the data. This is the reason we do not have a single type of memory in the system, we use memory hierarchy. Memory hierarchy of the system manages to fulfil the above-mentioned criteria with the help of a concept called locality of reference.
CPU can directly access main memory and registers. Because, instructions only take main memory address and registers as operands, therefore the data or any instructions must be in direct access device to be executed. CPU can access registers with one clock cycle and main memory access requires many clock cycles. Hence, cache is used to increase the access rate.
Another important task is to ensure the protection of one process data from another process. This protection is provided by the hardware. One way to do this is with the help of base register and limit register. The user process cannot access the memory address below the address specified in the limit register. The base register holds the starting physical address of the process and limit register holds the range of all addresses. For example if the base register holds 20000 and limit register is 35000, then the process can access all addresses from 20000 to 55000 (both inclusive).
The CPU hardware compares each address generated by the CPU with the value of the base register. If it is greater then or equal to the value of base register then it checks if the generated address is less than (base + limit register) value. If both the conditions are true then memory access is allowed otherwise not. (Refer PPT below)
For the CPU to execute a process, the process must be loaded into the main memory. There are two different types of address in the memory, logical address (generated by CPU) and physical address (address in the main memory). User deals with only logical addresses, therefore address translation ( or address binding) from logical to physical address must be done to locate the process in the main memory.
The address binding is done in the following ways:
- Compile Time– If the location of the process in the main memory is known at the compile time, then absolute code can be generated. For example if you know that the process will be loaded at location 10202 and will extend from here. But if at a later stage this address is occupied then the program needs to be recompiled
- Load Time– If the address is not known at the compile time, then the relocatable address is generated by the compiler and binding is delayed till load time.
- Execution Time– This type of binding is done when process is moving from one memory location to another during run-time.
Logical and Physical Address
Logical Address – An address generated by the CPU is referred to as the logical address. The set of all logical addresses comprise the Logical Address Space.
Physical Address – The address seen by the memory unit or the actual address of the physical memory (RAM) is referred to as the physical address. The set of all physical addresses comprise the Physical Address Space.
There are different ways of mapping a logical address into a physical address. One such method is as shown in Figure 3 where the value of the relocation register is added to the logical address in order to generate the physical address.
Swapping is a mechanism in which a process can be swapped temporarily out of main memory (or move) to secondary storage (disk) and make that memory available for other processes. At some later time, the system swaps back the process from the secondary storage to main memory.
Swapping is used in priority-based scheduling algorithms in order to make space for a higher priority process.
Question-Where to swap in the process?
Answer-The swapped out process can be swap back in the same physical address or different physical address. Normally a previously swapped out process will be swapped back into the same memory space, however, it depends on the type address binding method. If the address binding method is execution time-binding then a process can be swapped to a different memory space as physical address are computed during execution time.
The above discussion has been summarized in the form of a PPT with animations below.
Note: Click within the slide area for animations. Clicking on the “next” slide button will not display any animation
2 thoughts on “Memory Management”