CS 370 Assignment 3: Memminator! Fall 2026 Answered – Memory Management, MMU & Heap Allocation in C

$75.00

This downloadable ZIP file covers an ANSI C memory management simulation featuring virtual address translation, base and limit registers, memory protection, and first-fit heap allocation. The resource addresses simulated malloc, calloc, realloc, and free functions, block coalescing, fragmentation, process isolation, and memory boundary validation.

Original Work ?

Download Details:

  • Name: hw3-k587ae.zip
  • Type: zip
  • Size: 141.70 KB

Category: You will Instantly receive a download link upon Payment||Click Original Work Button for Custom work

Description

5/5 - (1 vote)

CS 370 Assignment 3: Memminator! Fall 2026 focuses on simulating operating system memory management using ANSI C. Students implement virtual-to-physical address translation, memory protection with base and limit registers, and heap allocation using first-fit algorithms. The assignment also explores memory fragmentation, block coalescing, process isolation, and simulated malloc, calloc, realloc, and free operations.

Assignment Description

In Shellminator, we simulated a shell and processes. In Schedulminator, we decided which simulated process
could use the CPU. In this assignment, we will give each process its own virtual address space, protect
that space from other processes, and simulate allocating memory on a process’s heap. The program remains
strictly ANSI C. It does not allocate real operating-system processes, inspect the host process’s address map,
or use paging, page tables, or swapping.
Physical Memory and the Process
Physical memory is represented by a single byte array of size 4096. Each simulated process owns one
nonoverlapping contiguous region of this array. A process stores a PID, a base register, a limit register, a
heap starting offset, and heap block metadata. The base is the physical start of its region; the limit is the
number of virtual bytes the process may address. Virtual addresses always start at zero for each process,
even when their physical bases differ.
For example, PID 1 with base 256 and PID 2 with base 1000 may both use virtual address 80. Those addresses
refer to physical bytes 336 and 1080, respectively. A process cannot name another process’s physical memory
directly: every access is interpreted relative to its own base and checked against its own limit.
Memory Management Unit (MMU)
The MMU is a small simulated component that checks addresses and translates them. For a valid virtual
byte address v, the mapping is
0 ≤ v < limit, physical = base + v.
Any virtual byte address outside this interval produces a simulated protection fault. A multi-byte access
must fit entirely within the process’s region. In particular, a valid first byte does not make a range valid if
its last byte crosses the limit. The simulator must not dereference a host pointer on an invalid access.
PID 1: base=256, limit=300
virtual 80 -> physical 336
virtual 299 -> physical 555
virtual 300 -> PROTECTION FAULT
The Heap
The process’s heap occupies the virtual interval from heap start through limit-1. The starter code tracks
heap blocks with a fixed-size metadata array. A block has an offset, a size in bytes, and a used/free flag.
The heap begins as one free block; subsequent allocations can split it into allocated and free blocks. When
neighboring blocks become free, they can be merged.
Unlike the standard C library, the simulated allocation functions return integer virtual offsets, not host
pointers. These offsets belong to one process and are meaningful only when interpreted through that process’s
MMU. INVALID (-1) represents failure. No real malloc(), calloc(), realloc(), or free() calls may be
used.
1
Understanding the Data Structures
The simulator uses three related structures. Think of Machine as the whole computer, Process as one
process’s memory configuration, and Block as a description of one portion of that process’s heap.
Block
A Block contains three integer fields:
• offset: the starting virtual address of the block, relative to its owning process; it is not a physical
address or C pointer.
• size: the number of bytes represented by the block.
• used: whether the block is allocated (nonzero) or free (zero).
For example, offset=80, size=20, used=1 describes an allocated 20-byte region at virtual offsets 80
through 99. A neighboring free block might begin at offset 100. Blocks describe heap ownership; they
do not store file contents or the actual bytes themselves.
Process
Each Process stores:
• pid: its identifying process number.
• base: the first physical-memory array index assigned to it.
• limit: the size in bytes of its virtual address space; valid individual virtual byte addresses are 0
through limit-1.
• heap start: the virtual offset where its allocatable heap begins.
• block count: how many entries in blocks are currently valid.
• blocks[MAX BLOCKS]: metadata describing the allocated and free portions of this process’s heap. Entries beyond block count are not part of the current block list.
For instance, a process with base=256, limit=300, and heap start=80 owns physical bytes 256 through
555, while its heap covers virtual offsets 80 through 299 (220 bytes). Its initial heap may be represented by
a single free block with offset 80 and size 220.
Machine
The Machine structure contains:
• memory[MEMORY SIZE]: the simulated physical memory, stored as bytes in one array.
• processes[MAX PROCESSES]: the simulated processes, each with its own base/limit and heap-block
metadata.
• count: the number of valid entries currently stored in the process array.
The relationship is therefore Machine → Process → Block. Actual bytes reside in Machine.memory, while
each process’s blocks describe which parts of its heap are in use. For example, PID 1 and PID 2 may both
allocate at virtual offset 80, yet the first may refer to physical byte 336 and the second to physical byte 1080.
The same virtual number does not mean shared storage.
2
Understanding Heap Allocation
The following examples describe observable behavior, not the algorithms you must write. Assume each
example starts with a fresh process having a 220-byte heap at virtual offsets 80 through 299, unless stated
otherwise. Calls return integer virtual offsets, not ordinary C pointers. INVALID means the requested
operation failed.
Example 1: sim malloc()
Suppose a program requests sim malloc(p,20). The result could be virtual offset 80. That means 20 bytes,
offsets 80 through 99, are reserved for this process. A later request for 30 bytes could return offset 100. The
contents of newly allocated memory are not guaranteed to be zero by sim malloc(). A request for zero bytes
fails in this simulator.
Example 2: sim free()
Suppose allocations of 20 and 30 bytes returned offsets 80 and 100. sim free(p,80) releases the first
allocation; it does not erase the second. A subsequent sim malloc(p,12) can reuse offset 80. Trying to
free offset 81 (inside a block rather than at its beginning) or freeing offset 80 a second time without a new
allocation is invalid.
Example 3: sim calloc()
sim calloc(m,p,4,3) requests space for four elements of three bytes each: 12 bytes total. Unlike sim malloc(),
all 12 allocated bytes must initially contain zero. This remains true if the allocation reuses a region previously
written by the process and then freed. The operation fails when its requested total cannot be represented
or accommodated.
Example 4: sim realloc()
Suppose sim malloc(p,20) returned offset 80 and the program stored values in those 20 bytes. sim realloc(m,p,80,40)
requests a 40-byte allocation holding the same original 20 bytes. The result may remain 80 if space permits,
or it may be a different virtual offset. In either case, the original 20 bytes of data must be preserved. If
resizing fails, the original allocation remains valid and unchanged. Shrinking from 40 bytes to 12 bytes
preserves the first 12 bytes. Resizing to zero releases the allocation and returns INVALID.
Example 5: Combining the Operations
Consider this sequence of requests for a single process:
a = sim_malloc(p, 20); /* suppose a = 80 */
b = sim_calloc(m, p, 4, 5); /* suppose b = 100 */
/* The program writes a value into the allocation at a. */
sim_free(p, a); /* offsets 80..99 are available again */
c = sim_malloc(p, 10); /* first-fit can reuse offset 80 */
b = sim_realloc(m, p, b, 30);
The allocation at b begins with 20 zeroed bytes. Resizing it to 30 bytes must preserve those 20 bytes, but
its returned offset might change. The allocation at c remains a separate live allocation and must not be
overwritten. These examples intentionally leave the choice of block management and resizing strategy to
you; the starter-code specifications determine the required behavior.
Example 6: Two Processes
PID 1 has base 256 and PID 2 has base 1000; both have heap start 80. Each may receive virtual offset 80
from sim malloc(). PID 1’s offset maps to physical 336, while PID 2’s maps to physical 1080. Writing
3
to one process’s allocation must not change the other’s memory. An address within a process’s base/limit
region may be MMU-valid even if it is not part of a currently allocated heap block: address protection and
heap-allocation bookkeeping are two different responsibilities in this simulation.
Your Work
The starter file mem handout.c contains complete @provided functions and incomplete @student functions.
Student-function comments describe observable behavior, examples, and constraints without prescribing the
algorithm or loop structure. Provided-function comments may describe implementation details. Implement
the following parts:
Part 1: Address Translation and Protection
mmu translate() translates a valid virtual byte address into a physical array index and returns INVALID
for an invalid one. mmu check range() validates a complete range, including boundary cases such as a
zero-length range at the limit. Do not use unchecked addition when testing range boundaries.
Part 2: Simulated Allocation
sim malloc() reserves the first suitable free heap block (first-fit). If a block is larger than the requested
size, split it when block-metadata capacity permits. A zero-byte request fails. Return a process-relative
heap offset.
Part 3: Releasing Heap Memory
sim free() accepts only the starting offset of a currently allocated block. It releases that block and coalesces
adjacent free blocks. Freeing a nonexistent, already freed, or interior offset must fail without corrupting the
heap.
Part 4: Zeroed Allocation
sim calloc() allocates count * size bytes and initializes every allocated byte to zero. Reject zero/negative
sizes and multiplication overflow. For example, sim calloc(m,p,4,3) requests 12 zeroed bytes in process
p.
Part 5: Resizing
sim realloc() changes the size of an existing allocation. Preserve its existing data up to the smaller of the
old and new sizes. Growing in place is allowed; otherwise, move to another heap block, copy the data, and
release the old block. A failed resize must preserve the old allocation. Resizing to zero frees the allocation
and returns INVALID. An INVALID original offset acts like allocation.
Part 6: Integration and Code Quality
Preserve the provided interfaces, compile under ANSI C/C90, keep the visible MMU/heap trace, and test
process isolation, invalid addresses, fragmentation, reuse, and failed allocations.
Visible Simulation
The supplied harness prints significant events. A typical run includes:
[OS] PID 1 base=256 limit=300; PID 2 base=1000 limit=300
[HEAP] PID 1 malloc(20) -> 80
[HEAP] PID 2 calloc(4,5) -> 80
4
[MMU] PID 1 WRITE v=80 -> p=336 value=65
[MMU] PID 2 WRITE v=80 -> p=1080 value=66
[MMU] PID 1 protection fault: vaddr 300
Note that identical virtual addresses do not imply shared physical storage.
Points
See RUBRIC.txt for the full 100-point grading breakdown and partial-credit expectations. Grading emphasizes behavior and boundary cases rather than matching the instructor’s implementation statement-forstatement.
General Notes
Build one function at a time. Test a valid translation, the exact limit boundary, allocation reuse after free,
zero initialization, resizing while preserving bytes, and an allocation failure. The provided sample run is not
the complete test suite.
Restrictions
• ANSI C/C90 only; no C++ or compiler-specific extensions.
• Do not use real malloc(), calloc(), realloc(), or free() to implement the simulated allocator.
• No paging, page tables, real process creation, system calls, host-file access, threads, or external libraries.
• Do not change the supplied function signatures or the physical-memory representation.
• All memory accesses must respect the process’s assigned physical region and virtual limits.
Specifications
Use the supplied Machine, Process, and Block structures. All allocation results are virtual offsets, and all
lengths and offsets are expressed in bytes. The starter harness creates two nonoverlapping processes; you
may add additional tests without changing the required interfaces.
Sample Run
gcc – std = c90 – pedantic – Wall – Wextra mem_handout . c -o memminator
./ memminator
Submission
Submit mem handout.c as instructed in Canvas/CodeGrade. Do not submit the instructor solution. Your
source must compile in the designated grading environment.
Agreement
By submitting, you acknowledge the stated restrictions and that the submitted implementation is your own
work. Ask the instructor or GA about unclear requirements before submission.

Solution Overview

The assignment covers implementing MMU address translation, range validation, first-fit heap allocation, block splitting, and free-block coalescing. It also addresses zero-initialized allocation, overflow detection, data-preserving reallocation, invalid memory access protection, and process isolation within a simulated 4096-byte memory array.