#include<conio.h>
#include<string.h>
void create();
void deletef();
void display();
int callrand();
struct fat
{
char name[15]; // structure to store file name
int size,start; // size of the file and Staring address of the file
}x[51];
struct mem // consider this as memory unit of 1000 units this is a linked list
{
int alo; // alo is used to identify whether that particular block is allotted or not;
int next; // next is used to store the next block address (similar to linked list)
}m[1000];
int no=1,used=0,low=0,high=1000,num=0;
// no variable is used for files count;
// here we are considering the total disk size as 1000 units so the variable ' high=1000'
// low is initially 0 where
// used variable is help ful to know how many blocks are filled in total 1000 blocks
// that means 1000- used gives the empty units of memory in the disk.
void main()
{ do{
int cho,i;// cho is used to store th user choice, i is used for calculation purpose
textcolor(GREEN);
clrscr();
x[0].size=0; // since it is a linked memory the 1st file size and stating position should be zero
x[0].start=0; // thats why no is declared as 1 at 18 th line
printf("\n\t\t\tLINKED MEMORY ALLOCATION ");
printf("\n\n FILE MENU");
printf("\n\n 1.Create New File\n 2.Delete File\n 3.Display File\n 4.Exit");
printf("\n\n Enter Your option:");
scanf("%d",&cho);
while(cho<0 || cho>4) // here the checking takes place whether the user choice is valid or not, only if valid it allows for next step
{ printf("\n Enter Valid Option:");
scanf("%d",&cho);
};
switch(cho)
{
case 1 : clrscr();
printf("\n\t\tCreating New File..\n");
create();
break;
case 2 : clrscr();
printf("\n\t\tDeleting File..\n");
deletef();
break;
case 3 : clrscr();
printf("\n\t\tDisplaying File..\n");
display();
break;
case 4 : exit();
}
}while(1);
}
void create() // create is the function used for crating of new file and allotting memory to it
{
int nl,tsize,cl=0,i;
if(no<=50 && used<=1000)// this program supports a maximum of 50 files and total size of 1000 units
// so at creation of each file this 2 limitations should be checked
{
printf("\nEnter the File Name:");
scanf("%s",&x[no].name); // reading file name
printf("\nEnter the File Size:");
scanf("%d",&x[no].size); // asking file size
tsize=x[no].size; // storing the file size to a temporary variable tsize for calculation purpose
if(x[no].size > 1000-used) // if the new file size is less than available memory units in the disk then only it should allow to create a new file
{
printf("\nDisk DO NOT have Enough MEMORY to save the file !");
}
else
{ do
{
nl=callrand(); // each memory block is assigned to a file in random manner
// callrand() function is used to get the random number
if(m[nl].alo==0) // if obtained random block is already allotted then it should not be use..!!
{ if(cl!=0)
m[cl].next=nl;
if(cl==0)
x[no].start=nl;
cl=nl;
m[cl].alo=1;
tsize--; // when ever a block is allotted to file the reaming size of the file to be allotted will be decreased by 1 unit
used++;
}
}while(tsize>0); // this process repeats until the total required blocks of the file is allotted to it
printf("\nFile Sucessfully Created by Name ' %s '",x[no].name);
num++;// this num is used to store number of user files created
no++; // then the files number will be increased by 1
}
}
else
printf("\nMemory or Files Limitation is Exceed");
getch();
}
int callrand()
{
int z;
z= low+rand()%high; // this line returns the random value between low and high
return z;
}
void deletef()
{ if(num>0) // if user created files are greater then 0 then only it goes for deletion, otherwise it means disk is empty
{ char na[15];
int i,rep,z,fflag=0; // rep is used for how many tmes the loop should be repeated to clean the all blocks that belongs to that file
//fflag is used to know whether the required name was found or not , if ffound=0 file not found, else found
printf("\nEnter File Name You want to Delete:");
scanf("%s",&na); // reading the file name to be deleted
i=0; // searching the file name in fat structure if that file is really exist or not
do
{
if(strcmp(na,x[i].name) == 0)//strcmp() is used to compare name of the file
{ fflag=1; // if file found the foundflag (fflag) will be changed to 1
rep=x[i].size; //rep is initialized to size because that many blocks are about to clean
z=x[i].start; // z is used to store the next address of linked list
do
{
m[z].alo=0; // alo is changed to 0, means making the block free
z=m[z].next; // linked list pointer is pointed to next address
rep--;
}while(rep!=0);
used-=x[i].size;
x[i].name[0] = '/0'; // crealing the name of the file in the fat list
printf("\nFile by Name ' %s ' was Sucessfully Deleted.",na);
num--; // after deleting the file users files count should be reduced by 1
}
i++;
}while(i<=no && fflag==0); // if no filein disk or file not found
i=0;
if(fflag==0)
printf("\nNo File Found by Name ' %s ' to Delete !",na);
}
else
printf("Disk is Empty !");
getch();
}
void display() // display() function is used to display block address used by file
// this is similar to delete but whit out clearing the block just displays the block id
{
char na[15];
int i,fflag=0,j,k;
if(num>0)
{ printf("Enter file name you Want to Display:");
scanf("%s",&na);
for(i=0;i<=no;i++)
{
if(strcmp(na,x[i].name) == 0)
{ fflag=1;
printf("\n\nName= %s",x[i].name);
printf("\nSize= %d ",x[i].size);
printf("\nStarting Addess= %d",x[i].start);
printf("\nMemory Allocation in Linked form: ");
k=x[i].start;
for(j=1;j<=x[i].size;j++)
{
printf("-->%d",k);
k=m[k].next;
}
}
}
if(fflag==0)
printf("\No File Found by Name ' %s ' to Display !",na);
}
else
printf("\nDisk is Empty !");
getch() ;
}