Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip> // header to control printing output
- using namespace std;
- struct block { // memory block
- int size;
- bool is_allocated;
- };
- struct process { // processess block
- int size;
- int allocated_in;
- };
- void first_fit(block[], int, process[], int);
- void best_fit(block[], int, process[], int);
- void worst_fit(block[], int, process[], int);
- void print(block[], process[], int);
- int main() {
- const int max_blocks = 10; // max blocks and processes
- const int max_processes = 10;
- block blocks[max_blocks] = {{100, false}, {500, false}, {200, false}, {300, false}, {600, false}}; // values to be processed
- process processes[max_processes] = {{212, -1}, {417, -1}, {112, -1}, {426, -1}};
- int block_count = 5, process_count = 4; // size of chuchu
- cout << "First Fit Allocation:\n";
- first_fit(blocks, block_count, processes, process_count);
- print(blocks, processes, process_count);
- for (int i = 0; i < block_count; i++) { // restarts the memory block usage allocation before continuing to another allocation
- blocks[i].is_allocated = false;
- }
- cout << "\nBest Fit Allocation:\n";
- best_fit(blocks, block_count, processes, process_count);
- print(blocks, processes, process_count);
- for (int i = 0; i < block_count; i++) {
- blocks[i].is_allocated = false;
- }
- cout << "\nWorst Fit Allocation:\n";
- worst_fit(blocks, block_count, processes, process_count);
- print(blocks, processes, process_count);
- return 0;
- }
- void first_fit(block blocks[], int block_count, process processes[], int process_count) {
- for (int i = 0; i < process_count; i++) {
- processes[i].allocated_in = -1;
- for (int j = 0; j < block_count; j++) {
- if (!blocks[j].is_allocated && blocks[j].size >= processes[i].size) { // is like linear search
- processes[i].allocated_in = j;
- blocks[j].is_allocated = true;
- break;
- }
- }
- }
- }
- void best_fit(block blocks[], int block_count, process processes[], int process_count) {
- for (int i = 0; i < process_count; i++) {
- int best_index = -1;
- for (int j = 0; j < block_count; j++) {
- if (!blocks[j].is_allocated && blocks[j].size >= processes[i].size) {
- if (best_index == -1 || blocks[j].size < blocks[best_index].size) { // finds the best index first before allocating value
- best_index = j;
- }
- }
- }
- if (best_index != -1) {
- processes[i].allocated_in = best_index;
- blocks[best_index].is_allocated = true;
- } else {
- processes[i].allocated_in = -1;
- }
- }
- }
- void worst_fit(block blocks[], int block_count, process processes[], int process_count) {
- for (int i = 0; i < process_count; i++) {
- int worst_index = -1;
- for (int j = 0; j < block_count; j++) {
- if (!blocks[j].is_allocated && blocks[j].size >= processes[i].size) {
- if (worst_index == -1 || blocks[j].size > blocks[worst_index].size) { // same as best_fit but this use > condition in finding the n_fit to ensure it is the highest value available
- worst_index = j;
- }
- }
- }
- if (worst_index != -1) {
- processes[i].allocated_in = worst_index;
- blocks[worst_index].is_allocated = true;
- } else {
- processes[i].allocated_in = -1;
- }
- }
- }
- void print(block blocks[], process processes[], int process_count) {
- cout << "Process No." << setw(15) << "Process Size" << setw(15) << "Block No." << setw(15) << "Block Size\n"; // setw() is for setting width size
- for (int i = 0; i < process_count; i++) { // I use this instead of \t
- cout << i + 1 << setw(16) << processes[i].size << setw(24);
- if (processes[i].allocated_in != -1) {
- cout << processes[i].allocated_in + 1;
- }
- else {
- cout << "Not Allocated";
- }
- cout << setw(14) << blocks[i].size << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement