Problem
No: 04
Name
of the problem: Sequential file Allocation
Theory:
Sequential allocation is a
simple and attractive mechanism for the allocation of indivisible goods. Agents
take turns, according to a policy, to pick items. Sequential allocation is
guaranteed to return an allocation that is efficient but may not have optimal social welfare.
Code:
#include<bits/stdc++.h>
using namespace std;
struct filetable
{
char name[20];
int sb,nob;
} ft[30];
int main()
{
int i,j,n;
char s[20];
cout<<"enter the no of files
";
cin>>n;
for(i=0; i<n; i++)
{
cout<<"enter file name
"<<i+1<<": ";
cin>>ft[i].name;
cout<<"enter starting block
of file "<<i+1<<": ";
cin>>ft[i].sb;
cout<<"enter no of blocks in
the file "<<i+1<<": ";
cin>>ft[i].nob;
}
cout<<"enter the file name to be
searched : ";
cin>>s;
for(i=0; i<n; i++)
if(strcmp(s,ft[i].name)==0)
break;
if(i==n)
cout<<"file not found.";
else
{
cout<<"file name\tstart block\t
no of blocks\t blocks occupied"<<endl;
cout<<ft[i].name<<"\t\t
"<<ft[i].sb<<"\t\t
"<<ft[i].nob<<"\t\t ";
for(j=0; j<ft[i].nob; j++)
{
cout<<ft[i].sb+j;
if(j<ft[i].nob-1)
{
cout<<",";
}}}
}
Problem
No: 05
Name
of the problem: Linked file allocation
Theory: In this scheme, each file is a linked list of disk
blocks that need not be contiguous. The disk blocks can be scattered anywhere
on the disk. The directory entry contains a pointer to the starting and the
ending file block. Each block contains a pointer to the next block occupied by
the file.
Code:
#include<bits/stdc++.h>
using namespace std;
struct fileTable
{
char name[20];
int nob;
struct block *sb;
}ft[30];
struct block
{
int bno;
struct block *next;
};
int main()
{
int i,j,n;
char s[20];
struct block *temp;
printf("Enter no of files: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
cout<<"enter file name
"<<i+1<<": ";
cin>>ft[i].name;
cout<<"enter no of blocks in
the file "<<i+1<<": ";
cin>>ft[i].nob;
ft[i].sb=(struct
block*)malloc(sizeof(struct block));
temp=ft[i].sb;
cout<<"enter the blocks of
the file : ";
scanf("%d",&temp->bno);
temp->next=NULL;
for(j=1;j<ft[i].nob;j++){
temp ->next =(struct
block*)malloc(sizeof(struct block));
temp=temp->next;
scanf("%d",&temp->bno);
}
temp->next=NULL;
}
printf("\n Enter the file to be
searched...");
scanf("%s",s);
for(i=0;i<n;i++)
if(strcmp(s,ft[i].name)==0)
break;
if(i==n)
printf("\nfile not found");
else{
printf("\n FILE NAME NO OF BLOCKS BLOCKS OCCUPIED");
printf("\n
%s\t\t%d\t\t",ft[i].name,ft[i].nob);
temp=ft[i].sb ;
for(j=0;j<ft[i].nob;j++)
{
cout<<temp->bno;
temp=temp->next;
if(j<ft[i].nob-1)
{
cout<<"->";
}
}
}
}
Problem
No: 06
Name
of the problem: Indexed file allocation
Theory:
Indexed
Allocation. Provides solutions to problems of contiguous and linked allocation.
An index block is created having all pointers to files. Each file has its own
index block which stores the addresses of disk space occupied by the file. The directory contains the addresses of index blocks of files.
Code:
#include<bits/stdc++.h>
using namespace std;
struct filetable
{
char name[20];
int nob,blocks[30];
}ft[30];
int main()
{
int i,j,n;
char s[20];
cout<<"enter the no of files:
";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"enter file
name"<<i+1<<": ";
cin>>ft[i].name;
cout<<"enter no of blocks in
the file "<<i+1<<": ";
cin>>ft[i].nob;
cout<<"enter the blocks of
file: ";
for(j=0;j<ft[i].nob;j++)
cin>>ft[i].blocks[j];
}
cout<<"enter the file name to be
searched: ";
cin>>s;
for(i=0;i<n;i++)
if(strcmp(s,ft[i].name)==0)
break;
if(i==n)
cout<<"file not found
";
else
{
printf("\nfile name no of blocks blocks occupied");
printf("\n %s\t\t%d\t",ft[i].name,ft[i].nob);
for(j=0; j<ft[i].nob; j++)
{
printf("%d",ft[i].blocks[j]);
if(j<ft[i].nob-1)
{
cout<<",";
}
}
}
}
No comments:
Post a Comment