Search
 
SCRIPT & CODE EXAMPLE
 

C

worst fit program in c

// Aim :- Implementation of the following Memory Allocation Methods for fixed partition
//              a) Worst-fit    b) Best-fit     c) First-fit

// PROGRAM :- c)FIRST-FIT

/*
    Algorithm:-
    ---------

    step-1: start.

    step-2: declare an constant MAX of value 25 globally.

    step-3: declare frag[MAX],b[MAX],f[MAX],i,j,nb,nf,temp of integer
            data type and bf[MAX],ff[MAX] of also static interger
            data type.

    step-4: Read number of blocks into nb and number of files into nf,
            then read size of each block into array b[] and read 
            the size of each file into array f[].
            
    step-5: for i = 1 to nf do,
                (a) for j = 1 to nb do,
                        if bf[i] is not equal to 1 then,
                            (1) assign temp = b[j]-f[i] and,
                            (2) if temp is greater than or equal to zero then,
                                (i) assign ff[i] = j and,
                                (ii) goto (b)
                (b) assign frag[i] = temp and,
                (c) assign bf[ff[i]]=1.
    
    step-6: Display File no, File size, Block no, Block size, Fragement
            values in form of a table.

    step-7: stop

*/

#include <stdio.h>
#define MAX 25

int main()
{
    int frag[MAX],b[MAX],f[MAX],i,j,nb,nf,temp;
    static int bf[MAX],ff[MAX];

    printf("
	Memory Management Scheme - First Fit");

    printf("
Enter the number of blocks:");
    scanf("%d",&nb);

    printf("Enter the number of files:");
    scanf("%d",&nf);

    printf("
Enter the size of the blocks:-
");
    for(i=1;i<=nb;i++)
    {
        printf("Block %d:",i);
        scanf("%d",&b[i]);
    }

    printf("Enter the size of the files :-
");
    for(i=1;i<=nf;i++)
    {
        printf("File %d:",i);
        scanf("%d",&f[i]);
    }

    for(i=1;i<=nf;i++)
    {
        for(j=1;j<=nb;j++)
        {
            if(bf[j]!=1)
            {
                temp=b[j]-f[i];

                if(temp>=0)
                {
                    ff[i]=j;
                    break;
                }
            }
        }

        frag[i]=temp;
        bf[ff[i]]=1;
        
    }

    printf("
File_no:	File_size :	Block_no:	Block_size:	Fragement");
    for(i=1;i<=nf;i++)
        printf("
%d		%d		%d		%d		%d",i,f[i],ff[i],b[ff[i]],frag[i]);
    
    getchar();
    return 0;
}

/*

    INPUT/OUTPUT:-
    ------------

    Enter the number of blocks: 3
    Enter the number of files: 2

    Enter the size of the blocks:-
    Block 1: 5
    Block 2: 2
    Block 3: 7

    Enter the size of the files:-
    File 1: 1
    File 2: 4

    File No          File Size        Block No             Block Size           Fragment
    1                              1             1                              5                             4
    2                              4             3                              7                             3

*/
Comment

PREVIOUS NEXT
Code Example
C :: binary operations on structs C 
C :: C Program to Maintain an Inventory of items in Online Store 
C :: first come first serve 
C :: how to limit tiktok on mikrotik 
C :: false and true in c 
C :: #pragma pack(1) in c 
C :: unigine 
C :: Integer Xor swap 
C :: 157.245.33.77: 
C :: c program boilerplate code 
C :: elastic search url date 
C :: c program for airthmetic operators 
C :: C linked sorted lists 
C :: allocate a array on strings in c 
C :: ::template 
C :: anticonstitutionnellement 
C :: lmkmaksmxkmakkxxamkskaamkamkaxsmkasm 
C :: C static libraries (creating archive from object files) 
C :: c logarithm check if number is base 
C :: write to file in c programming 
C :: how to check where the last char is in a string c 
C :: Recommended compiler and linker flags for GCC 
Dart :: python read json from url 
Dart :: flutter label alignment top 
Dart :: flutter textformfield decimal 
Dart :: flutter run code after build 
Dart :: textfield style flutter 
Dart :: hive regiter adapter enum 
Dart :: remove file extension from path dart 
Dart :: flutter image asset 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =