An event-driven simulation system that compares First-Fit and Best-Fit memory allocation strategies in a fixed-partition multiprogramming environment.
This project simulates a large batch-processing computer installation running under fixed partition multiprogramming with real storage (non-virtual memory). It helps determine which storage placement strategy yields optimal performance by comparing First-Fit and Best-Fit allocation algorithms.
- Event-Driven Simulation: Models job arrivals, allocations, and completions
- Two Allocation Strategies:
- First-Fit: Allocates jobs to the first available partition that fits
- Best-Fit: Allocates jobs to the smallest partition that fits
- Comprehensive Performance Metrics:
- Throughput (jobs processed per time unit)
- Storage utilization statistics
- Waiting queue length analysis
- Average waiting time
- Internal fragmentation measurements
- C compiler (GCC recommended)
- Standard C libraries
- Unix-like environment (Linux, macOS) or Windows with MinGW/Cygwin
# Clone the repository
git clone https://github.com/yourusername/batch-memory-allocation-simulator.git
cd batch-memory-allocation-simulator
# Compile the program
gcc -o simulator memory_simulator.c -lm
# Run the simulation
./simulator./simulator --strategy first-fit --jobs jobs.txt --memory memory.txt./simulator --strategy best-fit --jobs jobs.txt --memory memory.txtMemory Partitions File (memory.txt):
partition_id, start_address, size
1, 0, 5000
2, 5000, 10000
3, 15000, 15000
...
Job Stream File (jobs.txt):
job_id, arrival_time, memory_required, cpu_time
1, 0, 4500, 10
2, 2, 8000, 15
3, 5, 12000, 8
...
The simulator outputs the following metrics for each strategy:
- Total jobs completed per time unit
- Completion rate over simulation period
- Percentage of partitions never used
- Percentage of partitions heavily used (>75% of time)
- Average partition utilization
- Maximum queue length
- Average queue length
- Queue length over time
- Average waiting time per job
- Maximum waiting time
- Waiting time distribution
- Total wasted memory across all allocations
- Average fragmentation per partition
- Fragmentation percentage
The simulator operates on three main event types:
-
Job Arrival Event: New job enters the system
- Check for available partition
- Allocate if possible, otherwise add to waiting queue
-
Job Completion Event: Running job finishes
- Free the partition
- Check waiting queue for next job to allocate
-
Time Tick Event: Advance simulation clock
- Update job execution times
- Update waiting times for queued jobs
When multiple jobs are waiting and a partition becomes available:
- Jobs are served strictly in First-Come, First-Served (FCFS) order
- The job at the front of the queue is evaluated first
- If it doesn't fit, the next job in queue is checked
- Process continues until a fitting job is found or queue is exhausted
Job Clocks: Track CPU time consumed by each running job
- Initialized to 0 when job starts
- Incremented each time unit
- Job completes when clock reaches requested CPU time
Wait Clocks: Track time spent in waiting queue
- Start counting when job enters queue
- Stop when job is allocated to a partition
- Used for waiting time statistics
=== FIRST-FIT SIMULATION RESULTS ===
Total Simulation Time: 1000 time units
Jobs Completed: 87
Throughput: 0.087 jobs/time unit
Storage Utilization:
- Never Used: 10% (2 partitions)
- Lightly Used (<25%): 15% (3 partitions)
- Moderately Used (25-75%): 45% (9 partitions)
- Heavily Used (>75%): 30% (6 partitions)
Queue Statistics:
- Maximum Queue Length: 12 jobs
- Average Queue Length: 4.3 jobs
Waiting Time:
- Average: 23.5 time units
- Maximum: 87 time units
Internal Fragmentation:
- Total: 45,230 memory units wasted
- Average per allocation: 520 units
- Percentage: 18.2%
Based on simulation results:
First-Fit Advantages:
- Faster allocation (O(n) worst case)
- Better for large jobs
- More predictable performance
Best-Fit Advantages:
- Better memory utilization
- Lower internal fragmentation
- More efficient for varied job sizes
- For homogeneous workloads: First-Fit often performs comparably with less overhead
- For heterogeneous workloads: Best-Fit typically yields better utilization
- For time-critical systems: First-Fit provides more consistent allocation times
- For memory-constrained systems: Best-Fit maximizes memory efficiency
Note: Results are workload-dependent. Run simulations with your specific job mix for accurate recommendations.
batch-memory-allocation-simulator/
│
├── src/
│ ├── memory_simulator.c # Main simulation engine
│ ├── event_queue.c # Event management
│ ├── partition_manager.c # Memory partition handling
│ └── statistics.c # Performance metrics calculation
│
├── include/
│ ├── simulator.h
│ ├── event_queue.h
│ ├── partition_manager.h
│ └── statistics.h
│
├── data/
│ ├── memory.txt # Sample memory configuration
│ └── jobs.txt # Sample job stream
│
├── results/
│ ├── first_fit_results.txt
│ └── best_fit_results.txt
│
├── docs/
│ └── design_document.md
│
├── tests/
│ └── test_simulator.c
│
├── Makefile
├── README.md
└── LICENSE
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/improvement) - Commit your changes (
git commit -am 'Add new feature') - Push to the branch (
git push origin feature/improvement) - Create a Pull Request
- Implement Worst-Fit allocation strategy
- Add Next-Fit algorithm
- Support dynamic partition creation
- Add visualization of memory usage over time
- Implement priority-based scheduling
- Add support for job preemption
- Create web-based visualization dashboard
This project is licensed under the MIT License - see the LICENSE file for details.
- Based on classical operating systems memory management concepts
- Inspired by fixed-partition multiprogramming systems from batch-processing era
- Educational project for understanding memory allocation strategies
- Silberschatz, A., Galvin, P. B., & Gagne, G. (2018). Operating System Concepts (10th ed.)
- Tanenbaum, A. S., & Bos, H. (2014). Modern Operating Systems (4th ed.)
For questions or suggestions, please open an issue or contact [your-email@example.com]
Note: This is an educational simulation and does not represent production-quality memory management systems.