A2_Linked
A2_Linked
Assume Disk of
size 'd', value of which should be taken from user. Use seperate tables(implemented
using linked lists) to keep tack of Used & Free space respectively. Make use of the
following Menu to perform Operations:
// - Allocate space for newly created file.
// - Deallocate space for now-deleted file.
// - Show Used and/or Free Space on Disk.
// - Exit
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct File_tab
{
char name[10];
int start,end;
struct File_tab *next;
}File;
typedef struct BLOCK
{
int data;
int next;
}block;
File *first=NULL;
block *disk;
int ch,i,fcnt,acnt,size;
//initialization of disk blocks
void init()
{
for(i=0;i<size;i++)
{
disk[i].next=-1;
disk[i].data=0; //to free block
}
}
int search() //s is size of file
{
int cnt=0,j;
for(i=0;i<size;i++) //blcok size
{
if(disk[i].data==0) //allocated
return i;
}
return -1;
}
void allocate()
{
File *f1,*f2; //file table implementation
int res,fsize,j,cnt;
f1=(File *)malloc (sizeof(File));
printf("\nEnter the file name ");
scanf("%s",&(f1->name));
printf("\nEnter the file size ");
scanf("%d",&(fsize));
if(fcnt<fsize) //freecnt
printf("\nMemeory not availbale");
else
{
res=search(); //index of free block
f1->start=res;
f1->next=NULL;
cnt=0;
disk[res].data=1; //allocated
fcnt--;
acnt++;
for(i=res;i<size;i++)
{
j=i+1;
for(;cnt<fsize-1;j++)
{
if(disk[j].data==0)
{
disk[i].next=j;
disk[j].data=1;
fcnt--;
acnt++;
cnt++;
i=j;
}//if
}//for j
if(cnt==fsize-1)
{
f1->end=j-1;
disk[j-1].next=-1; //last block
break;
}
}//for i;
//table
if(first==NULL) //first entry
first=f1;
else
{
f2=first; //add entry at last
while(f2->next!=NULL)
f2=f2->next;
f2->next=f1;
}
}
}
void show()
{
File *ptr;
ptr=first;
main()
{
printf("Enter the size of disk ");
scanf("%d",&size);
fcnt=size;
acnt=0;
disk=(block *)malloc(sizeof(block)*size);
init();
while(1)
{
printf("\n\n\tMenu \n\n\t1.Allocate the memory for file\n\n\t2.Deallocate");
printf("\n\n\t3.show used and free space from disk \n\n\t4.Exit ");
printf("\n\n\tEnter the choice ");
scanf("%d",&ch);
switch(ch)
{
case 1: allocate();
break;
case 2 :deallocate();
break;
case 3 :show();
break;
case 4: exit(0);
}
}
}