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

Program-6

Uploaded by

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

Program-6

Uploaded by

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

/*6.

Develop a C program to simulate the following contiguous memory allocation


Techniques:
A) Worst fit
B) Best fit
C) First fit. */

A) Worst fit

#include <stdio.h>

#define MAX_BLOCKS 100

struct MemoryBlock {
int block_id;
int size;
int allocated;
};

void worstFit(struct MemoryBlock blocks[], int m, int processSize) {


int worstIdx = -1;
for (int i = 0; i < m; i++) {
if (blocks[i].allocated == 0 && blocks[i].size >= processSize) {
if (worstIdx == -1 || blocks[i].size > blocks[worstIdx].size) {
worstIdx = i;
}
}
}
if (worstIdx != -1) {
blocks[worstIdx].allocated = 1;
printf("Process with size %d allocated to block %d\n", processSize,
worstIdx + 1);
} else {
printf("Cannot allocate the process with size %d\n", processSize);
}
}

int main() {
int m;
printf("Enter the number of memory blocks: ");
scanf("%d", &m);
struct MemoryBlock blocks[MAX_BLOCKS];
for (int i = 0; i < m; i++) {
blocks[i].block_id = i + 1;
printf("Enter the size of memory block %d: ", i + 1);
scanf("%d", &blocks[i].size);
blocks[i].allocated = 0;
}
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int processSize;
printf("Enter the size of process %d: ", i + 1);
scanf("%d", &processSize);
worstFit(blocks, m, processSize);
}
return 0;
}
B) Best fit

#include <stdio.h>

#define MAX_BLOCKS 100

struct MemoryBlock {
int block_id;
int size;
int allocated;
};

void bestFit(struct MemoryBlock blocks[], int m, int processSize) {


int bestIdx = -1;
for (int i = 0; i < m; i++) {
if (blocks[i].allocated == 0 && blocks[i].size >= processSize) {
if (bestIdx == -1 || blocks[i].size < blocks[bestIdx].size) {
bestIdx = i;
}
}
}
if (bestIdx != -1) {
blocks[bestIdx].allocated = 1;
printf("Process with size %d allocated to block %d\n", processSize, bestIdx
+ 1);
} else {
printf("Cannot allocate the process with size %d\n", processSize);
}
}

int main() {
int m;
printf("Enter the number of memory blocks: ");
scanf("%d", &m);
struct MemoryBlock blocks[MAX_BLOCKS];
for (int i = 0; i < m; i++) {
blocks[i].block_id = i + 1;
printf("Enter the size of memory block %d: ", i + 1);
scanf("%d", &blocks[i].size);
blocks[i].allocated = 0;
}
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int processSize;
printf("Enter the size of process %d: ", i + 1);
scanf("%d", &processSize);
bestFit(blocks, m, processSize);
}
return 0;
}

C) First fit.

#include <stdio.h>
#define MAX_BLOCKS 100

struct MemoryBlock {
int block_id;
int size;
int allocated;
};

void firstFit(struct MemoryBlock blocks[], int m, int processSize) {


for (int i = 0; i < m; i++) {
if (blocks[i].allocated == 0 && blocks[i].size >= processSize) {
blocks[i].allocated = 1;
printf("Process with size %d allocated to block %d\n", processSize, i +
1);
return;
}
}
printf("Cannot allocate the process with size %d\n", processSize);
}

int main() {
int m;
printf("Enter the number of memory blocks: ");
scanf("%d", &m);
struct MemoryBlock blocks[MAX_BLOCKS];
for (int i = 0; i < m; i++) {
blocks[i].block_id = i + 1;
printf("Enter the size of memory block %d: ", i + 1);
scanf("%d", &blocks[i].size);
blocks[i].allocated = 0;
}
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int processSize;
printf("Enter the size of process %d: ", i + 1);
scanf("%d", &processSize);
firstFit(blocks, m, processSize);
}
return 0;
}

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