0% found this document useful (0 votes)
2 views3 pages

OS 14 b

The document presents a C program for indexed allocation of files on a disk, allowing users to input the number of files and their respective index and data blocks. It checks for free blocks and ensures that the input is valid before allocating the blocks. Upon successful allocation, it displays the File Allocation Table indicating which blocks are allocated.

Uploaded by

jayaprakash9223
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)
2 views3 pages

OS 14 b

The document presents a C program for indexed allocation of files on a disk, allowing users to input the number of files and their respective index and data blocks. It checks for free blocks and ensures that the input is valid before allocating the blocks. Upon successful allocation, it displays the File Allocation Table indicating which blocks are allocated.

Uploaded by

jayaprakash9223
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/ 3

Sprno:9223

14B INDEXED ALLOCATION


PROGRAM:
#include <stdio.h>
#define MAX_BLOCKS 50
int main() {
int disk[MAX_BLOCKS] = {0}; // 0 = free, 1 = allocated
int n, i, j, indexBlock, len, block, count;
printf("Enter number of files: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\nEnter index block and number of blocks for file %d: ", i + 1);
scanf("%d %d", &indexBlock, &len);
// Check if index block is free and in range
if (indexBlock >= MAX_BLOCKS || disk[indexBlock] == 1) {
printf("Error! Index block not free or out of range.\n");
i--; // Retry current file
continue;
}
int blocks[len];
count = 0;
// Input data blocks
for (j = 0; j < len; ) {
printf("Enter block number %d: ", j + 1);
scanf("%d", &block);
if (block >= MAX_BLOCKS || disk[block] == 1) {
printf("Error! Block not free or out of range.\n");
// Do not increment j to retry the same block number
continue;
}
Sprno:9223

blocks[j] = block;
count++;
j++;
}
if (count == len) {
// Mark index block allocated
disk[indexBlock] = 1;
// Mark data blocks allocated
for (j = 0; j < len; j++) {
disk[blocks[j]] = 1;
}
printf("File allocated successfully!\n");
}
}
// Display allocated blocks
printf("\nFile Allocation Table (Indexed):\n");
for (i = 0; i < MAX_BLOCKS; i++) {
if (disk[i] == 1) {
printf("Block %d: Allocated\n", i);
}
}
return 0;
}
OUTPUT:
Enter number of files: 1
Enter index block and number of blocks for file 1: 5 2
Enter block number 1: 10
Enter block number 2: 12
File allocated successfully!
Sprno:9223

File Allocation Table (Indexed):


Block 5: Allocated
Block 10: Allocated
Block 12: Allocated

You might also like