Write a C program to implement linked file allocation by considering the following assumptions and given sample input and output.
Note:
Assume that the free disk space that is available is between 1 and 100. And the blocks are linked in order i.e 1 is linked to 2, 2 is linked to 3 ….. and 99 is linked to 100. If a certain block is allocated to a file, say 4, then 3 will be linked to 5.
Assume that 'n' blocks are already allocated to files. Write a C program to allocate space for 1 file using Linked File Allocation. Always assume that the starting block is a free block.
Input format:
Input consists of a number of blocks already allocated ( int ), n allocated block numbers ( int ), name ( string), Start of the block ( int ), and length ( int ) of the file to be allocated disk space. Enter allocated block numbers in ascending order.
Output format:
The output consists of file allocation details using linked allocation technique after allocating the input file.
[ Refer Sample Input and Output for further details ]
Sample Input and Output:
[ All text in bold corresponds to input ]
Enter the number of allocated blocks
6
Enter the block numbers of allocated blocks
4
6
7
10
22
27
Enter the name of the file
F1
Enter the starting block allocated to the file
3
Enter the length of the file
5
F1 File Allocation Details
3-> Allocated to F1
4-> Already Allocated
5-> Allocated to F1
6-> Already Allocated
7-> Already Allocated
8-> Allocated to F1
9-> Allocated to F1
10-> Already Allocated
11-> Allocated to F1
Answer:
#include<stdio.h> int main() { int n,bl,arr[100],le,k,start,i; char name[100]; printf("Enter the number of allocated blocks\n"); scanf("%d",&n); printf("Enter the block numbers of allocated blocks\n"); for(i=0;i<100;i++){ arr[i]=0; } for(i=1;i<=n;i++) { scanf("%d",&bl); arr[bl]=bl; } printf("Enter the name of the file\n"); scanf("%s",name); printf("Enter the starting block allocated to the file\n"); scanf("%d",&start); printf("Enter the length of the file\n"); scanf("%d",&le); printf("%s File Allocation Details\n",name); for(i=start,k=0;i<100 && k<le;i++) { if(arr[i]==0){ printf("%d - Allocated to %s\n",i,name); k++; } else{ printf("%d - Already Allocated\n",i); } } }
0 Comments