0% found this document useful (0 votes)
19 views4 pages

CFA

This C program implements a simple file management system using a bit vector to track memory blocks. Users can create and delete files, which updates the memory allocation accordingly, and view the current state of memory blocks and the directory of files. The program continuously prompts for user input until the user chooses to exit.

Uploaded by

gauriovhal46
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)
19 views4 pages

CFA

This C program implements a simple file management system using a bit vector to track memory blocks. Users can create and delete files, which updates the memory allocation accordingly, and view the current state of memory blocks and the directory of files. The program continuously prompts for user input until the user chooses to exit.

Uploaded by

gauriovhal46
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/ 4

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>

#define MAX_FILES 100

struct File_Details
{
char File_Name[10];
int File_size;
int File_start;
} directory[MAX_FILES];

int *Bit_vector;
int memory_size;
int count = 0;

void Initialize_bit_vector()
{
printf("Enter the size of memory (number of blocks): ");
scanf("%d", &memory_size);

Bit_vector = (int *)malloc(memory_size * sizeof(int));

for (int i = 0; i < memory_size; i++)


{
Bit_vector[i] = 1;
}
}

void Show_memory_blocks()
{
printf("Memory Blocks: ");
for (int i = 0; i < memory_size; i++)
{
printf("%d ", Bit_vector[i]);
}
printf("\n");
}

int Check_if_free_block_available(int block_no)


{
if (Bit_vector[block_no] == 1)
return 1;
else
return 0;

int Check_for_continuous_space(int file_size)


{
for (int i = 0; i <= memory_size - file_size; i++)
{
int j;
for (j = i; j < i + file_size; j++)
{
if (!Check_if_free_block_available(j))
{
break;
}
}
if (j == i + file_size)
{
return i; // Return the starting block if a continuous block is found
}
}
return -1; // Return -1 if no continuous block is found
}

void Update_bit_vector(int start_pos, int file_size, int value)


{
for (int i = start_pos; i < start_pos + file_size; i++)
{
Bit_vector[i] = value; // Mark the blocks based on value (0 = used, 1 =
free)
}
}

void Add_directory_entry(char *filename, int file_size, int start_pos)


{
strcpy(directory[count].File_Name, filename);
directory[count].File_size = file_size;
directory[count].File_start = start_pos;
count++;
}

void Create_File()
{
char filename[10];
int file_size, start_pos;

printf("Enter File name: ");


scanf("%s", filename);

printf("Enter File size: ");


scanf("%d", &file_size);

// Check if a continuous block of memory is available


start_pos = Check_for_continuous_space(file_size);

if (start_pos == -1)
{
printf("Error: No free memory available\n");
return;
}

// Mark the allocated memory blocks as used


Update_bit_vector(start_pos, file_size, 0);

// Add file details to the directory


Add_directory_entry(filename, file_size, start_pos);

printf("File created: %s, Start: %d, Length: %d\n", filename, start_pos,


file_size);
}

void Delete_File()
{
char filename[10];
printf("Enter name to delete: ");
scanf("%s", filename);

for (int i = 0; i < count; i++)


{
if (strcmp(directory[i].File_Name, filename) == 0)
{
int file_size = directory[i].File_size;
int file_start = directory[i].File_start;

// Update the bit vector to mark the blocks as free


Update_bit_vector(file_start, file_size, 1);

// Remove the file from the directory by shifting the remaining entries
for (int j = i; j < count - 1; j++)
{
directory[j] = directory[j + 1];
}
count--;
printf("File deleted.\n");
return;
}
}
printf("File not found.\n");
}

void Display_directory()
{
printf("Directory:\n");
for (int i = 0; i < count; i++)
{
printf("Name: %s, Start: %d, Length: %d\n", directory[i].File_Name,
directory[i].File_start, directory[i].File_size);
}
}

int main()
{
int choice;
Initialize_bit_vector();

while (1)
{
printf("\n1. Show Memory Blocks\n");
printf("2. Create New File\n");
printf("3. Show Directory\n");
printf("4. Delete File\n");
printf("5. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);

switch (choice)
{
case 1: Show_memory_blocks(); break;
case 2: Create_File(); break;
case 3: Display_directory(); break;
case 4: Delete_File(); break;
case 5:
free(Bit_vector); // Free the allocated memory
exit(0);
break;
default: printf("Invalid Choice\n");
}
}

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