A MMU simulator in C
MPL-2.0 License
#memman
A memory management system simulator in C
You need make and clang and be sure that your system provides lreadline
(from GNU readline) for the prompt iteration and auto-complete.
$ make
$ ./memman
If you wish to test the application:
$ make && make test
The instructions for executing the simulator are detailed in the prompt. Type ajuda
(pt-BR for 'help') if you need help.
If you wish to inspect the files, use xxd
.
For example, on initialization (w/ 64B virtual mem
):
$ /tmp xxd -g 1 mm.vir # all 64 bytes must be 255 (in b10 - 0xff in b16)
0000000: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
0000010: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
0000020: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
0000030: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
total_mem virtual_mem
t0 name tf b p1 t1 [pn tn]
...
t0 name tf b p1 t1 [pn tn]
where:
total_mem
: total physical memoryvirtual_mem
: virtual memoryt0
: time when the process arrives in the systemname
: process nametf
: time at which the process is finalizedb
: number of bytes utilized by the processpn tn
: position (in local memory) accessed by the process at the tn
instant of timeThe simulator provides a prompt ([mm]
) that lets the user insert a few commands:
carrega <filename>
: load the given filename
into the simulatorespaco <number>
: specifies the free space manager algorithmsubstitui <number>
: specifies the page substitution algorithmexecuta <interval>
: specifies the "print interval"sai
: finishes the simulationThere are 3 free space management algirhtms:
And four page substitution algorithms:
ps.: assuming that each page and page frame has a size of 16B.
The simulator creates two binary files (each time they execute), each one simulating a type of memory. They must be initialized with their respective values as specified by the trace file - see trace.
/tmp/mm.mem
/tmp/mm.vir
All of its positions must be initially intialized with -1
and later be filled with the corresponding process mem identifier (which must be unique for each process).
The MMU is responsible for translating a given virtual addres into a physical address. It does so by mapping the virtual page into a physical page and then passing (without changes) the offset along with the mapped page frame.
v_address = proc_base + proc_address;
phys_addr = MMU(v_address);
typedef struct mm_mmu_t {
VirtualPage* pages;
mm_replacement_alg;
} mm_mmu_t;
mm_mmu_t* mm_mmu_create();
mm_mmu_t* mm_mmu_destroy(mm_mmu_t*);
unsigned mm_mmu_map(mm_mmu_t*, mm_segment_t*);
void mm_mmu_unmap(mm_segment_t*);
unsigned mm_mmu_access(mm_mmu_t*, unsigned position);
typedef struct mm_vpage_t {
char r : 1;
char m : 1;
char p : 1;
unsigned phys_pos;
} mm_vpage_t;
Processes come into the system as specified by the traces input file
1.1 mm_manager_t
created with simulation info
Whenever a process comes, memory is assigned (mapped) to the process in the virtual memory.
2.1. mm_seglist_add_process(process)
: here is where free space management algorithms takes place. A segment is returned, representing the position in virtual memory that the process owns (with base
and length
)
P
bit in the virtual page trable's entry).mm_memory_assign(virtual, start, end, value)
reflects the memory representation in the memory fileWhenever the process tries to access a given memory, MMU deals with it by managing the pages as it needs to (causing page faults when necessary and using the given page replacement algorithm).
3.1. mm_mmu_map(base, length)
: creates a mapping in its internal table for the a given position in physical memory.
3.2. mm_mmu_access(position)
tries to access the given position. If it is not present, triggers the current page replecement algorithm and then re-executes its instruction.
When tf
comes, i.e, the termination time is reached, physical memory is unmapped if there are any mapping to the process' pages and the free memory management algorithm takes place again to now create a free space and completly remove the process from the virtual memory.
MPLv2