0 votes
101 views
in Operating System by (420 points)
edited by
Write a program to implement dynamic partitioning placement algorithms i.e Best Fit, First – Fit and Worst –Fit.

1 Answer

0 votes
by (98.9k points)
selected by
 
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 
 

 

 

 

 

Related questions

0 votes
1 answer 113 views
0 votes
1 answer 107 views
0 votes
1 answer 113 views

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

561 users

...