#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 */