C program:
#include<stdio.h>
#include<malloc.h>
int Fnm[10],Fsz[10],strt[10],F_start[10],F_end[10];
int n=0,m=0;
int cr(int,int);
int dl(int);
int dsp();
int cr(int nm,int sz)
{
int i,flag=1,j;
for(i=0;i<=m;i++)
if( (F_end[i]-F_start[i]) >= sz)
{
flag=0;
}
if(!flag)
{
for(j=0;j<n;j++);
n++;
Fnm[j]=nm;
Fsz[j]=sz;
strt[j]=F_start[i];
F_start[i]+=sz;
printf("\n After allocation of this file/process,The FAT will be =>> \n\n");
dsp();
}
else
printf("\nNo enough space.\n");
}
int dl(int nm)
{
int i,j,k,flag=1;
for(i=0;i<n;i++)
if(Fnm[i]==nm)
break;
if(i==n)
{
flag=0;
printf("\nFile not found.\n");
}
else
{
for(j=0;j<=m;j++)
if(strt[i]==F_end[j])
break;
if(j<=m)
F_end[j]+=Fsz[i];
else
{
F_start[j]=strt[i];
F_end[j]=strt[i]+Fsz[i];
m++;
}
for(k=i;k<n;k++)
Fnm[k]=Fnm[k+1],Fsz[k]=Fsz[k+1],strt[k]=strt[k+1];
n--;
}
if(flag)
{
printf("\n\n After deletion of this file/process ,FAT will be =>>\n\n");
dsp();
}
}
int dsp()
{
int i;
printf("\n\n NAME SIZE START ADDR\n\n");
for(i=0;i<n;i++)
printf(" | %d | %d | %d |\n",Fnm[i],Fsz[i],strt[i]);
printf("\n\n");
}
int main()
{
int nm,sz,h;
F_start[0]=0,F_end[0]=999;
do
{
printf("\t\t\t1.Create file.\n");
printf("\t\t\t2.Delete file.\n");
printf("\t\t\t3.Exit.\n");
printf("\t\t\tChoice ==> ");
scanf("%d",&h);
if(h==1){
printf("\nThe name of file or process ==>> ");
scanf("%d",&nm);
printf("\nThe size of the file or process ==>> ");
scanf("%d",&sz);
cr(nm,sz);
}
if(h==2){
printf("\nWhich file or process want to delete ==>> ");
scanf("%d",&nm);
dl(nm);
}
if(h!=1 && h!=2 && h!=3)
printf("\nWrong choice ==> Try again.\n");
}while(h!=3);
}
//Output Of the above program:-
#include<stdio.h>
#include<malloc.h>
int Fnm[10],Fsz[10],strt[10],F_start[10],F_end[10];
int n=0,m=0;
int cr(int,int);
int dl(int);
int dsp();
int cr(int nm,int sz)
{
int i,flag=1,j;
for(i=0;i<=m;i++)
if( (F_end[i]-F_start[i]) >= sz)
{
flag=0;
}
if(!flag)
{
for(j=0;j<n;j++);
n++;
Fnm[j]=nm;
Fsz[j]=sz;
strt[j]=F_start[i];
F_start[i]+=sz;
printf("\n After allocation of this file/process,The FAT will be =>> \n\n");
dsp();
}
else
printf("\nNo enough space.\n");
}
int dl(int nm)
{
int i,j,k,flag=1;
for(i=0;i<n;i++)
if(Fnm[i]==nm)
break;
if(i==n)
{
flag=0;
printf("\nFile not found.\n");
}
else
{
for(j=0;j<=m;j++)
if(strt[i]==F_end[j])
break;
if(j<=m)
F_end[j]+=Fsz[i];
else
{
F_start[j]=strt[i];
F_end[j]=strt[i]+Fsz[i];
m++;
}
for(k=i;k<n;k++)
Fnm[k]=Fnm[k+1],Fsz[k]=Fsz[k+1],strt[k]=strt[k+1];
n--;
}
if(flag)
{
printf("\n\n After deletion of this file/process ,FAT will be =>>\n\n");
dsp();
}
}
int dsp()
{
int i;
printf("\n\n NAME SIZE START ADDR\n\n");
for(i=0;i<n;i++)
printf(" | %d | %d | %d |\n",Fnm[i],Fsz[i],strt[i]);
printf("\n\n");
}
int main()
{
int nm,sz,h;
F_start[0]=0,F_end[0]=999;
do
{
printf("\t\t\t1.Create file.\n");
printf("\t\t\t2.Delete file.\n");
printf("\t\t\t3.Exit.\n");
printf("\t\t\tChoice ==> ");
scanf("%d",&h);
if(h==1){
printf("\nThe name of file or process ==>> ");
scanf("%d",&nm);
printf("\nThe size of the file or process ==>> ");
scanf("%d",&sz);
cr(nm,sz);
}
if(h==2){
printf("\nWhich file or process want to delete ==>> ");
scanf("%d",&nm);
dl(nm);
}
if(h!=1 && h!=2 && h!=3)
printf("\nWrong choice ==> Try again.\n");
}while(h!=3);
}
//Output Of the above program:-
Vest Nice blog for learning new things,thanks for such beautiful blog.
ReplyDeletebelow some new idea plz check once.
kajal hot