0% found this document useful (0 votes)
2 views3 pages

OS 11 c

The document contains a C program implementing the Worst Fit memory allocation algorithm. It allocates processes to memory blocks based on the largest available block that can accommodate the process size. The program also displays the allocation results and remaining memory in each block after the allocation.

Uploaded by

jayaprakash9223
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

OS 11 c

The document contains a C program implementing the Worst Fit memory allocation algorithm. It allocates processes to memory blocks based on the largest available block that can accommodate the process size. The program also displays the allocation results and remaining memory in each block after the allocation.

Uploaded by

jayaprakash9223
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Sprno:9223

11C WORST FIT


PROGRAM:
#include <stdio.h>
#define MAX_BLOCKS 20
#define MAX_PROCESSES 20
void worstFit(int blockSize[], int blocks, int processSize[], int processes) {
int allocation[MAX_PROCESSES];
// Initialize all allocations to -1
for (int i = 0; i < processes; i++) {
allocation[i] = -1;
}
// Worst Fit Allocation
for (int i = 0; i < processes; i++) {
int worstIdx = -1;
for (int j = 0; j < blocks; j++) {
if (blockSize[j] >= processSize[i]) {
if (worstIdx == -1 || blockSize[j] > blockSize[worstIdx]) {
worstIdx = j;
}
}
}
// Allocate the process to the worst fitting block
if (worstIdx != -1) {
allocation[i] = worstIdx;
blockSize[worstIdx] -= processSize[i];
}
}
// Display allocation results
printf("\nProcess\tSize\tAllocated Block\n");
for (int i = 0; i < processes; i++) {
Sprno:9223

printf("P%d\t%d\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("Block %d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
// Show remaining memory
printf("\nRemaining Memory in Blocks:\n");
for (int i = 0; i < blocks; i++) {
printf("Block %d: %d\n", i + 1, blockSize[i]);
}
}
int main() {
int blockSize[MAX_BLOCKS], processSize[MAX_PROCESSES];
int blocks, processes;
printf("Enter number of memory blocks: ");
scanf("%d", &blocks);
printf("Enter sizes of %d blocks:\n", blocks);
for (int i = 0; i < blocks; i++) {
scanf("%d", &blockSize[i]);
}
printf("Enter number of processes: ");
scanf("%d", &processes);
printf("Enter sizes of %d processes:\n", processes);
for (int i = 0; i < processes; i++) {
scanf("%d", &processSize[i]);
}
worstFit(blockSize, blocks, processSize, processes);
return 0;
}
Sprno:9223

OUTPUT:
Enter number of memory blocks: 5
Enter sizes of 5 blocks:
100 500 200 300 600
Enter number of processes: 3
Enter sizes of 3 processes:
212 417 112
Process Size Allocated Block
P1 212 Block 5
P2 417 Block 2
P3 112 Block 5
Remaining Memory in Blocks:
Block 1: 100
Block 2: 83
Block 3: 200
Block 4: 300
Block 5: 276

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy