6/recent/ticker-posts

OS First Fit

Write a program to create free space in such a way that the nodes are always maintained in ascending order with respect to starting address. Free space is maintained as a linked list of nodes with each node having the starting byte address and the ending byte address of a free block.

 
Input format :
Input consists of starting and ending byte addresses until the user ends his/her input.
( Always starting address should be less than or equal to ending address )
 
Output format :
The output consists of the free space blocks available for holding the process in ascending order with respect to starting byte address.
[ Refer Sample Input and Output for further details ]

 
Sample Input and Output :
[All text in bold corresponds to the input and the rest corresponds to output]

 
Enter the starting and ending addresses of the block ( enter -1 -1 to exit )
12
36
Enter the starting and ending addresses of the block ( enter -1 -1 to exit )
2
9
Enter the starting and ending addresses of the block ( enter -1 -1 to exit )
42
58
Enter the starting and ending addresses of the block ( enter -1 -1 to exit )
37
38
Enter the starting and ending addresses of the block ( enter -1 -1 to exit )
92
97
Enter the starting and ending addresses of the block ( enter -1 -1 to exit )
66
72
Free space list :
Start End
12 36
37 38
42 58
66 72
92 97


Answer:

#include<stdio.h>
int main()
{
    int start[100],end[100],i,j,temp,count=0;
    for(i=0;i<100;i++){
        count++;
        printf("Enter the starting and ending addresses of the block ( enter -1 -1 to exit )\n");
        scanf("%d",&start[i]);
        scanf("%d",&end[i]);
        if(start[i]==-1 && end[i]==-1)
        {
            break;
        }
    }
    for(i=0;i<count-1;i++){
        for(j=0;j<count-i-1;j++)
        {
            if(start[j]>start[j+1]){
                temp=start[j];
                start[j]=start[j+1];
                start[j+1]=temp;
                
                temp=end[j];
                end[j]=end[j+1];
                end[j+1]=temp;
                
            }
        }
    }
    printf("Free space list:\n");
    printf("Start end\n");
    for(i=0;i<count;i++)
    {
        if(start[i]!=-1 && end[i]!=-1){
            printf("%d %d",start[i],end[i]);
        }
    }
}



Post a Comment

0 Comments