Linked file allocation (Oparating system)
1) Write a program to simulate Linked file allocation method. Assume disk with n number of blocks. Give value of n as input. Write menu driver program with menu options as mentioned above and implement each option.
linked file allocation
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void show_bitvector();
int search(char *);
void show_directory();
void create_file();
void delete_file();
struct FAT
{
char fname[20];
int ib;
struct FAT *next;
}*head=NULL;
int *bitvector,total_block;
int main()
{
int i,ch;
printf("\n Enter the no.of blocks: ");
scanf("%d",&total_block);
bitvector=(int *)malloc(total_block*(sizeof(int)));
for(i=0;i<total_block;i++)
{
bitvector[i]=0;
}
while(1)
{
printf("\n*************MENU*****************\n 1.show bit vector\n 2.create new file\n 3.show Directory\n 4.delete file\n 5.exit\n");
printf("Enter the choice\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
show_bitvector();
break;
case 2:
create_file();
break;
case 3:
show_directory();
break;
case 4:
delete_file();
break;
case 5:
exit(0);
default:
printf("wrong choice\n");
}
}
}
void show_bitvector()
{
int i;
for(i=0;i<total_block;i++)
{
printf("%d\t",bitvector[i]);
}
}
int search(char f[20])
{
int c=1;
struct FAT*temp;
for(temp=head;temp!=NULL;temp=temp->next,c++)
{
if(strcmp(f,temp->fname)==0)
{
return c;
}
}
return -1;
}
void create_file()
{
struct FAT*temp,*NN;
char f[20];
int size,x,i,j,k,free=0,initial;
printf("Enter the file name do you wants to create\n");
scanf("%s",f);
printf("Enter the size of the file\n");
scanf("%d",&size);
x=search(f);
if(x!=-1)
{
printf("The file is allready exist\n");
return;
}
//for finding the continuous free blocks
for(i=0;i<total_block;i++)
{
if(bitvector[i]==0)
{
free++;
}
}
//to check whether there is a sufficent memory for allocating a file or not;
if(free<size+1)
{
printf("insufficient memory to allocate the file\n");
return;
}
//to find out the first free block;
for(i=0;i<total_block;i++)
{
if(bitvector[i]==0)
{
break;
}
}
initial=i;
//to find out the next free block and add the address of next block in the first block
for(j=i+1;j<total_block;j++)
{
if(bitvector[j]==0)
{
bitvector[i]=j;
i=j;
size--;
}
if(size==0)
{
bitvector[i]=-1;
break;
}
}
NN=(struct FAT*)malloc(sizeof(struct FAT));
strcpy(NN->fname,f);
NN->ib=initial;
NN->next=NULL;
if(head==NULL)
{
head=NN;
}
else
{
for(temp=head;temp->next!=NULL;temp=temp->next);
temp->next=NN;
}
printf("file created sucessfully\n");
show_bitvector();
}
void show_directory()
{
struct FAT*temp;
printf("\n********** file allocation table ***********\n");
printf("\n File Name \t Initial Block");
for(temp=head;temp!=NULL;temp=temp->next)
{
if(temp==NULL)
{
printf("Directory is empty\n");
return;
}
printf("\n%s\t\t %d\n",temp->fname,temp->ib);
}
}
void delete_file()
{
struct FAT*temp,*prev;
char f[20];
int x,i,size,initial;
printf("Enter the file name to be delete\n");
scanf("%s",f);
x=search(f);
if(x==-1)
{
printf("File not found\n");
return;
}
if(x==1)
{
initial=head->ib;
temp=head;
head=head->next;
temp->next=NULL;
free(temp);
}
else
{
for(prev=head,i=1;i<x-1;i++,prev=prev->next);
temp=prev->next;
prev->next=temp->next;
temp->next=NULL;
initial=temp->ib;
free(temp);
}
for(i=initial;i<total_block;)
{
x=bitvector[i];
bitvector[i]=0;
i=x;
if(bitvector[i]==-1)
{
bitvector[i]=0;
break;
}
}
printf("File Deleted Sucessfully\n");
show_bitvector();
}
Comments
Post a Comment