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

file allocation

The document contains three simplified C programs demonstrating different file allocation methods: Contiguous, Linked, and Indexed. Each program prompts the user for input regarding block allocation and checks for availability before allocating the specified blocks. If the requested blocks are already allocated, the program notifies the user of the failure to allocate the file.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

file allocation

The document contains three simplified C programs demonstrating different file allocation methods: Contiguous, Linked, and Indexed. Each program prompts the user for input regarding block allocation and checks for availability before allocating the specified blocks. If the requested blocks are already allocated, the program notifies the user of the failure to allocate the file.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Contiguous File Allocation (Simplified)


c
Copy
Edit
#include <stdio.h>
#include <conio.h>

void main() {
int start, length, i, flag = 0;
int blocks[50] = {0};

clrscr();

printf("Enter start block: ");


scanf("%d", &start);
printf("Enter length: ");
scanf("%d", &length);

for (i = start; i < start + length; i++) {


if (blocks[i] == 1) {
flag = 1;
break;
}
}

if (flag == 0) {
for (i = start; i < start + length; i++)
blocks[i] = 1;

printf("File allocated from block %d to %d\n", start, start + length - 1);


} else {
printf("Blocks not free. Allocation failed.\n");
}

getch();
}
2. Linked File Allocation (Simplified)
c
Copy
Edit
#include <stdio.h>
#include <conio.h>

void main() {
int blocks[50] = {0};
int start, length, i, count = 0;

clrscr();

printf("Enter starting block: ");


scanf("%d", &start);
printf("Enter length: ");
scanf("%d", &length);

if (blocks[start] == 0) {
blocks[start] = 1;
printf("Allocated: %d ", start);
for (i = 0; i < 50 && count < length - 1; i++) {
if (blocks[i] == 0) {
blocks[i] = 1;
printf("-> %d ", i);
count++;
}
}
printf("-> NULL\n");
} else {
printf("Starting block already allocated.\n");
}

getch();
}
3. Indexed File Allocation (Simplified)
c
Copy
Edit
#include <stdio.h>
#include <conio.h>

void main() {
int blocks[50] = {0}, indexBlock, i, n, b;

clrscr();

printf("Enter index block: ");


scanf("%d", &indexBlock);

if (blocks[indexBlock] == 0) {
blocks[indexBlock] = 1;

printf("Enter number of blocks: ");


scanf("%d", &n);

printf("Enter block numbers: ");


for (i = 0; i < n; i++) {
scanf("%d", &b);
if (blocks[b] == 0)
blocks[b] = 1;
else {
printf("Block %d is already allocated. Failed.\n", b);
break;
}
}

if (i == n)
printf("File allocated successfully.\n");
} else {
printf("Index block already allocated.\n");
}

getch();
}

You might also like