You are on page 1of 3

#include<stdio.

h>
#include<stdbool.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define block 100

void contiguous(int *f)


{
int i, start, length, j, c, k, count = 0;
printf("Enter starting block and length of files: ");
scanf("%d%d", &start,&length);
for(k=start;k<(start+length);k++)
if(f[k]==0)
count++;
if(length==count)
{
for(j=start;j<(start+length);j++)
if(f[j]==0)
{
f[j]=1;
printf("...%d",j);
}
if(j!=(start+length-1))
printf("\nThe file is allocated to disk\n\n");
}
else
printf(" The file is not allocated beacause already allocated\n\n");
}
void indexed(int *f)
{
int index[block],i, n, start, length, j, c, k, ind,count=0;
printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk : \
n", ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated \n",ind);
}
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d......%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed");
}
}
void linked(int *f)
{
int p,i, start, length, j, c, k, a;
printf("Enter how many blocks already allocated: ");
scanf("%d",&p);
printf("Enter blocks already allocated: ");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
printf("Enter index startarting block and lengthgth: ");
scanf("%d%d", &start,&length);
k=length;
if(f[start]==0)
{
for(j=start;j<(start+k);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("...%d",j);
}
else
{
printf("%d Block is already allocated \n",j);
k++;
}
}
}
else
printf("\n%d startarting block is already allocated \n",start);
}

int main()
{
bool c=true;
int ch;
int *f=(int *)malloc (block*sizeof(int));
while(c)
{
printf("\n\t1.Contiguous Allocation\n\t2.Indexed Allocation\n\t3.Linked Allocation\
n\t4.Exit\n");
printf("Enter the method:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
contiguous(f);
break;
}
case 2:
{
indexed(f);
break;
}
case 3:
{
linked(f);
break;
}
case 4:
{
free(f);
c=false;
printf("Exitting....\n");
break;
}
default:
{
}
}
}
}

You might also like