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

OS 14 c

The document presents a C program for linked allocation of disk blocks, allowing users to allocate files based on starting blocks and lengths. It initializes a disk with a maximum of 50 blocks, checks for free blocks, and manages the allocation process while handling errors for invalid inputs. The program also displays a file allocation table after successful allocations.

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)
8 views3 pages

OS 14 c

The document presents a C program for linked allocation of disk blocks, allowing users to allocate files based on starting blocks and lengths. It initializes a disk with a maximum of 50 blocks, checks for free blocks, and manages the allocation process while handling errors for invalid inputs. The program also displays a file allocation table after successful allocations.

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

14C LINKED ALLOCATION


PROGRAM:
#include <stdio.h>
#define MAX_BLOCKS 50
struct Block {
int allocated;
int next;
};
int main() {
struct Block disk[MAX_BLOCKS];
int n, i, j, k, start, len;
// Initialize disk blocks
for (i = 0; i < MAX_BLOCKS; i++) {
disk[i].allocated = 0;
disk[i].next = -1;
}
printf("Enter number of files: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\nEnter starting block and length of file %d: ", i + 1);
scanf("%d %d", &start, &len);
if (start >= MAX_BLOCKS || disk[start].allocated == 1) {
printf("Error! Starting block not free or out of range.\n");
i--;
continue;
}
disk[start].allocated = 1;
int prev = start;
int count = 1; // count includes starting block
Sprno:9223

// Allocate remaining blocks


for (j = 0; j < len - 1; j++) {
for (k = 0; k < MAX_BLOCKS; k++) {
if (disk[k].allocated == 0) {
break;
}
}
if (k == MAX_BLOCKS) {
printf("Error! Not enough free blocks.\n");
break;
}
disk[k].allocated = 1;
disk[prev].next = k;
prev = k;
count++;
}
if (count == len) {
printf("File allocated successfully!\n");
} else {
printf("File partially allocated due to lack of space!\n");
}
}
// Display file allocation table
printf("\nFile Allocation Table (Linked):\n");
for (i = 0; i < MAX_BLOCKS; i++) {
if (disk[i].allocated) {
printf("Block %d -> %d\n", i, disk[i].next);
}
}
Sprno:9223

return 0;
}
OUTPUT:
Enter number of files: 2
Enter starting block and length of file 1: 5 3
File allocated successfully!
Enter starting block and length of file 2: 10 2
File allocated successfully!
File Allocation Table (Linked):
Block 5 -> 0
Block 0 -> 1
Block 1 -> -1
Block 10 -> 2
Block 2 -> -1

You might also like