In: Computer Science
Write source code in C to simulate the contiguous file allocation with the following conditions:
• Prompt the user to ender the no of files
• Enter the name of the file
• Enter the Starting block number
• Enter no of block occupied by the file
i.Condition: No two files must have the same
block(if the user enter the same block no present in the previous
file prompt the user “Block already in
use”)
#include<stdio.h>
#include<conio.h>
struct
{
char fname[10];
} f[10];
main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
printf("Enter no. of files: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file %d name: ", i+1);
scanf("%s", f[i].fname);
printf("Enter no. of blocks occupied by file %s: ",f[i].fname);
scanf("%d",&b[i]);
printf("Enter the starting block of file %s: ",f[i].fname);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
}
printf("Filename\tStart block\tlength\n");
for(i=0;i<n;i++)
printf("%s\t %d \t%d\n",f[i].fname,t[i],b[i]);
printf("blocks occupiedate:\n");
for(i=0;i<n;i++)
{
printf("file name %s ",f[i].fname);
for(j=0;j<b[i];j++)
printf("\t%d",c[i][j]);
printf("\n");
}
}
I've been trying to solve this question:
Condition: No two files must have the same block(if the user enter the same block no present in the previous file prompt the user “Block already in use”), "what should I write to implement IF CONDITION" in this part of question? I wrote the whole code but still I have to perform code with this condition.
At first according to the question, you should take the input of starting block of file before the no. of blocks to allocate. Because if the starting block lies in the blocks previoisly allocated, then we need to tell user that "Block already in use".
Also, if the starting block is valid but the block size overlaps with the other block previously allocated, this will also be a problem. So we need to check for this condition also.
So, the reading input section of your code will look like this:
for(i=0;i<n;i++)
{
printf("Enter file %d name: ", i+1);
scanf("%s", f[i].fname);
printf("Enter the starting block of file %s: ",f[i].fname);
scanf("%d",&sb[i]);
//check is starting block no. lies in any
// of the previously allocated blocks.
int flag = 0;
for(int k = 0; k < i; k++){
//if starting block no. lies within the block already present
if(sb[i] >= sb[k] - b[k] && sb[i] <= sb[k]){
//change in flag value will indicate
//that block is already allocated
flag = 1;
break;
}
}
if(flag){
printf("Block already in use!\n");
i--;
continue;
}
printf("Enter no. of blocks occupied by file %s: ",f[i].fname);
scanf("%d",&b[i]);
//Check if the block size allocated do not overlap
// with previously allocated block
flag = 0;
for(int k = 0; k < i; k++){
//if block overlaps with the block already present
if(sb[i] + b[i] >= sb[k] - b[k] && sb[i] + b[i] <= sb[k]){
//change in flag value will indicate
//that block is already allocated
flag = 1;
break;
}
}
if(flag){
printf("Block already in use!\n");
i--;
continue;
}
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
}
The output is:
If this helps you, do upvote as it motivates us a lot!