0% found this document useful (0 votes)
6 views

week 9-10

Uploaded by

Swati Joshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

week 9-10

Uploaded by

Swati Joshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Week 9-10

#include <stdio.h>
#include <string.h>

struct File {
char name[20];
int startBlock;
int numBlocks;
int blocks[100]; // Assuming a maximum of 100 blocks for each file
};

// Function for Contiguous Allocation


void contiguous_allocation(struct File files[], int fileCount, char searchFile[]) {
for (int i = 0; i < fileCount; i++) {
if (strcmp(files[i].name, searchFile) == 0) {
printf("File Name: %s\n", files[i].name);
printf("Start block: %d\n", files[i].startBlock);
printf("No. of blocks: %d\n", files[i].numBlocks);
printf("Blocks occupied: ");
for (int j = 0; j < files[i].numBlocks; j++) {
printf("%d ", files[i].startBlock + j);
}
printf("\n");
return;
}
}
printf("File not found\n");
}
// Function for Linked Allocation
void linked_allocation(struct File files[], int fileCount, char searchFile[]) {
for (int i = 0; i < fileCount; i++) {
if (strcmp(files[i].name, searchFile) == 0) {
printf("File Name: %s\n", files[i].name);
printf("Start block: %d\n", files[i].startBlock);
printf("No. of blocks: %d\n", files[i].numBlocks);
printf("Blocks occupied: ");
for (int j = 0; j < files[i].numBlocks; j++) {
printf("%d ", files[i].blocks[j]);
}
printf("\n");
return;
}
}
printf("File not found\n");
}

// Function for Indexed Allocation


void indexed_allocation(struct File files[], int fileCount, char searchFile[]) {
for (int i = 0; i < fileCount; i++) {
if (strcmp(files[i].name, searchFile) == 0) {
printf("File Name: %s\n", files[i].name);
printf("Start block: %d\n", files[i].startBlock);
printf("No. of blocks: %d\n", files[i].numBlocks);
printf("Blocks occupied: ");
for (int j = 0; j < files[i].numBlocks; j++) {
printf("%d ", files[i].blocks[j]);
}
printf("\n");
return;
}
}
printf("File not found\n");
}

int main() {
int fileCount, choice;
char searchFile[20];

printf("Enter number of files: ");


scanf("%d", &fileCount);

struct File files[fileCount];

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


printf("Enter file %d name: ", i + 1);
scanf("%s", files[i].name);
printf("Enter starting block of file %d: ", i + 1);
scanf("%d", &files[i].startBlock);
printf("Enter no of blocks in file %d: ", i + 1);
scanf("%d", &files[i].numBlocks);

if (choice == 2 || choice == 3) {
printf("Enter blocks for file %d: ", i + 1);
for (int j = 0; j < files[i].numBlocks; j++) {
scanf("%d", &files[i].blocks[j]);
}
}
}

printf("Enter the file name to be searched: ");


scanf("%s", searchFile);

printf("Choose the allocation strategy:\n1. Contiguous\n2. Linked\n3. Indexed\n");


scanf("%d", &choice);

if (choice == 1) {
contiguous_allocation(files, fileCount, searchFile);
} else if (choice == 2) {
linked_allocation(files, fileCount, searchFile);
} else if (choice == 3) {
indexed_allocation(files, fileCount, searchFile);
} else {
printf("Invalid choice\n");
}

return 0;
}

You might also like