1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
#include <iostream> #include <vector> #include <iomanip> class FileSystem { public: FileSystem(int totalBlocks) : blocks(totalBlocks, false) {} // Contiguous Allocation bool allocateContiguous(int fileSize, int& startBlock) { for (int i = 0; i <= blocks.size() - fileSize; ++i) { bool canAllocate = true; for (int j = i; j < i + fileSize; ++j) { if (blocks[j]) { canAllocate = false; break; } } if (canAllocate) { startBlock = i; for (int j = i; j < i + fileSize; ++j) { blocks[j] = true; } return true; } } return false; } // Linked Allocation bool allocateLinked(const std::vector<int>& fileBlocks, std::vector<int>& fileTable) { fileTable.clear(); for (int block : fileBlocks) { if (block >= blocks.size() || blocks[block]) { return false; } blocks[block] = true; fileTable.push_back(block); } return true; } // Indexed Allocation bool allocateIndexed(const std::vector<int>& fileBlocks, std::vector<int>& indexTable) { indexTable.clear(); for (int block : fileBlocks) { if (block >= blocks.size() || blocks[block]) { return false; } blocks[block] = true; indexTable.push_back(block); } return true; } void printBlocks() const { std::cout << "Block Status:\n"; for (int i = 0; i < blocks.size(); ++i) { std::cout << "Block " << i << ": " << (blocks[i] ? "Allocated" : "Free") << "\n"; } } private: std::vector<bool> blocks; }; int main() { int totalBlocks; std::cout << "Enter total number of blocks in the file system: "; std::cin >> totalBlocks; FileSystem fs(totalBlocks); int choice; std::cout << "Select allocation method:\n"; std::cout << "1. Contiguous Allocation\n"; std::cout << "2. Linked Allocation\n"; std::cout << "3. Indexed Allocation\n"; std::cout << "Enter choice (1-3): "; std::cin >> choice; if (choice == 1) { int fileSize, startBlock; std::cout << "Enter file size for contiguous allocation: "; std::cin >> fileSize; if (fs.allocateContiguous(fileSize, startBlock)) { std::cout << "File allocated starting at block " << startBlock << "\n"; } else { std::cout << "Allocation failed. Not enough contiguous space.\n"; } } else if (choice == 2) { int numBlocks; std::cout << "Enter number of blocks for linked allocation: "; std::cin >> numBlocks; std::vector<int> fileBlocks(numBlocks); std::cout << "Enter blocks for linked allocation: "; for (int& block : fileBlocks) { std::cin >> block; } std::vector<int> fileTable; if (fs.allocateLinked(fileBlocks, fileTable)) { std::cout << "File allocated with linked allocation.\n"; std::cout << "File Table: "; for (int block : fileTable) { std::cout << block << " "; } std::cout << "\n"; } else { std::cout << "Allocation failed. Invalid blocks or already allocated.\n"; } } else if (choice == 3) { int numBlocks; std::cout << "Enter number of blocks for indexed allocation: "; std::cin >> numBlocks; std::vector<int> fileBlocks(numBlocks); std::cout << "Enter blocks for indexed allocation: "; for (int& block : fileBlocks) { std::cin >> block; } std::vector<int> indexTable; if (fs.allocateIndexed(fileBlocks, indexTable)) { std::cout << "File allocated with indexed allocation.\n"; std::cout << "Index Table: "; for (int block : indexTable) { std::cout << block << " "; } std::cout << "\n"; } else { std::cout << "Allocation failed. Invalid blocks or already allocated.\n"; } } else { std::cout << "Invalid choice.\n"; } fs.printBlocks(); return 0; } |
Explanation
- Class
FileSystem
:- Purpose: Simulates a file system with different file allocation methods.
- Attributes:
blocks
: Vector indicating the status of each block (allocated or free).
- Methods:
allocateContiguous(int fileSize, int& startBlock)
: Allocates contiguous blocks for a file, if possible, and sets the start block.allocateLinked(const std::vector<int>& fileBlocks, std::vector<int>& fileTable)
: Allocates blocks for a file using linked allocation, maintaining a file table.allocateIndexed(const std::vector<int>& fileBlocks, std::vector<int>& indexTable)
: Allocates blocks for a file using indexed allocation, maintaining an index table.printBlocks()
: Prints the status of all blocks in the file system.
- Main Function:
- Setup: Prompts the user for the total number of blocks in the file system.
- User Interaction: Allows the user to select an allocation method and input file blocks or size.
- Allocation: Executes the selected allocation method and prints the result.
- Block Status: Displays the final status of all blocks in the file system.
Usage
- File Allocation Simulation: Demonstrates three basic file allocation methods: contiguous, linked, and indexed.
- User Interaction: Provides options for different allocation methods and displays the outcome of the allocation process.