0% found this document useful (0 votes)
19 views23 pages

Operating System Ass2

The document outlines three programs simulating different file allocation methods: Sequential, Linked, and Indexed. Each program includes a menu-driven interface for creating, deleting, and displaying files, as well as checking disk status. The code snippets demonstrate the implementation of each method, managing disk blocks and file metadata.

Uploaded by

noahdcruze1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views23 pages

Operating System Ass2

The document outlines three programs simulating different file allocation methods: Sequential, Linked, and Indexed. Each program includes a menu-driven interface for creating, deleting, and displaying files, as well as checking disk status. The code snippets demonstrate the implementation of each method, managing disk blocks and file metadata.

Uploaded by

noahdcruze1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Assignment 2:File Allocation Methods

Slot-1

i)Write a program to simulate Sequential (Contiguous) file allocation method. Assume disk
with n number of blocks. Give value of n as input. Write menu driver program with menu
options as mentioned above and implement each option.

Program:-

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#define MAX_BLOCKS 100 // Define a maximum number of blocks

int disk[MAX_BLOCKS]; // 0: free, 1: allocated

struct File {

char name[50];

int start_block;

int length;

};

struct File files[20]; // Array to store file information (max 20 files)

int num_files = 0;

void create_file(int n) {

char name[50];

int length;

1
printf("Enter file name: ");

scanf("%s", name);

printf("Enter file size (blocks): ");

scanf("%d", &length);

int start_block = -1;

for (int i = 0; i <= n - length; i++) {

bool contiguous = true;

for (int j = 0; j < length; j++) {

if (disk[i + j] == 1) {

contiguous = false;

break;

if (contiguous) {

start_block = i;

break;

if (start_block != -1) {

for (int i = 0; i < length; i++) {

disk[start_block + i] = 1;

strcpy(files[num_files].name, name);

files[num_files].start_block = start_block;

files[num_files].length = length;

2
num_files++;

printf("File '%s' created. Start: %d, Length: %d\n", name, start_block, length);

} else {

printf("Not enough contiguous space.\n");

void delete_file() {

char name[50];

printf("Enter file name to delete: ");

scanf("%s", name);

for (int i = 0; i < num_files; i++) {

if (strcmp(files[i].name, name) == 0) {

for (int j = 0; j < files[i].length; j++) {

disk[files[i].start_block + j] = 0;

// Shift remaining files in the array

for (int k = i; k < num_files - 1; k++) {

files[k] = files[k + 1];

num_files--;

printf("File '%s' deleted.\n", name);

return; // File found and deleted

3
printf("File '%s' not found.\n", name);

void display_files() {

if (num_files == 0) {

printf("No files allocated.\n");

return;

printf("Files:\n");

for (int i = 0; i < num_files; i++) {

printf("- %s: Start: %d, Length: %d\n", files[i].name, files[i].start_block, files[i].length);

void display_disk_status(int n) {

printf("Disk Status: ");

for (int i = 0; i < n; i++) {

printf("%d ", disk[i]);

printf("\n");

int free_blocks = 0;

for (int i = 0; i < n; i++) {

if (disk[i] == 0) {

free_blocks++;

4
}

printf("Total blocks: %d, Free blocks: %d\n", n, free_blocks);

int main() {

int n;

while (true) {

printf("Enter the number of disk blocks: ");

if (scanf("%d", &n) != 1 || n <= 0 || n > MAX_BLOCKS) { // Check for valid input

printf("Invalid input. Please enter a positive integer less than or equal to %d.\n",
MAX_BLOCKS);

while (getchar() != '\n'); // Clear the input buffer

continue; // Ask for input again

break; // Exit the loop if input is valid

for (int i = 0; i < MAX_BLOCKS; i++) {

disk[i] = 0; // Initialize disk to free

int choice;

do {

printf("\nSequential Allocation Menu:\n");

printf("1. Create File\n");

5
printf("2. Delete File\n");

printf("3. Display Files\n");

printf("4. Display Disk Status\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

create_file(n);

break;

case 2:

delete_file();

break;

case 3:

display_files();

break;

case 4:

display_disk_status(n);

break;

case 5:

break; // Exit the loop

default:

printf("Invalid choice.\n");

} while (choice != 5);

6
return 0;

Output:-

Slot-II

i) Write a program to simulate Linked file allocation method. Assume disk with n number
of blocks. Give value of'n as input. Write menu driver program with menu options as
mentioned above and implement each option.

Program:-

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <stdbool.h>

#define MAX_BLOCKS 100

7
#define MAX_FILES 20

int disk[MAX_BLOCKS]; // 0: free, -1: EOF, other: next block

struct File {

char name[50];

int start_block;

};

struct File files[MAX_FILES];

int num_files = 0;

int free_blocks[MAX_BLOCKS]; // Keep track of free blocks

int num_free_blocks;

void initialize_free_blocks(int n) {

num_free_blocks = n;

for (int i = 0; i < n; i++) {

free_blocks[i] = i;

int allocate_block() {

if (num_free_blocks == 0) {

return -1; // No free blocks

int block = free_blocks[0];

for (int i = 0; i < num_free_blocks - 1; i++) {

free_blocks[i] = free_blocks[i + 1];

8
}

num_free_blocks--;

return block;

void free_block(int block) {

free_blocks[num_free_blocks] = block;

num_free_blocks++;

// Keep free_blocks sorted (important for efficiency)

for (int i = num_free_blocks - 1; i > 0; i--) {

if (free_blocks[i] < free_blocks[i - 1]) {

int temp = free_blocks[i];

free_blocks[i] = free_blocks[i - 1];

free_blocks[i - 1] = temp;

} else {

break;

void create_file(int n) {

char name[50];

int length;

printf("Enter file name: ");

scanf("%s", name);

9
printf("Enter file size (blocks): ");

scanf("%d", &length);

if (length > num_free_blocks) {

printf("Not enough free blocks available.\n");

return;

int start_block = allocate_block();

if (start_block == -1) return; //Allocation failed

int current_block = start_block;

disk[current_block] = -1; // Initialize to EOF in case length is 1

files[num_files].start_block = start_block;

strcpy(files[num_files].name, name);

num_files++;

for (int i = 1; i < length; i++) {

int next_block = allocate_block();

if (next_block == -1) { //Allocation failed mid way

printf("Allocation failed mid way. File creation partially complete.\n");

disk[current_block]=-1; //Mark the end of the file

return;

disk[current_block] = next_block;

current_block = next_block;

disk[current_block] = -1; // Mark the end of this block

10
printf("File '%s' created. Start: %d\n", name, start_block);

void delete_file() {

char name[50];

printf("Enter file name to delete: ");

scanf("%s", name);

for (int i = 0; i < num_files; i++) {

if (strcmp(files[i].name, name) == 0) {

int current_block = files[i].start_block;

while (current_block != -1) {

int next_block = disk[current_block];

free_block(current_block);

disk[current_block] = 0; // Reset the disk

current_block = next_block;

for (int k = i; k < num_files - 1; k++) {

files[k] = files[k + 1];

num_files--;

printf("File '%s' deleted.\n", name);

return;

11
printf("File '%s' not found.\n", name);

void display_files() {

if (num_files == 0) {

printf("No files allocated.\n");

return;

printf("Files:\n");

for (int i = 0; i < num_files; i++) {

printf("- %s: ", files[i].name);

int current_block = files[i].start_block;

while (current_block != -1) {

printf("%d -> ", current_block);

current_block = disk[current_block];

printf("EOF\n");

void display_disk_status(int n) {

printf("Disk Status: ");

for (int i = 0; i < n; i++) {

printf("%d ", disk[i]);

printf("\n");

12
printf("Total blocks: %d, Free blocks: %d\n", n, num_free_blocks);

int main() {

int n;

while (true) {

printf("Enter the number of disk blocks: ");

if (scanf("%d", &n) != 1 || n <= 0 || n > MAX_BLOCKS) {

printf("Invalid input. Please enter a positive integer less than or equal to %d.\n",
MAX_BLOCKS);

while (getchar() != '\n'); // Clear the input buffer

continue;

break;

initialize_free_blocks(n);

for (int i = 0; i < MAX_BLOCKS; i++) {

disk[i] = 0; // Initialize disk to free

int choice;

do {

printf("\nLinked File Allocation Menu:\n");

printf("1. Create File\n");

printf("2. Delete File\n");

13
printf("3. Display Files\n");

printf("4. Display Disk Status\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

create_file(n);

break;

case 2:

delete_file();

break;

case 3:

display_files();

break;

case 4:

display_disk_status(n);

break;

case 5:

break;

default:

printf("Invalid choice.\n");

} while (choice != 5);

return 0;

14
}

Output:-

Slot-III

i)Write a program to simulate Indexed file allocation method. Assume disk with n number
of blocks. Give value of n as input. Write menu driver program with menu options as
mentioned above and implement each option.

Program:-

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <stdbool.h>

#define MAX_BLOCKS 100

#define MAX_FILES 20

#define MAX_INDEX_ENTRIES 10 // Max entries in an index block

15
int disk[MAX_BLOCKS]; // 0: free, -2: index block, other: data block

struct File {

char name[50];

int index_block;

};

struct File files[MAX_FILES];

int num_files = 0;

int free_blocks[MAX_BLOCKS];

int num_free_blocks;

void initialize_free_blocks(int n) {

num_free_blocks = n;

for (int i = 0; i < n; i++) {

free_blocks[i] = i;

int allocate_block() {

if (num_free_blocks == 0) {

return -1; // No free blocks

int block = free_blocks[0];

for (int i = 0; i < num_free_blocks - 1; i++) {

free_blocks[i] = free_blocks[i + 1];

num_free_blocks--;

16
return block;

void free_block(int block) {

free_blocks[num_free_blocks] = block;

num_free_blocks++;

// Keep free_blocks sorted (important for efficiency)

for (int i = num_free_blocks - 1; i > 0; i--) {

if (free_blocks[i] < free_blocks[i - 1]) {

int temp = free_blocks[i];

free_blocks[i] = free_blocks[i - 1];

free_blocks[i - 1] = temp;

} else {

break;

void create_file(int n) {

char name[50];

int length;

printf("Enter file name: ");

scanf("%s", name);

printf("Enter file size (blocks): ");

scanf("%d", &length);

17
if (length > num_free_blocks - 1) { // -1 for index block

printf("Not enough free blocks available.\n");

return;

int index_block = allocate_block();

if (index_block == -1) return; // Allocation failed

disk[index_block] = -2; // Mark as index block

files[num_files].index_block = index_block;

strcpy(files[num_files].name, name);

num_files++;

int index_table[MAX_INDEX_ENTRIES];

for (int i = 0; i < length; i++) {

int data_block = allocate_block();

if (data_block == -1) { //Allocation failed mid way

printf("Allocation failed mid way. File creation partially complete.\n");

return;

index_table[i] = data_block;

disk[index_block] = index_table; // Store index table in the index block

printf("File '%s' created. Index block: %d\n", name, index_block);

void delete_file() {

char name[50];

18
printf("Enter file name to delete: ");

scanf("%s", name);

for (int i = 0; i < num_files; i++) {

if (strcmp(files[i].name, name) == 0) {

int index_block = files[i].index_block;

int *index_table = disk[index_block]; // Get the index table

for (int j = 0; j < MAX_INDEX_ENTRIES; j++) {

if (index_table[j] != 0){ // Check if the entry is valid before freeing

free_block(index_table[j]);

disk[index_table[j]] = 0;

free_block(index_block);

disk[index_block] = 0;

for (int k = i; k < num_files - 1; k++) {

files[k] = files[k + 1];

num_files--;

printf("File '%s' deleted.\n", name);

return;

19
printf("File '%s' not found.\n", name);

void display_files() {

if (num_files == 0) {

printf("No files allocated.\n");

return;

printf("Files:\n");

for (int i = 0; i < num_files; i++) {

printf("- %s: Index block: %d, Data blocks: ", files[i].name, files[i].index_block);

int *index_table = disk[files[i].index_block];

for (int j = 0; j < MAX_INDEX_ENTRIES; j++) {

if (index_table[j] != 0) // Check if the entry is valid before printing

printf("%d ", index_table[j]);

printf("\n");

void display_disk_status(int n) {

printf("Disk Status: ");

for (int i = 0; i < n; i++) {

if (disk[i] == -2) {

printf("Index ");

} else {

20
printf("%d ", disk[i]);

printf("\n");

printf("Total blocks: %d, Free blocks: %d\n", n, num_free_blocks);

int main() {

int n;

while (true) {

printf("Enter the number of disk blocks: ");

if (scanf("%d", &n) != 1 || n <= 0 || n > MAX_BLOCKS) {

printf("Invalid input. Please enter a positive integer less than or equal to %d.\n",
MAX_BLOCKS);

while (getchar() != '\n'); // Clear the input buffer

continue;

break;

initialize_free_blocks(n);

for (int i = 0; i < MAX_BLOCKS; i++) {

disk[i] = 0; // Initialize disk to free

int choice;

21
do {

printf("\nIndexed File Allocation Menu:\n");

printf("1. Create File\n");

printf("2. Delete File\n");

printf("3. Display Files\n");

printf("4. Display Disk Status\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

create_file(n);

break;

case 2:

delete_file();

break;

case 3:

display_files();

break;

case 4:

display_disk_status(n);

break;

case 5:

break;

default:

printf("Invalid choice.\n");

22
}

} while (choice != 5);

return 0;

Output:-

23

You might also like