File Allocation
File Allocation
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct dir
{
char filename[20];
int startblock, fileLen;
}directory[50];
void initBV()
{
for(i=0;i<n;i++)
{
bv[i]=rand()%2;
}
}
void displayBV()
{
printf("\nBit vector Contents:\n");
for(i=0;i<n;i++)
{
printf("%d",bv[i]);
}
}
void createFile()
{
printf("\nEnter file name:");
scanf("%s",fname);
if(file_count>50)
{
printf("Error:Maximum no.of files reached\n");
return;
}
for(i=0;i<n;i++)
{
if(strcmp(directory[i].filename,fname)==0)
{
printf("File Name already exist...");
return;
}
}
no_free_blocks=0;
for(i=0;i<n;i++)
{
if(bv[i]==0)
{
no_free_blocks++;
}
}
if(freecount<blocks_required)
{
printf("Error:Not enough contigious free blocks to allocate the file!\n");
return;
}
for(i=SBlock;i<SBlock+blocks_required;i++)
{
bv[i]=1;
}
file_count++;
printf("File %s is created successfully..\n",fname);
}
void displayDirectory()
{
printf("Directory Contents:\n");
printf("%-20s %-12s %-7s %-15s\n", "FileName", "startBlock", "Length","Allocated Blocks");
for(i=0;i<file_count;i++)
{
printf("%-20s %-12d %-7d ", directory[i].filename, directory[i].startblock, directory[i].fileLen);
for (j=directory[i].startblock; j<directory[i].startblock+directory[i].fileLen; j++)
{
printf("%d, ",j);
}
printf("\n");
}
}
void deleteFile()
{
printf("\nEnter file name:");
scanf("%s",fname);
for(i=0;i<file_count;i++)
{
if(strcmp(directory[i].filename,fname)==0)
{
SBlock=directory[file_count].startblock;
blocks_required=directory[file_count].fileLen;
for(i=SBlock;i<SBlock+blocks_required;i++)
{
bv[i]=0;
}
break;
}
}
void main()
{
int choice=0;
printf("Enter the number of Disk Blocks:\n");
scanf("%d",&n);
no_free_blocks=n;
initBV();
do
{
printf("\n********Menu********\n");
printf("\n1.Show Bit Vector”):
printf("\n2.Create New File”);
printf("\n3.Show Directory\n”);
printf(“\n4.Delete File”);
printf("\n5.Exit\n");
printf("\nEnter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1: displayBV();
break;
case 2: createFile();
break;
case 3: displayDirectory();
break;
case 4: deleteFile();
break;
case 5: break;
Question 2: 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
• Delete File
• Exit
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct NODE
{
int data;
struct NODE *next;
};
struct direct
{
char filename[20];
int start, end;
struct NODE *listhead;
}directory[20];
void init_bitVector()
{
int i;
printf("Enter total no. of disk blocks : ");
scanf("%d",&n);
for (i=0;i<n;i++)
{
bv[i]=rand()%2;
}
file_count=0;
head=NULL;
}
void display_bitvector()
{
int i;
for (i=0; i<n; i++)
printf("%d ",bv[i]);
printf("\n");
}
void display_dir()
{
if (file_count==0)
{
printf("No file is created yet\n");
return;
}
printf("\nDirectory : ");
printf("\nFile Name Start Block Number End Block Number Allocated blocks\n");
for (i=0; i<file_count; i++)
{
printf("%s %10d %10d\t", directory[i].filename, directory[i].start, directory[i].end);
for (temp=directory[i].listhead; temp!=NULL; temp=temp->next)
{
printf("%d->",temp->data);
}
printf("NULL\n\n");
}
}
void createFile()
{
char fname[30];
no_free_blocks =0;
printf("Enter file name : ");
scanf("%s", fname);
printf("Enter the no of blocks : ");
scanf("%d",&blocks_required);
directory[file_count].end=i;
file_count++;
printf("File %s created successfully \n",fname);
}
void deleteFile()
{
int dirIndex;
char fname[30];
printf("Enter file name : ");
scanf("%s", fname);
// search for a file entry in the directory and de-allocate all blocks
for(i=0; i<file_count; i++)
{
if (strcmp(directory[i].filename, fname) == 0)
{
dirIndex=i;
temp = head = directory[i].listhead;
while ( temp != NULL)
{
bv[temp->data]=0;
temp = head;
head = head->next;
free(temp);
}
}
}
int main()
{
int choice;
init_bitVector();
while(1)
{
printf("\n*******Menu*******\n");
printf("\n1 : Show bit Vector");
printf("\n2 : Create New File");
printf("\n3 : Show Directory");
printf("\n4 : Delete file");
printf("\n5 : Exit\n");
printf("\nEnter Your Choice : \n");
scanf("%d",&choice);
switch (choice)
{
case 1: display_bitvector();
break;
case 2: createFile();
break;
case 3: display_dir();
break;
case 4: deleteFile();
break;
case 5: return;
default: printf("Invalid option\n");
}
}
}