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

Slip 2 Q1 Slip 6 Q1

The document describes a program that simulates linked file allocation on a disk with a given number of blocks. It implements a menu driven program with options to show the bit vector, create a new file, show the directory, and exit. It uses a linked list to track free blocks and randomly marks some blocks as allocated during initialization.

Uploaded by

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

Slip 2 Q1 Slip 6 Q1

The document describes a program that simulates linked file allocation on a disk with a given number of blocks. It implements a menu driven program with options to show the bit vector, create a new file, show the directory, and exit. It uses a linked list to track free blocks and randomly marks some blocks as allocated during initialization.

Uploaded by

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

/*Slip2 Q1 and Slip 61

Write a program to simulate Linked file allocation method. Assume disk with n
number of blocks. Give value of n as input. Randomly mark some block as allocated
and
accordingly maintain the list of free blocks Write menu driver program with menu
options as
mentioned below and implement each option.
• Show Bit Vector
• Create New File
• Show Directory
• Exit

*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_BLOCKS 100

// Node structure for the linked list


struct Node {
int block;
struct Node* next;
};

int disk[MAX_BLOCKS] = {0}; // 0 represents free block, 1 represents allocated


block
struct Node* freeList = NULL; // Linked list to store the free blocks

void initializeFreeList(int n) {
for (int i = n - 1; i >= 0; i--) {
if (disk[i] == 0) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->block = i;
newNode->next = freeList;
freeList = newNode;
}
}
}

void showBitVector(int n) {
printf("\nBit Vector (Disk Allocation):\n");
for (int i = 0; i < n; i++) {
printf("%d ", disk[i]);
}
printf("\n");
}

void createNewFile(int n) {
int size;
printf("\nEnter the size of the file: ");
scanf("%d", &size);

if (freeList == NULL) {
printf("\nNo free blocks available to create the file.\n");
return;
}
struct Node* current = freeList;
// Allocate the blocks for the file
for (int i = 0; i < size; i++) {
disk[current->block] = 1;
struct Node* temp = current;
current = current->next;
free(temp);
}
freeList = current;

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


}

void showDirectory(int n) {
printf("\nDirectory Listing:\n");
for (int i = 0; i < n; i++) {
if (disk[i] == 1) {
printf("Block %d: Allocated\n", i);
} else {
printf("Block %d: Free\n", i);
}
}
}

int main() {
int n;

printf("Enter the number of blocks on the disk: ");


scanf("%d", &n);

srand(time(NULL)); // Seed for random block allocation

// Randomly mark some blocks as allocated


for (int i = 0; i < n; i++) {
if (rand() % 2 == 1) {
disk[i] = 1;
}
}

initializeFreeList(n);

int choice;
do {
printf("\nMenu:\n");
printf("1. Show Bit Vector\n");
printf("2. Create New File\n");
printf("3. Show Directory\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
showBitVector(n);
break;

case 2:
createNewFile(n);
break;
case 3:
showDirectory(n);
break;

case 4:
printf("\nExiting the program.\n");
break;

default:
printf("\nInvalid choice. Please enter a valid option.\n");
}
} while (choice != 4);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy