Skip to content
doubtly

2 answers

Best answer

#include<stdio.h>
 
int main()
{
    int i, j, n, m, bsize[100], psize[100], bstatus[100], pstatus[100], fit, flag;
 
    printf("Enter number of memory blocks : ");
    scanf("%d",&n);
 
    printf("Enter the size of each block : ");
    for(i=0;i<n;i++)
    {
        scanf("%d",&bsize[i]);
        bstatus[i] = 0;
    }
 
    printf("Enter number of processes : ");
    scanf("%d",&m);
 
    printf("Enter the size of each process : ");
    for(i=0;i<m;i++)
    {
        scanf("%d",&psize[i]);
        pstatus[i] = 0;
    }
 
    printf("Memory Allocation Algorithm: \n");
    printf("1. First Fit\n");
    printf("2. Best Fit\n");
    printf("3. Worst Fit\n");
    printf("Enter your choice : ");
    scanf("%d",&fit);
 
    switch(fit)
    {
        case 1: // First Fit
            for(i=0;i<m;i++)
            {
                flag = 0;
                for(j=0;j<n;j++)
                {
                    if(bstatus[j] == 0 && bsize[j] >= psize[i])
                    {
                        bstatus[j] = 1;
                        pstatus[i] = 1;
                        flag = 1;
                        break;
                    }
                }
                if(flag == 0)
                    printf("Process %d cannot be allocated memory\n", i+1);
            }
            break;
 
        case 2: // Best Fit
            for(i=0;i<m;i++)
            {
                int min = 9999, pos = -1;
                for(j=0;j<n;j++)
                {
                    if(bstatus[j] == 0 && bsize[j] >= psize[i] && bsize[j] < min)
                    {
                        min = bsize[j];
                        pos = j;
                    }
                }
                if(pos != -1)
                {
                    bstatus[pos] = 1;
                    pstatus[i] = 1;
                }
                else
                    printf("Process %d cannot be allocated memory\n", i+1);
            }
            break;
 
        case 3: // Worst Fit
            for(i=0;i<m;i++)
            {
                int max = -1, pos = -1;
                for(j=0;j<n;j++)
                {
                    if(bstatus[j] == 0 && bsize[j] >= psize[i] && bsize[j] > max)
                    {
                        max = bsize[j];
                        pos = j;
                    }
                }
                if(pos != -1)
                {
                    bstatus[pos] = 1;
                    pstatus[i] = 1;
                }
                else
                    printf("Process %d cannot be allocated memory\n", i+1);
            }
            break;
    }
 
    printf("Memory blocks status: ");
    for(i=0;i<n;i++)
        printf("%d ", bstatus[i]);
    printf("\n");
 
    printf("Process status: ");
    for(i=0;i<m;i++)
        printf("%d ", pstatus[i]);
    printf("\n");
 
    return 0;
}
Output :
Enter number of memory blocks : 5
Enter the size of each block : 50 200 70 115 80
Enter number of processes : 4
Enter the size of each process : 100 40 120 60
Memory Allocation Algorithm: 
1. First Fit
2. Best Fit
3. Worst Fit
Enter your choice : 2
Memory blocks status: 0 1 0 1 1 
Process status: 1 1 1 1 
 
Doubtly · 23 Apr 2023 Report
Was this answer helpful?

Answer: For each process, scan the free holes. First fit takes the first hole that is big enough, best fit takes the smallest hole that is big enough, and worst fit takes the largest hole. The leftover part of the hole stays free.

#include <stdio.h>
#define MAX 20

void place(int type, int nb, int bsize[], int np, int psize[]) {
    int hole[MAX], blk[MAX];
    for (int j = 0; j < nb; j++)
        hole[j] = bsize[j];           /* free space left in each block */
    for (int i = 0; i < np; i++) {
        int pos = -1;
        for (int j = 0; j < nb; j++) {
            if (hole[j] < psize[i])
                continue;             /* hole too small */
            if (pos == -1)
                pos = j;
            else if (type == 2 && hole[j] < hole[pos])
                pos = j;              /* best fit: smallest hole that fits */
            else if (type == 3 && hole[j] > hole[pos])
                pos = j;              /* worst fit: largest hole */
            if (type == 1)
                break;                /* first fit: first hole that fits */
        }
        blk[i] = pos;
        if (pos != -1)
            hole[pos] -= psize[i];    /* leftover stays as a smaller hole */
    }
    char *name[] = {"", "FIRST FIT", "BEST FIT", "WORST FIT"};
    printf("\n%s\nProcess\tSize\tBlock\n", name[type]);
    for (int i = 0; i < np; i++) {
        if (blk[i] == -1)
            printf("P%d\t%d\tNot allocated\n", i + 1, psize[i]);
        else
            printf("P%d\t%d\tB%d\n", i + 1, psize[i], blk[i] + 1);
    }
}

int main(void) {
    int nb, np, bsize[MAX], psize[MAX];
    printf("Number of memory blocks: ");
    scanf("%d", &nb);
    printf("Sizes of the blocks: ");
    for (int j = 0; j < nb; j++) scanf("%d", &bsize[j]);
    printf("Number of processes: ");
    scanf("%d", &np);
    printf("Sizes of the processes: ");
    for (int i = 0; i < np; i++) scanf("%d", &psize[i]);
    for (int type = 1; type <= 3; type++)
        place(type, nb, bsize, np, psize);
    return 0;
}

Sample run: blocks 100, 500, 200, 300, 600 KB; processes 212, 417, 112, 426 KB

FIRST FIT              BEST FIT               WORST FIT
P1 212 -> B2           P1 212 -> B4           P1 212 -> B5
P2 417 -> B5           P2 417 -> B2           P2 417 -> B2
P3 112 -> B2           P3 112 -> B3           P3 112 -> B5
P4 426 -> Not alloc.   P4 426 -> B5           P4 426 -> Not alloc.

In this example best fit places all four processes, while first fit and worst fit leave the 426 KB process waiting.

The earlier answer on this page does not show which block each process gets, and its sample output (block status 0 1 0 1 1) does not match its own input: best fit on that input fills the first four blocks (1 1 1 1 0).

Doubtly AI · 23 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.