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

OS Lab Program 9

Uploaded by

jayashankaryt14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

OS Lab Program 9

Uploaded by

jayashankaryt14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment Detail: Develop a C program to simulate the Linked file allocation strategies.

Program:

#include <stdio.h>

#include <stdlib.h>

int main() {

int f[50] = {0}; // Array to keep track of allocated blocks, initialized to 0 (unallocated)

int allocatedBlocks, i, startBlock, length, choice, blockIndex, tempBlock;

printf("Enter the number of blocks already allocated: ");

scanf("%d", &allocatedBlocks);

printf("Enter the blocks already allocated: ");

for (i = 0; i < allocatedBlocks; i++) {

scanf("%d", &tempBlock);

if (tempBlock >= 0 && tempBlock < 50) {

f[tempBlock] = 1; // Mark the block as allocated

} else {

printf("Invalid block number %d. It should be between 0 and 49.\n", tempBlock);

do {

printf("Enter the starting block index and length of file: ");

scanf("%d%d", &startBlock, &length);

if (length <= 0 || length > 50) {

printf("Invalid length. Please try again.\n");

continue;
}

int canAllocate = 1;

int blocksAllocated = 0;

printf("Allocated blocks:\n");

for (i = 0; i < 50 && blocksAllocated < length; i++) {

if (f[i] == 0) { // Check if the block is free

printf("%d ----> ", i);

f[i] = 1; // Allocate the block

blocksAllocated++;

if (blocksAllocated < length) {

printf("\nInsu_icient free blocks available. File could not be allocated.\n");

// Rollback any partial allocations

for (i = 0; i < 50; i++) {

if (f[i] == 1 && blocksAllocated > 0) {

f[i] = 0;

blocksAllocated--;

} else {

printf("\nFile allocated successfully.\n");

printf("Do you want to enter more files (Yes - 1 / No - 0)? ");


scanf("%d", &choice);

} while (choice == 1);

return 0;

OUTPUT:

Enter the number of blocks already allocated: 5

Enter the blocks already allocated: 1 2 3 4 5

Enter the starting block index and length of file: 0 5

Allocated blocks:

0 ----> 6 ----> 7 ----> 8 ----> 9 ---->

File allocated successfully.

Do you want to enter more files (Yes - 1 / No - 0)? 0

You might also like