6/recent/ticker-posts

Linked File Allocation 2

Write a C program to allocate space for n files using Linked File Allocation.  If the starting block is occupied, then consider the next free block for storing the file. If the file size is greater than the free block then that file cannot be allocated.


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.
 

Input format: 

Input consists of a number of files (n) to be allocated disk space ( int ), name ( string ), Start of the block ( int ), and length ( int ) of the n files to be allocated disk space.

 

Output format:
The output consists of file allocation details using linked allocation technique after allocating the input files.
[ Refer Sample Input and Output for further details ]
 
Sample Input and Output :
[ All text of bold corresponds to input ]
Enter the number of files to be allocated
6
Enter the name of the file 1
F1
Enter the starting block allocated to the file 1
5
Enter the length of the file 1
15
Enter the name of the file 2
F2
Enter the starting block allocated to the file 2
2
Enter the length of the file 2
25
Enter the name of the file 3
F3
Enter the starting block allocated to the file 3
44
Enter the length of the file 3
10
Enter the name of the file 4
F4
Enter the starting block allocated to the file 4
60
Enter the length of the file 4
30
Enter the name of the file 5
F5
Enter the starting block allocated to the file 5
42
Enter the length of the file 5
32
Enter the name of the file 6
F6
Enter the starting block allocated to the file 6
42
Enter the length of the file 6
17
File Allocation Details
Disk space allocated to F1
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Disk space allocated to F2
2 3 4 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
Disk space allocated to F3
44 45 46 47 48 49 50 51 52 53
Disk space allocated to F4
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
F5 cannot be allocated disk space
Disk space allocated to F6
42 43 54 55 56 57 58 59 90 91 92 93 94 95 96 97 98


Answer:

#include<stdio.h>
int main()
{
    int n,arr[101],i,le,start,temp_le[100],temp_s[100],k,m,z,avail,x;
    char name[20][20];
    printf("Enter the number of files to be allocated\n");
    scanf("%d",&n);
    for(i=0;i<101;i++)
    {
        arr[i]=0;
    }
    for(i=0;i<n;i++){
        printf("Enter the name of the file %d\n",i+1);
        scanf("%s",name[i]);
        printf("Enter the starting block allocated to the file %d\n",i+1);
        scanf("%d",&start);
        temp_s[i]=start;
        printf("Enter the length of the file %d\n",i+1);
        scanf("%d",&le);
        temp_le[i]=le;
        
    }
    printf("File Allocation Details\n");
    for(i=0;i<n;i++)
    {
        avail=0;
        z=0;
        for(x=temp_s[i];x<100;x++){
            if(arr[x]==0){
                avail++;
            }
        }
        if(temp_le[i]<avail){
            for(m=temp_s[i],k=0; m<100 && k<temp_le[i];m++){
                if(arr[m]==0){
                    if(z==0){
                        printf("Disk space allocated to %s\n",name[i]);
                        z=1;
                    }
                    arr[m]=m;
                    printf("%d",m);
                    k++;
                }
            }
        }
        else
        {
            printf("%s cannot be allocated disk space",name[i]);
        }
        printf("\n");
    }
}


Post a Comment

0 Comments