PROGRAM CODING to implement File allocation
#include<stdio.h>
#include<stdlib.h>
struct list
{
int datano;
char *name;
struct list *next;
}*hf,*lf,*ha[10],*la[10];
int m,n;
int size;
struct file
{
char name[20];
int size;
int nob;
}f[10];
struct index
{
int datano;
char *name;
struct list **link;
struct index *next;
}*h,*l;
int a[200],g=0;
void createfree()
{
int i;
hf=(struct list*)malloc(sizeof(struct list));
hf->next=NULL;
for(i=1;i<=m;i++)
{
struct list*node=(struct list*)malloc(sizeof(struct list));
node->datano=i;
node->name=NULL;
node->next=NULL;
if(i==1)
hf->next=node;
else
lf->next=node;
lf=node;
}
}
void createind()
{
int i;
h=(struct index*)malloc(sizeof(struct index));
h->next=NULL;
for(i=0;i<n;i++)
{
struct index *node=(struct index*)malloc(sizeof(struct index));
node->datano=i;
node->name=NULL;
node->next=NULL;
node->link=NULL;
if(i==0)
h->next=node;
else
l->next=node;
l=node;
}
}
void read()
{
int i;
printf("\nEnter the no of blocks in the disk:--\n");
scanf("%d",&m);
printf("\nEnter the size of each block:--\n");
scanf("%d",&size);
printf("\nEnter the number of files:--\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the file of %d name:--",i+1);
scanf("%s",f[i].name);
printf("\nEnter the size of file %d:--",i+1);
scanf("%d",&f[i].size);
if(f[i].size%size==0)
f[i].nob=f[i].size/size;
else
f[i].nob=f[i].size/size+1;
}
}
void gen(int z)
{
int i=0,r,j;
srand(z);
for(i=0;i<200;i++)
{
r=(rand()%200+1);
for(j=0;j<i;j++)
if(r==a[j])
break;
if(j==i)
{
a[i]=r;
}
else
i--;
}
}
void contiguous()
{
int i,j,g=1;
struct list *temp;
for(i=0;i<n;i++)
{
ha[i]=(struct list*)malloc(sizeof(struct list));
for(j=0;j<f[i].nob;j++)
{
struct list *node=(struct list*)malloc(sizeof(struct list));
node->datano=g;
g++;
node->name=f[i].name;
node->next=NULL;
if(j==0)
ha[i]->next=node;
else
la[i]->next=node;
la[i]=node;
}
}
temp=hf;
for(i=0;i<g;i++)
temp=temp->next;
hf->next=temp;
}
void dele(int r)
{
struct list *t;
for(t=hf;t!=NULL;t=t->next)
if(t->next->datano==r)
{
t->next=t->next->next;
break;
}
}
void linked()
{
int r=1,i,j,k=0;
struct list *t;
for(i=0;i<n;i++)
{
ha[i]=(struct list*)malloc(sizeof(struct list));
for(j=0;j<f[i].nob;j++)
{
r=a[k];
k++;
dele(r);
struct list *node=(struct list*)malloc(sizeof(struct list));
node->datano=r;
node->name=f[i].name;
node->next=NULL;
if(j==0)
ha[i]->next=node;
else
la[i]->next=node;
la[i]=node;
}
}
}
void indexed()
{
int r=1,i,j,k=0;
struct index *t;
for(i=0;i<n;i++)
{
ha[i]=(struct list*)malloc(sizeof(struct list));
for(j=0;j<f[i].nob;j++)
{
a:
r=a[k];
k++;
if(r<(n+1))
goto a;
dele(r);
struct list*node=(struct list*)malloc(sizeof(struct list));
node->datano=r;
node->name=f[i].name;
node->next=NULL;
if(j==0)
ha[i]->next=node;
else
la[i]->next=node;
la[i]=node;
}
}
t=h->next;
for(i=0;i<n;i++)
{
t->name=f[i].name;
t->datano=i+1;
t->link=&ha[i];
t=t->next;
}
}
void contdis()
{
int i;
printf("\n\n\t Filename\t start\t Length\n");
for(i=0;i<n;i++)
{
printf("\t\t%s\t",f[i].name);
printf("\t%d\t\t",ha[i]->next->datano);
printf("%d\n",f[i].nob);
}
}
void linkdis()
{
int i,j;
printf("\n\n\t Filename\t start\t End\n");
for(i=0;i<n;i++)
{
printf("\t\t%s\t\t",f[i].name);
printf("%d\t",ha[i]->next->datano);
printf("%d\n",la[i]->datano);
}
struct list *t;
printf("\n\n INDIVIDUAL FILE LISTING:-");
printf("\n\t Filename \t datablocks\n");
for(i=0;i<n;i++)
{
printf("\n%s",f[i].name);
for(t=ha[i]->next;t!=NULL;t=t->next)
{
printf("-->%d",t->datano);
}
}
}
void indexdis()
{
int i;
struct index *temp;
temp=h->next;
printf("\n\t FILE NAME \t BLOCK INDEXED\n");
for(i=0;i<n;i++)
{
printf("\t\t%s",f[i].name);
printf("\t\t\t%d\n",temp->datano);
temp=temp->next;
}
struct list *t;
printf("\n\n INDEX TABLE LISTING:-");
printf("\n FILENAME INDEXED BLOCK");
for(i=0;i<n;i++)
{
printf("\n%s \n",f[i].name);
for(t=ha[i]->next;t!=NULL;t=t->next)
printf("\t\t%d\n",t->datano);
}
}
int main()
{
int z,ch;
read();
createfree();
gen(z);
do
{
printf("\n\tFILE ALLOCATION TECHNIQUES\n1.CONTIGUOUS\n2.LINKED\n3.INDEXED\n4.EXIT\n\tEnter your choice:-");
scanf("%d",&ch);
switch(ch)
{
case 1:
contiguous();
contdis();
createfree();
break;
case 2:
gen(z);
z++;
linked();
linkdis();
createfree();
break;
case 3:
gen(z);
z++;
createind();
indexed();
indexdis();
createfree();
break;
case 4:
exit(1);
}
}while(ch!=4);
}
No comments:
Post a Comment