Sie sind auf Seite 1von 9

Contiguous file allocation

#include <bits/stdc++.h>
#define ll long long int
#define SPACE 50
using namespace std;
bool mem[SPACE];
int free_start;

struct FAT
{
int fsize, fstart = -1;
string fname;
};

vector<FAT> table;

void allocate(string name, int size)


{
FAT *new_file = new FAT;
new_file->fname = name;
new_file->fsize = size;
// cout << size;

int i = 0;
bool got = false;
while (i < SPACE && !got)
{
int free = 0;
int start;
if (!mem[i])
{
start = i;
while (i < SPACE && !mem[i])
{
free++;
i++;
if (free >= size)
{
// cout << free << endl;
new_file->fstart = start;
got = true;
break;
}
}
}
if (got)
{
for (int i = start; i < (start + size); i++)
mem[i] = true;
table.push_back(*new_file);
if (start > free_start)
free_start = start + size;
delete (new_file);
break;
}
i++;
}
if (got == false)
cout << "Memory has not been allocated to this file\n";
}

bool com(FAT a, FAT b)


{
return (a.fstart < b.fstart);
}

void deallocate(string name)


{
int idx, size;
bool got = false;
for (int i = 0; i < table.size(); i++)
{
if (table[i].fname == name)
{
idx = table[i].fstart;
size = table[i].fsize;
got = true;
table.erase(table.begin() + i);
}
}
if (!got)
{
cout << "File does not exist!\n";
return;
}
for (int i = idx; i < (idx + size); i++)
{
mem[i] = false;
}
}

void display()
{
cout << "-----FILE ALLOCATION TABLE-----\n";
cout << "file Name Starting address Size \n";
for (int i = 0; i < table.size(); i++)
{
if (table[i].fstart != -1)
cout << table[i].fname << " " << table[i].fstart<<" "<<
table[i].fsize << endl
}
for (int i =0; i<table.size(); i++)
{
if(table[i].fstart != -1)
{
int j;
cout<<"Blocks occupied by "<<table[i].fname<<" :";
for( j = table[i].fstart ; j < table[i].fstart + table[i].fsize ; j++)
cout<< j<< " ";
cout<<endl;
}
}
cout << endl;
}

void Defragmentation()
{
sort(table.begin(), table.end(), com);
int j = 0;
for (int i = 0; i < table.size(); i++)
{
if (j != table[i].fstart)
{
table[i].fstart = j;
}
j = j + table[i].fsize;
}
j = 0;
for (int i = 0; i < table.size(); i++)
{
j = table[i].fstart;
while (j < (table[i].fstart + table[i].fsize))
{
mem[j] = true;
j++;
}
}
while (j < SPACE)
{
mem[j] = false;
j++;
}
}

int main()
{
int n;
memset(mem, false, sizeof(mem));
free_start = 0;
cout << "Contigous File Allocation\n";
cout << "Enter no. of file :";
cin >> n;
int size;
string name="";
for( int i =0 ; i < n; i++ )
{
cout << "Enter no. of blocks the occupied by file"<<i<<" :";
cin >> size;
cout << "Enter the name of the file :";
cin >> name;
allocate(name,size);
}
display();
return 0;
}

OUTPUT:
Linked file allocation
#include<bits/stdc++.h>
#define SPACE 50
using namespace std;

struct FAT
{
int fsize, fstart=-1;
string fname;
};

vector<FAT> table;
bool mem[SPACE];
int nt[SPACE];

void allocate(string name,int size)


{
FAT *new_file = new FAT;
new_file->fname = name;
new_file->fsize = size;
vector<int> path;
bool first = false;
for(int i=0;i<SPACE && size;i++)
{
if(!mem[i])
{
if(!first)
{
new_file->fstart = i;
first = true;
table.push_back(*new_file);
}
path.push_back(i);
mem[i] = true;
size--;
}
}
int j;
for( j=0;j<path.size()-1;j++)
nt[path[j]] = path[j+1];
nt[path[path.size()-1]] = -1;
path.clear();
delete(new_file);
}

void deallocate(string name)


{
int idx, size;
bool got = false;
for (int i = 0; i < table.size(); i++)
{
if (table[i].fname == name)
{
idx = table[i].fstart;
size = table[i].fsize;
got = true;
table.erase(table.begin() + i);
}
}
if (!got)
{
cout << "File does not exist!\n";
return;
}
int i = idx;
while(nt[i]!= -1)
{
mem[i] = false;
i = nt[i];
}
mem[i] = false;
}

void display()
{
cout<<"-----FILE ALLOCATION TABLE-----\n";
cout<<"file name Starting address size\n";
for(int i=0;i<table.size();i++)
{
if(table[i].fstart!=-1)
cout<<table[i].fname<<" "<<table[i].fstart<<" "<<table[i].fsize<<endl;
}
for(int i=0;i<table.size();i++)
{
if(table[i].fstart!=-1)
{
cout<<"Blocks occupied by "<<table[i].fname<<" :";
for(int j = table[i].fstart; j< table[i].fstart + table[i].fsize ; j++ )
{
if(j != table[i].fstart + table[i].fsize -1)
cout << j << "->";
else
cout << j<<endl;
}
}
}
/*cout<<"BIT vector\n";
for (int i = 0; i < SPACE; i++)
cout << mem[i]<<" ";
cout << endl;
for (int i = 0; i < SPACE; i++)
cout << nt[i] << " ";*/

int main()
{
int n;
memset(mem,false,sizeof(mem));
memset(nt,0,sizeof(nt));
cout << "Linked File Allocation\n";
cout << "Enter the no. of files :";
cin >> n;
int size;
string name;
for(int i=0;i<n;i++)
{
cout <<"Enter the no. of blocks occupied by the file :";
cin >> size;
cout <<"Enter the name of the file :";
cin >> name;
allocate(name,size);
}
display();

OUTPUT:

Das könnte Ihnen auch gefallen