100% found this document useful (1 vote)
8K views

Implementation of Worst-Fit Algorithm Coding: #Include Main

The document describes the implementation of a worst-fit algorithm for memory allocation. It takes input on the number of memory partitions, their sizes, the number of processes, and their memory requirements. It then allocates each process to the partition with the largest remaining space using a worst-fit approach. The output shows the allocations and any unallocated processes.

Uploaded by

zeal4triumph
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
8K views

Implementation of Worst-Fit Algorithm Coding: #Include Main

The document describes the implementation of a worst-fit algorithm for memory allocation. It takes input on the number of memory partitions, their sizes, the number of processes, and their memory requirements. It then allocates each process to the partition with the largest remaining space using a worst-fit approach. The output shows the allocations and any unallocated processes.

Uploaded by

zeal4triumph
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

IMPLEMENTATION OF WORST-FIT ALGORITHM

CODING:
#include<stdio.h>
main()
{
int i,j,temp,f[10],fp[10];
int no,p[15],part[15],pno,pr[15],prmem[15];
//Input partition size
printf("\n*******************************************");
printf("\n IMPLEMENTATION OF WORST-FIT ALGORITHM");
printf("\n*******************************************");
printf("\n Enter the number of partitions");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
p[i]=i;
printf("Enter the memory for partition %d:\t",i);
scanf("%d",&part[i]);
}
//Arrange partitions in descending order
for(i=1;i<=no;i++)
{
for(j=1;j<=i;j++)
{
if(part[j]<part[i])
{
temp=part[i];
part[i]=part[j];
part[j]=temp;

temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
printf("\nFree memory");
for(i=1;i<=no;i++)
{
printf("\n partition %d: \t %d",p[i],part[i]);
}

//Input process details


printf("\n Enter the number of process");
scanf("%d",&pno);
for(i=1;i<=pno;i++)
{
pr[i]=i;
printf("Enter the size for process %d:\t",i);
scanf("%d",&prmem[i]);
}

//Applying Worst-Fit Method


printf("\n----------------------------------------\n");
printf("PROCESS|\t PARTITION| \t FREE_MEMORY|\n");
printf("\n----------------------------------------\n");
j=1;
for(i=1;i<=no;i++)
{
f[i]=0;fp[j]=0;
}
while(j<=pno)
{
for(i=1;i<=no;i++)
{
if((part[i]>=prmem[j]) && (f[i]==0))
{
part[i]=part[i]-prmem[j];
fp[j]=1;//process alloted
f[i]=1;//partition alloted
printf("%d \t %d \t %d \n",pr[j],p[i],part[i]);
goto l1;
}}
l1:
j++;
}
for(i=1;i<=no;i++)
{
if(f[i]==0)
{
printf(" \t %d \t %d \n",p[i],part[i]);
}}
printf("The following process is not allocatted:");
for(i=1;i<=pno;i++)
{
if(fp[i]==0)
{
printf(" %d ",pr[i]);
}
}
}
Output:-
*************************************
IMPLEMENTATION OF WORST FIT ALGORITHM
*************************************
Enter the number of partitions 3

Enter the memory size for partition 1 : 700

Enter the memory size for partition 2 : 600

Enter the memory size for partition 3 : 500

Free memory
Partition 1 : 700
Partition 2 : 600
Partition 3 : 500
Enter the number of process3

Enter the size for process 1 : 100

Enter the size for process 2 : 200

Enter the size for process 3 : 300

-----------------------------------------------------------------------

PROCESS | PARTITION | FREE_MEMEORY |

------------------------------------------------------------------------

1 1 600

2 2 400

3 3 200

The following process is not allocatted

You might also like