-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfit.c
135 lines (131 loc) · 4.63 KB
/
fit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
int main()
{
int memory[MAX], size[MAX], alloc[MAX], i, j, n, m, choice;
printf("Enter the number of memory blocks: ");
scanf("%d", &n);
printf("Enter the size of each block:\n");
for (i = 0; i < n; i++) {
scanf("%d", &memory[i]);
size[i] = memory[i];
}
printf("Enter the number of processes: ");
scanf("%d", &m);
do {
printf("\n1. First Fit\n");
printf("2. Best Fit\n");
printf("3. Worst Fit\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: // First Fit
for (i = 0; i < m; i++) {
printf("Enter the size of process %d: ", i+1);
scanf("%d", &size[i]);
for (j = 0; j < n; j++) {
if (size[i] <= memory[j]) {
memory[j] -= size[i];
alloc[i] = j+1;
printf("Process %d allocated to memory block %d\n", i+1, j+1);
break;
}
}
if (j == n) {
printf("Process %d cannot be allocated memory\n", i+1);
alloc[i] = 0;
}
}
printf("\nMemory blocks:\n");
for (i = 0; i < n; i++) {
printf("%d\t", i+1);
}
printf("\nAllocated processes:\n");
for (i = 0; i < m; i++) {
if (alloc[i] != 0) {
printf("%d\t", alloc[i]);
} else {
printf("NA\t");
}
}
break;
case 2: // Best Fit
for (i = 0; i < m; i++) {
printf("Enter the size of process %d: ", i+1);
scanf("%d", &size[i]);
int min_index = -1;
for (j = 0; j < n; j++) {
if (size[i] <= memory[j]) {
if (min_index == -1 || memory[j] < memory[min_index]) {
min_index = j;
}
}
}
if (min_index != -1) {
memory[min_index] -= size[i];
alloc[i] = min_index+1;
printf("Process %d allocated to memory block %d\n", i+1, min_index+1);
} else {
printf("Process %d cannot be allocated memory\n", i+1);
alloc[i] = 0;
}
}
printf("\nMemory blocks:\n");
for (i = 0; i < n; i++) {
printf("%d\t", i+1);
}
printf("\nAllocated processes:\n");
for (i = 0; i < m; i++) {
if (alloc[i] != 0) {
printf("%d\t", alloc[i]);
} else {
printf("NA\t");
}
}
break;
case 3:
for (i = 0; i < m; i++) {
printf("Enter the size of process %d: ", i+1);
scanf("%d", &size[i]);
int max_index = -1;
for (j = 0; j < n; j++) {
if (size[i] <= memory[j]) {
if (max_index == -1 || memory[j] > memory[max_index]) {
max_index = j;
}
}
}
if (max_index != -1) {
memory[max_index] -= size[i];
alloc[i] = max_index+1;
printf("Process %d allocated to memory block %d\n", i+1, max_index+1);
} else {
printf("Process %d cannot be allocated memory\n", i+1);
alloc[i] = 0;
}
}
printf("\nMemory blocks:\n");
for (i = 0; i < n; i++) {
printf("%d\t", i+1);
}
printf("\nAllocated processes:\n");
for (i = 0; i < m; i++) {
if (alloc[i] != 0) {
printf("%d\t", alloc[i]);
} else {
printf("NA\t");
}
}
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
break;
}
} while (choice != 4);
return 0;
}