Implementation of file allocation methods using vectors

This is a type of allocation in which a file occupies contiguous blocks of a given memory. This type of allocation is fastest because we can access any part of the file just by adding it to the starting index of the file. However, this allocation is not useful when you have to insert a file whose size is greater than the greatest empty slot available.

Below is the implementation of Contiguous File Allocation which follows First-Fit Allocation:

C++

// C++ implementation of the Contiguous // File Allocation which follow First-Fit using namespace std; // File Class class File < // Name of File string filename; // Size of file size_t size; // Partition no. is which part of // file is present at a particular // block of memory int partition; class Block < // If Block is occupied // by a file or not bool occupied = false ; // File of the block // This function will set file into // current block and set occupied flag void set_file(File file) this ->file = file; occupied = true ; // This function will return // filename of given block string get_file_name() return file.filename; // This function will return // partition number of file int get_file_partition_no() return file.partition; // This function will return // if the block is empty or not bool is_empty() return !occupied; // This function will set the occupied // flag as false meaning that it will // free the given block void set_empty() occupied = false ; // Function to return the number of // empty Blocks from given memory int get_empty_count(vector memory) int sum = 0; vector::iterator slot; // Count no. of empty blocks in // given memory for (slot = memory.begin(); slot != memory.end(); slot++) < sum += (*slot).is_empty(); return sum; // Function will return if the file // exists in a given memory bool file_exists(vector memory, string name) vector::iterator slot; for (slot = memory.begin(); slot != memory.end(); slot++) < if (!(*slot).is_empty() && (*slot).get_file_name() == name) < return true ; return false ; // Function will set the file in the memory // this will follow first fit allocation void set_contiguous_memory(vector* memory, vector< int >* index, bool avail = false ; int i = 0, count = 0, main_index; vector::iterator slot; // Check if the file already exists if (file_exists((*memory), file.filename)) cout << "File already exists" // Iterate through full memory for (slot = (*memory).begin(); slot != (*memory).end(); slot++) < // Check if there are // contiguous Blocks available if ((*slot).is_empty()) < // This if condition will note // the 1st index for each // Block empty if (count == 1) main_index = i; // Check if contiguous // Blocks are available, // it will set our flag // avail as true and break if (count == file.size) < avail = true ; // Else if there is even a // single non-empty block // in between we will set // count as 0 // If our flag is set, // this condition will set // Here starting index of // our given file will be // pushed in index page (*index).push_back(main_index); // Utilize count variable // again for setting file for ( int i = main_index; i < main_index + file.size; file.partition = count; (*memory).at(i).set_file(file); cout << "File " << file.filename << " has been successfully" cout << "The size of the file is" << " greater than" cout << "the greatest slot available" << " in contiguous memory" cout << "Hence, File " << file.filename << " cannot be allocated" // Function to Delete file from memory void delete_contiguous_mem(vector* memory, vector< int >* index_page, string file) vector< int >::iterator slot; int index, i = 0, main_index; // Check if the file exist or not if (!file_exists((*memory), file)) cout << "File does not exist" << endl; // Iterate all the indexes for (slot = (*index_page).begin(); slot != (*index_page).end(); slot++) < // If specified file is found, // this condition will // set the value of index no. // in index and // main_index will have starting // location of // file in memory if ((*memory).at(*slot).get_file_name() main_index = (*slot); // Iterate through the main index until // filename is similar to specified // filename and status of Block is i = main_index; while (i < (*memory).size() && (*memory).at(i).get_file_name() && !(*memory).at(i).is_empty()) < // Set the Block as empty (*memory).at(i).set_empty(); // Erase entry of file from index page (*index_page).erase((*index_page).begin() + index); cout << "File " << file << " has been successfully deleted" // Function to display main index page void show_contiguous_index(vector memory, vector < int >index_page) int max = 9, i, j; vector::iterator slot; string fname; // Iterate through all index pages for (i = 0; i < index_page.size(); if (memory.at(index_page.at(i)) .get_file_name() // Get the length of file max = memory.at(index_page .get_file_name() cout << "+" << string(max + 2, '-' ) << string(max / 2 << string(max / 2 - 3, ' ' ) << "| Start Address | " << " End Address | Size" << " of the file |\n+" << string(max + 2, '-' ) // Iterate index_pages for (i = 0; i < index_page.size(); << string(max / 2 + max % 2 .at(index_page .get_file_name() .at(index_page .get_file_name() << memory.at(index_page.at(i)) .get_file_name() << string(max / 2 .at(index_page .get_file_name() - to_string(index_page - to_string(index_page << index_page.at(i) - to_string(index_page j = index_page fname = memory .get_file_name(); // Till j is less than memory size while (j < memory.size() .get_file_name() // Print the index pages details cout << string(7 - to_string(j) - to_string(j) - to_string(j) - to_string(j - index_page - to_string(j - index_page << j - index_page.at(i) + 1 - to_string(j - index_page cout << "+" << string(max + 2, '-' ) // Function to display index of each // partition of specified file void show_contiguous_indexes(vector memory, vector < int >index_page, string filename) int index, i; vector< int >::iterator slot; // If file exist then display file // index and partition if (file_exists(memory, filename)) < cout << "\n| Current Location |" << " Partition Number |" ; // Iterate through all the index for (slot = index_page.begin(); slot != index_page.end(); slot++) < if (memory.at(*slot).get_file_name() index = (*slot); // Loop till memory size is greater than // index and file is allocated while (index < memory.size() .get_file_name() - to_string(index) - to_string(index) - to_string(index) - to_string(memory _partition - to_string(memory _partition .get_file_partition_no() - to_string(memory .get_file_partition_no()) cout << "File does not exist " << "in given memory" // Driver Code // Declare memory of size 16 Blocks vector memory(16); // Declare index page vector < int >index_page; cout << "Remaining memory :- " << get_empty_count(memory) // Set the data temp.filename = "home.txt" ; temp.size = 5; set_contiguous_memory(&memory, &index_page, temp.filename = "Report.docx" ; temp.size = 6; set_contiguous_memory(&memory, &index_page, temp.filename = "new_img.png" ; temp.size = 3; set_contiguous_memory(&memory, &index_page, temp.filename = "test.cpp" ; temp.size = 2; set_contiguous_memory(&memory, &index_page, cout << "Remaining memory :- " << get_empty_count(memory) // Function call to show all the // memory allocation show_contiguous_index(memory, index_page); cout << "Now we will check each partition of" ; cout << " Report.docx and test.cpp before" cout << "deleting them to see" << " which locations " ; cout << "are going to be set free" << " as our slots" // Function call to show all the // memory allocation show_contiguous_indexes(memory, index_page, "Report.docx" ); // Function call to show all the // memory allocation show_contiguous_indexes(memory, index_page, // Now delete Report.docx & test.cpp delete_contiguous_mem(&memory, &index_page, "Report.docx" ); delete_contiguous_mem(&memory, &index_page, cout << "Remaining memory :- " << get_empty_count(memory) // Function call to show all the // memory allocation show_contiguous_index(memory, index_page); // Creating hello.jpeg file now // and setting it to memory temp.filename = "hello.jpeg" ; temp.size = 8; set_contiguous_memory(&memory, &index_page, cout << "Check index page: " << endl; // Function call to show all the // memory allocation show_contiguous_index(memory, index_page); Output:
Remaining memory :- 16 File home.txt has been successfully allocated File Report.docx has been successfully allocated File new_img.png has been successfully allocated File test.cpp has been successfully allocated Remaining memory :- 0 +-------------+---------------+-------------+------------------+ | File Name | Start Address | End Address | Size of the file | +-------------+---------------+-------------+------------------+ | home.txt | 0 | 4 | 5 | | Report.docx | 5 | 10 | 6 | | new_img.png | 11 | 13 | 3 | | test.cpp | 14 | 15 | 2 | +-------------+---------------+-------------+------------------+ Now we will check each partition of Report.docx and test.cpp before deleting them to see which locations are going to be set free as our slots File Name = Report.docx +------------------+------------------+ | Current Location | Partition Number | +------------------+------------------+ | 5 | 0 | | 6 | 1 | | 7 | 2 | | 8 | 3 | | 9 | 4 | | 10 | 5 | +------------------+------------------+ File Name = test.cpp +------------------+------------------+ | Current Location | Partition Number | +------------------+------------------+ | 14 | 0 | | 15 | 1 | +------------------+------------------+ File Report.docx has been successfully deleted File test.cpp has been successfully deleted Remaining memory:- 8 +-------------+---------------+-------------+------------------+ | File Name | Start Address | End Address | Size of the file | +-------------+---------------+-------------+------------------+ | home.txt | 0 | 4 | 5 | | new_img.png | 11 | 13 | 3 | +-------------+---------------+-------------+------------------+ The size of the file is greater than the greatest slot available in contiguous memory Hence, File hello.jpeg cannot be allocated Check index page: +-------------+---------------+-------------+------------------+ | File Name | Start Address | End Address | Size of the file | +-------------+---------------+-------------+------------------+ | home.txt | 0 | 4 | 5 | | new_img.png | 11 | 13 | 3 | +-------------+---------------+-------------+------------------+

