0% found this document useful (0 votes)
13 views5 pages

IFA

The document is a C program that implements a simple file management system using a bit vector for memory allocation. It allows users to create and delete files, display memory blocks, and show the directory of files. The program manages memory blocks and keeps track of allocated and free blocks using a linked structure.

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)
13 views5 pages

IFA

The document is a C program that implements a simple file management system using a bit vector for memory allocation. It allows users to create and delete files, display memory blocks, and show the directory of files. The program manages memory blocks and keeps track of allocated and free blocks using a linked structure.

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/ 5

#include<stdio.

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

#define MAX_FILES 100


#define MAX_INDEX 50
#define BIT_VECTOR 100

struct bit_vector_node
{
int allocated;
int index_table[MAX_INDEX];
struct bit_vector_node *next;
};

struct dir_entry
{
char file_name[10];
int index_block_no;
int file_size;
};

struct bit_vector_node *bit_vector[BIT_VECTOR];

struct dir_entry dir[MAX_FILES];

int dir_count = 0;

int free_blocks[BIT_VECTOR];

int free_block_count = 0;

int memory_size;

void initialize_bit_vector()
{
int i , j;

printf("Enter the size of memory blocks : ");


scanf("%d",&memory_size);

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


{
bit_vector[i] = (struct bit_vector_node *)malloc(sizeof(struct
bit_vector_node));

bit_vector[i] -> allocated = 1;

for(j=0 ; j < MAX_INDEX ; j++)


{
bit_vector[i] -> index_table[j] = -1;
}

bit_vector[i] -> next = NULL;

free_blocks[i] = i;
}
free_block_count = memory_size;
}

void show_memory_blocks()
{
int is_index_block;
int i, j;

printf("Memory Blocks:\n");

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


{
if (bit_vector[i]->allocated == 1)
{
printf("1 ");
}
else
{
is_index_block = 0;

for (j = 0; j < MAX_INDEX; j++)


{
if (bit_vector[i]->index_table[j] != -1)
{
is_index_block = 1;
break;
}
}

if (is_index_block)
{
printf("{0,{");

for (j = 0; j < MAX_INDEX; j++)


{
if (bit_vector[i]->index_table[j] != -1)
{
printf("%d", bit_vector[i]->index_table[j]);

if (j < MAX_INDEX - 1 && bit_vector[i]->index_table[j +


1] != -1)
{
printf(",");
}
}
}

printf("}} ");
}
else
{
printf("{0,{}} ");
}
}
}
printf("\n");
}
int find_free_block()
{
int random_index , block_index;

if(free_block_count == 0)
return -1;

random_index = rand() % free_block_count;


block_index = free_blocks[random_index];
free_blocks[random_index] = free_blocks[free_block_count - 1];
free_block_count--;
return block_index;
}

void add_directory_entry(char *filename , int filesize , int index_block_no)


{
strcpy(dir[dir_count].file_name , filename);
dir[dir_count].file_size = filesize;
dir[dir_count].index_block_no = index_block_no;
dir_count++;
}

void create_file()
{
char filename[10];
int filesize;
int index_block_no;
int block_no;
int i;

printf("Enter file name to create : ");


scanf("%s",filename);

printf("Enter file size : ");


scanf("%d",&filesize);

index_block_no = find_free_block();

if(index_block_no == -1)
{
printf("No free memory !");
}

else
{
bit_vector[index_block_no] -> allocated = 0;
}

for(i=0 ; i < filesize ; i++)


{
block_no = find_free_block();

if(block_no == -1)
{
printf("No free memory !");
}
else
{
bit_vector[block_no] -> allocated = 0;
bit_vector[index_block_no] -> index_table[i] = block_no;
}
}

add_directory_entry(filename , filesize , index_block_no);


printf("File created : %s , Index Block : %d , Length : %d\n", filename ,
index_block_no , filesize);
}

void delete_file()
{
char filename[10];
int index;
int filesize;
int block_no;
int i, j;

printf("Enter file name to delete : ");


scanf("%s",filename);

for(i=0 ; i < dir_count ; i++)


{
if(strcmp(dir[i].file_name , filename) == 0)
{
filesize = dir[i].file_size;
index = dir[i].index_block_no;

for(j= 0 ; j < filesize ; j++)


{
block_no = bit_vector[index] -> index_table[j];
bit_vector[block_no] -> allocated = 1;
free_blocks[free_block_count++] = block_no;
}

bit_vector[index] -> allocated = 1;


free_blocks[free_block_count++] = index;

for(j = i ; j < dir_count ; j++)


{
dir[j] = dir[j+1];
}

dir_count--;
printf("File deleted\n");
}
else
{
printf("File not found\n");
}
}
}

void display_directory()
{
int i;
printf("Directory : \n");

for(i=0 ; i < dir_count ; i++)


{
printf("Name : %s , Length : %d , Index block : %d\n", dir[i].file_name,
dir[i].file_size , dir[i].index_block_no);
}
}

int main()
{
int choice;
initialize_bit_vector();

do
{
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: printf("You chose to Exit !"); break;
default: printf("Invalid Choice\n");
}
}while(choice !=5);
}

You might also like