Experiment 10 (OSII)
Experiment 10 (OSII)
Aim: To Learn About different Memory Management Techniques in Unix Operating System.
Theory: Write a theory about different Memory Management Techniques in Unix Operating
System.
1. Virtual Memory:
Central to memory management in Unix is the concept of virtual memory. This technique
creates the illusion of a larger contiguous memory space for a process than physically
available RAM.
Virtual memory combines physical RAM and disk space (swap space) to provide this
illusion. The Memory Management Unit (MMU), a hardware component, translates virtual
addresses used by the process into physical memory addresses.
Benefits:
Allows processes to be larger than physical memory.
Enables running multiple processes concurrently.
Simplifies memory allocation for programmers as they don't need to worry about
physical memory constraints.
2. Paging:
A core technique used to implement virtual memory.
Divides both physical memory (RAM) and logical memory (process address space) into
fixed-size blocks called frames and pages, respectively.
A page table maintained by the MMU keeps track of the mapping between virtual pages and
physical frames.
Only the required pages of a process are loaded into RAM, promoting efficient memory
utilization.
Advantages:
Eliminates internal fragmentation, a common issue with contiguous memory
allocation.
Provides flexibility in memory allocation as pages don't need to be contiguous.
3. Swapping:
Another technique employed by Unix for memory management.
Involves temporarily moving inactive processes or entire process page sets from RAM to
swap space on disk.
This frees up physical memory for more active processes.
When a swapped-out process becomes active again, its pages are loaded back into RAM
from the swap space.
Swapping complements paging by allowing the OS to run more processes than can fit
in physical memory at once.
4. Overlays (Optional):
An older technique, less commonly used in modern Unix systems.
Suitable for very large programs that cannot fit entirely in memory.
Divides the program into smaller, logical segments called overlays.
Only the currently needed overlay is loaded into memory, and overlays are swapped in and
out as required during program execution.
5. Memory Protection:
A crucial aspect of memory management to ensure process isolation and prevent programs
from corrupting each other's memory or the kernel's memory space.
Unix utilizes memory protection mechanisms provided by the hardware (MMU) to define
read-only, read-write, and execute permissions for different memory regions.
1) Paging
#include <stdio.h>
int memory[MEMORY_SIZE];
int main() {
allocate_page(1, 0);
allocate_page(2, 1);
allocate_page(1, 2); // Simulate allocation failure
return 0;
}
2) Swapping
#include <stdio.h>
int ram[100];
int disk[200];
void swap_out(int process_id) {
// Simulate copying data from RAM to disk for process_id
printf("Swapping process %d out to disk\n", process_id);
for (int i = 0; i < 100; i++) {
disk[i + process_id * 100] = ram[i];
}
// Clear RAM for swapped-out process (not shown for simplicity)
}
int main() {
swap_out(1);
swap_in(1);
return 0;
}
//Attach two prints of output along with program one for paging and another one for
swapping
Conclusion.