#include <stdio.h>

void implimentWorstFit(int blockSize[], int blocks, int processSize[], int processes)
{
    // This will store the block id of the allocated block to a process
    int allocation[processes];
    int occupied[blocks];
    
    // initially assigning -1 to all allocation indexes
    // means nothing is allocated currently
    for(int i = 0; i < processes; i++){
        allocation[i] = -1;
    }
    
    for(int i = 0; i < blocks; i++){
        occupied[i] = 0;
    }
 
    // pick each process and find suitable blocks
    // according to its size ad assign to it
    for (int i=0; i < processes; i++)
    {
	int indexPlaced = -1;
	for(int j = 0; j < blocks; j++)
	{
	    // if not occupied and block size is large enough
	    if(blockSize[j] >= processSize[i] && !occupied[j])
            {
                // place it at the first block fit to accomodate process
                if (indexPlaced == -1)
                    indexPlaced = j;
                    
                // if any future block is larger than the current block where
                // process is placed, change the block and thus indexPlaced
                else if (blockSize[indexPlaced] < blockSize[j])
                    indexPlaced = j;
            }
        }
 
        // If we were successfully able to find block for the process
        if (indexPlaced != -1)
        {
            // allocate this block j to process p[i]
            allocation[i] = indexPlaced;
            
            // make the status of the block as occupied
            occupied[indexPlaced] = 1;
 
            // Reduce available memory for the block
            blockSize[indexPlaced] -= processSize[i];
        }
    }
 
    printf("\nProcess No.\tProcess Size\tBlock no.\n");
    for (int i = 0; i < processes; i++)
    {
        printf("%d \t\t\t %d \t\t\t", i+1, processSize[i]);
        if (allocation[i] != -1)
            printf("%d\n",allocation[i] + 1);
        else
            printf("Not Allocated\n");
    }
}
 
// Driver code
int main()
{
    int blockSize[] = {100, 50, 30, 120, 35};
    int processSize[] = {40, 10, 30, 60};
    int blocks = sizeof(blockSize)/sizeof(blockSize[0]);
    int processes = sizeof(processSize)/sizeof(processSize[0]);
 
    implimentWorstFit(blockSize, blocks, processSize, processes);
 
    return 0;
}// 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("\n\tMemory Management Scheme - First Fit");

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

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

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

    printf("Enter the size of the files :-\n");
    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("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
    for(i=1;i<=nf;i++)
        printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%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

*/

C相关代码片段

openGL create shape

script to check program running and restart

printf("")

Rewind to a commit Git

mouse click for colab

how to remove newline in the end of fgets string

fit text to conatiner flutter

gorm string array

js observe url change

clock skew detected makefile

time() function in c

worst fit code in c

first fit code in c

roem evaluation pyspark

calculator c

safet algorith im c

c copy char*

jfug function

soil moisture sensor interfacing with ESP32

flutter sidebar menu example

access images in c panel htaccess file

c include header file in the folder aboce

c include libray

gcc name output file

C data link escape char

How to type DLE in C

vscode Ctrl+d undo

C[strtol] func

pointer c++

power in c

sort three numbers

strip in c

c time duration

overleaf itemize a b c

route groups

itoa c code

queue in c

'wsgirequest' object has no attribute 'get'

c copy file

how to print array data in html using javascript

hello world program in c

how to compare a variable with a string in c

c print array

c rand range

exponents without pow c

how to reversed number in c

c random number with range

pthread mutex destroy while lock

pthread init mutex twice

insert c or p

woocommerce align add to cart buttons

vscode fold all code when open file

commit github non funziona

makefile both c and c++

scanf with space

c macro variable arguments

c printf format

check if prime c

read file line by line c

c use define in string

pattern program in c

Write the code in C to implement selection sort

c program of Goldbach conjecture

otput only one decimal point in c

trim string c

tokenize string c

firebase message in app doesn't work

implementing stack using linked list

esercizi semplici con le funzioni in c

array to linked list in c

sorted array to bst implementation in c

doubly linked list insertion in the middle

arch distro

distance vector routing algorithm

implement typedef with arrays

c exit vs _exit

explain in simple with c code

convert matrix to lower triangular matrix

Addition and subtraction of matrix

Transpose of a matrix