2. Indexed File Allocation Method:

This is a type of allocation where we have 2 index pages, one is the main index page w.r.t all files and the other is a sub-indexed page which is w.r.t to a particular file. This type of allocation does not require a contiguous slot of memory since we are keeping track of each block individually. In the given program below, the vector of vectors is used for implementing the main index and sub-index.

Below is the implementation for Indexed File Allocation Method:

C++

// C++ implementation of the Indexed // File Allocation Method using namespace std; // File Class class File < // Name of File string filename; // Size of file size_t size; // Partition no. is which part of // file is present at a particular // block of memory int partition; // Block class class Block < // If Block is occupied // by a file or not bool occupied = false ; // File of the block // This function will set file // into current block // and set occupied flag void set_file(File file) this ->file = file; occupied = true ; // This function will return // filename of given block string get_file_name() return file.filename; // This function will return // partition number of file int get_file_partition_no() return file.partition; // This function will return // if the block is empty or not bool is_empty() return !occupied; // This function will set the // occupied flag as false // meaning that it will free // the given block void set_empty() occupied = false ; // Function to return the number of // empty Blocks from given memory int get_empty_count(vector memory) int sum = 0; vector::iterator slot; // count no. of empty blocks in // given memory for (slot = memory.begin(); slot != memory.end(); slot++) sum += (*slot).is_empty(); return sum; // This function will generate random indexes // from empty memory slots to test indexing int generate_index(vector memory) int index = -1; // Check if memory is full if (!get_empty_count(memory) == 0) < // Here it will generate index until // the memory block at generated // index is found to be empty index = rand () % memory.size(); index = abs (index); > while (!memory.at(index).is_empty()); return index; // This function will return if the file // exists in a given memory bool file_exists(vector memory, string name) vector::iterator slot; for (slot = memory.begin(); slot != memory.end(); slot++) < if (!(*slot).is_empty() && (*slot).get_file_name() == name) < return true ; return false ; // This function will set file in // memory and push index for each partition // and then push the file index to main // index vector void set_indexed_memory(vector* memory, vector >* index_page, // Declaration newpage to set the // index page for given file vector < int >newpage; // Check if file exists if (file_exists((*memory), file.filename)) cout << "File already exists" << endl; // Check if available memory is greater than // size of given file if (get_empty_count(*memory) >= file.size) < // Iterate till file size for ( int i = 0; i < file.size; i++) < // Generate random empty index index = generate_index(*memory); // Set file partition file.partition = i; // Push the file to memory (*memory).at(index).set_file(file); // Push the index to newpage newpage.push_back(index); // Push new index page into main // index page (*index_page).push_back(newpage); cout << "File " << file.filename << " has been successfully allocated" cout << "Not enough available space" // Function to delete a file from given // indexed memory void delete_from_indexed_memory(vector* memory, vector >* index_page, string file) vector< int >::iterator slot; vector >::iterator it; int index, i = 0; // Check if file exists if (file_exists((*memory), file)) < // Iterate main index for (it = (*index_page).begin(); it != (*index_page).end(); it++) < // Check for sub-index at // start location slot = (*it).begin(); // Check if it equals filename .get_file_name() // Set for index and break // Set the memory flag as empty for (slot = (*index_page).at(index).begin(); slot != (*index_page).at(index).end(); (*memory).at(*slot).set_empty(); // Erase file index from main index page (*index_page) .erase((*index_page).begin() + index); cout << "File " << file << " has been successfully deleted" cout << "File does not exist" // Function to display the main index void show_indexed_index(vector memory, vector > index_page) int max = 9; vector::iterator slot; vector >::iterator it; // Iterate over index pages for (it = index_page.begin(); it != index_page.end(); it++) < .get_file_name() max = memory .get_file_name() cout << "+" << string(max + 2, '-' ) << string(max / 2 + max % 2 - 4, ' ' ) << string(max / 2 - 3, ' ' ) << "| Start Address | End Address" << " | Size of the file |\n+" << string(max + 2, '-' ) // Iterate through all the index pages for (it = index_page.begin(); it != index_page.end(); it++) < << string(max / 2 .get_file_name() .get_file_name() .get_file_name() << string(max / 2 .get_file_name() - to_string((*it) - to_string((*it) - to_string((*it) - to_string((*it) - to_string((*it) .at((*it).size() - 1) - to_string((*it) - to_string((*it) - to_string((*it) - to_string((*it) cout << "+" << string(max + 2, '-' ) // Function to show each partition details // w.r.t filename void show_indexed_indexes(vector memory, vector > index_page, string filename) int index, i = 0, main_index; vector >::iterator slot; // Check if file exists if (file_exists(memory, filename)) < for (slot = index_page.begin(); slot != index_page.end(); slot++) < if (memory.at((*slot).at(0)).get_file_name() main_index = i; // Display File Details cout << "File page index at main index :- " << main_index cout << "| Current Location | Partition Number |\n" ; i < index_page.at(main_index).size(); index = index_page .at(main_index) cout << "|" << string(9 - to_string(index) - to_string(index) - to_string(index) - to_string(memory .get_file_partition_no()) - to_string(memory .get_file_partition_no()) .get_file_partition_no() - to_string(memory .get_file_partition_no()) cout << "File does not exist" // Driver Code // Declare memory of size 16 Blocks vector memory(16); // Declare index page vector > index_page; cout << "Remaining memory :- " << get_empty_count(memory) // Set the data temp.filename = "home.txt" ; temp.size = 5; set_indexed_memory(&memory, &index_page, temp.filename = "Report.docx" ; temp.size = 6; set_indexed_memory(&memory, &index_page, temp.filename = "new_img.png" ; temp.size = 3; set_indexed_memory(&memory, &index_page, temp.filename = "test.cpp" ; temp.size = 2; set_indexed_memory(&memory, &index_page, // Print the Remaining memory cout << "Remaining memory :- " << get_empty_count(memory) // Print all the index memory show_indexed_index(memory, index_page); cout << "Now we will check each partition" << " for Report.docx and test.cpp" << " before deleting them" << endl; // Print all the index memory show_indexed_indexes(memory, index_page, "Report.docx" ); // Print all the index memory show_indexed_indexes(memory, index_page, // Now delete Report.docx and test.cpp delete_from_indexed_memory(&memory, &index_page, "Report.docx" ); delete_from_indexed_memory(&memory, &index_page, cout << "Remaining memory :- " << get_empty_count(memory) // Print all the index memory show_indexed_index(memory, index_page); // Creating hello.jpeg file now // and setting it to memory temp.filename = "hello.jpeg" ; temp.size = 8; set_indexed_memory(&memory, &index_page, cout << "Check index page :- " // Print all the index memory show_indexed_index(memory, index_page); // Now we will see index for each partition of cout << "Remaining memory :- " << get_empty_count(memory) cout << "We will check each partition" << " for hello.jpeg to see " ; cout << "if the locations of deleted files" << " are utilized or not" << endl; // Print all the index memory show_indexed_indexes(memory, index_page, memory.clear(); memory.shrink_to_fit(); index_page.clear(); index_page.shrink_to_fit(); Output:
Remaining memory :- 16 File home.txt has been successfully allocated File Report.docx has been successfully allocated File new_img.png has been successfully allocated File test.cpp has been successfully allocated Remaining memory :- 0 +-------------+---------------+-------------+------------------+ | File Name | Start Address | End Address | Size of the file | +-------------+---------------+-------------+------------------+ | home.txt | 7 | 1 | 5 | +-------------+---------------+-------------+------------------+ | Report.docx | 15 | 2 | 6 | +-------------+---------------+-------------+------------------+ | new_img.png | 4 | 14 | 3 | +-------------+---------------+-------------+------------------+ | test.cpp | 5 | 0 | 2 | +-------------+---------------+-------------+------------------+ Now we will check each partition for Report.docx and test.cpp before deleting them File Name = Report.docx File page index at main index :- 1 +------------------+------------------+ | Current Location | Partition Number | +------------------+------------------+ | 15 | 0 | | 10 | 1 | | 12 | 2 | | 13 | 3 | | 11 | 4 | | 2 | 5 | +------------------+------------------+ File Name = test.cpp File page index at main index :- 3 +------------------+------------------+ | Current Location | Partition Number | +------------------+------------------+ | 5 | 0 | | 0 | 1 | +------------------+------------------+ File Report.docx has been successfully deleted File test.cpp has been successfully deleted Remaining memory :- 8 +-------------+---------------+-------------+------------------+ | File Name | Start Address | End Address | Size of the file | +-------------+---------------+-------------+------------------+ | home.txt | 7 | 1 | 5 | +-------------+---------------+-------------+------------------+ | new_img.png | 4 | 14 | 3 | +-------------+---------------+-------------+------------------+ File hello.jpeg has been successfully allocated Check index page :- +-------------+---------------+-------------+------------------+ | File Name | Start Address | End Address | Size of the file | +-------------+---------------+-------------+------------------+ | home.txt | 7 | 1 | 5 | +-------------+---------------+-------------+------------------+ | new_img.png | 4 | 14 | 3 | +-------------+---------------+-------------+------------------+ | hello.jpeg | 12 | 13 | 8 | +-------------+---------------+-------------+------------------+ Remaining memory :- 0 We will check each partition for hello.jpeg to see if the locations of deleted files are utilized or not File Name = hello.jpeg File page index at main index :- 2 +------------------+------------------+ | Current Location | Partition Number | +------------------+------------------+ | 12 | 0 | | 10 | 1 | | 11 | 2 | | 15 | 3 | | 0 | 4 | | 2 | 5 | | 5 | 6 | | 13 | 7 | +------------------+------------------+

3. Linked File Allocation Method:

This is a type of allocation where we linked all the partitions of a file to point to the memory location where the next partition of the file is placed. In the given program below, next will be allocated as -1 when the last partition is reached.

Below is the implementation of Linked File Allocation Method: