Operating System Ass2
Operating System Ass2
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>
struct File {
char name[50];
int start_block;
int length;
};
int num_files = 0;
void create_file(int n) {
char name[50];
int length;
1
printf("Enter file name: ");
scanf("%s", name);
scanf("%d", &length);
if (disk[i + j] == 1) {
contiguous = false;
break;
if (contiguous) {
start_block = i;
break;
if (start_block != -1) {
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 {
void delete_file() {
char name[50];
scanf("%s", name);
if (strcmp(files[i].name, name) == 0) {
disk[files[i].start_block + j] = 0;
num_files--;
3
printf("File '%s' not found.\n", name);
void display_files() {
if (num_files == 0) {
return;
printf("Files:\n");
void display_disk_status(int n) {
printf("\n");
int free_blocks = 0;
if (disk[i] == 0) {
free_blocks++;
4
}
int main() {
int n;
while (true) {
printf("Invalid input. Please enter a positive integer less than or equal to %d.\n",
MAX_BLOCKS);
int choice;
do {
5
printf("2. Delete File\n");
printf("5. Exit\n");
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:
default:
printf("Invalid choice.\n");
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>
7
#define MAX_FILES 20
struct File {
char name[50];
int start_block;
};
int num_files = 0;
int num_free_blocks;
void initialize_free_blocks(int n) {
num_free_blocks = n;
free_blocks[i] = i;
int allocate_block() {
if (num_free_blocks == 0) {
8
}
num_free_blocks--;
return block;
free_blocks[num_free_blocks] = block;
num_free_blocks++;
free_blocks[i - 1] = temp;
} else {
break;
void create_file(int n) {
char name[50];
int length;
scanf("%s", name);
9
printf("Enter file size (blocks): ");
scanf("%d", &length);
return;
files[num_files].start_block = start_block;
strcpy(files[num_files].name, name);
num_files++;
return;
disk[current_block] = next_block;
current_block = next_block;
10
printf("File '%s' created. Start: %d\n", name, start_block);
void delete_file() {
char name[50];
scanf("%s", name);
if (strcmp(files[i].name, name) == 0) {
free_block(current_block);
current_block = next_block;
num_files--;
return;
11
printf("File '%s' not found.\n", name);
void display_files() {
if (num_files == 0) {
return;
printf("Files:\n");
current_block = disk[current_block];
printf("EOF\n");
void display_disk_status(int n) {
printf("\n");
12
printf("Total blocks: %d, Free blocks: %d\n", n, num_free_blocks);
int main() {
int n;
while (true) {
printf("Invalid input. Please enter a positive integer less than or equal to %d.\n",
MAX_BLOCKS);
continue;
break;
initialize_free_blocks(n);
int choice;
do {
13
printf("3. Display Files\n");
printf("5. Exit\n");
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");
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_FILES 20
15
int disk[MAX_BLOCKS]; // 0: free, -2: index block, other: data block
struct File {
char name[50];
int index_block;
};
int num_files = 0;
int free_blocks[MAX_BLOCKS];
int num_free_blocks;
void initialize_free_blocks(int n) {
num_free_blocks = n;
free_blocks[i] = i;
int allocate_block() {
if (num_free_blocks == 0) {
num_free_blocks--;
16
return block;
free_blocks[num_free_blocks] = block;
num_free_blocks++;
free_blocks[i - 1] = temp;
} else {
break;
void create_file(int n) {
char name[50];
int length;
scanf("%s", name);
scanf("%d", &length);
17
if (length > num_free_blocks - 1) { // -1 for index block
return;
files[num_files].index_block = index_block;
strcpy(files[num_files].name, name);
num_files++;
int index_table[MAX_INDEX_ENTRIES];
return;
index_table[i] = data_block;
void delete_file() {
char name[50];
18
printf("Enter file name to delete: ");
scanf("%s", name);
if (strcmp(files[i].name, name) == 0) {
free_block(index_table[j]);
disk[index_table[j]] = 0;
free_block(index_block);
disk[index_block] = 0;
num_files--;
return;
19
printf("File '%s' not found.\n", name);
void display_files() {
if (num_files == 0) {
return;
printf("Files:\n");
printf("- %s: Index block: %d, Data blocks: ", files[i].name, files[i].index_block);
printf("\n");
void display_disk_status(int n) {
if (disk[i] == -2) {
printf("Index ");
} else {
20
printf("%d ", disk[i]);
printf("\n");
int main() {
int n;
while (true) {
printf("Invalid input. Please enter a positive integer less than or equal to %d.\n",
MAX_BLOCKS);
continue;
break;
initialize_free_blocks(n);
int choice;
21
do {
printf("5. Exit\n");
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
}
return 0;
Output:-
23