-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathpartition_allocation.c
More file actions
87 lines (84 loc) · 2.65 KB
/
partition_allocation.c
File metadata and controls
87 lines (84 loc) · 2.65 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h>
int search(int size, int partitions[], int n_partitions, const char method)
{
int index = INT_MAX;
if(method == 'f') {
for(int i=0;i<n_partitions;i++) {
if(partitions[i]>=size) {
return i;
}
}
}
else if(method == 'n') {
for(int i=n_partitions-1;i>=0;i--) {
if(partitions[i]>=size) {
return i;
}
}
}
else if(method == 'b') {
int min_req = INT_MAX, min_ind = INT_MAX;
for(int i=0;i<n_partitions;i++) {
if(partitions[i]>=size && partitions[i]<min_req) {
min_req = partitions[i];
min_ind = i;
}
}
return min_ind;
}
else if(method == 'w') {
int max_req = INT_MIN, max_ind = INT_MAX;
for(int i=0;i<n_partitions;i++) {
if(partitions[i]>=size && partitions[i]>max_req) {
max_req = partitions[i];
max_ind = i;
}
}
return max_ind;
}
else {
printf("method %c not found.\n", method);
}
return index;
}
bool allocate_process(int processes[], int n_processes, int partitions[], int n_partitions, const char method)
{
int **allocated_processes;
allocated_processes = (int**)calloc(n_processes, sizeof(int*));
for(int process=0;process<n_processes;process++)
{
int size = processes[process];
int index = search(size, partitions, n_partitions, method);
if(index>n_partitions) {
printf("unable to allocate process %d.\n", process);
printf("size of process: %d.\n", size);
printf("available sizes of partitions: ");
for(int i=0;i<n_partitions;i++) {
printf("%d ", partitions[i]);
}
return false;
}
allocated_processes[process] = (int*)calloc(2, sizeof(int*));
allocated_processes[process][0] = size;
allocated_processes[process][1] = index;
partitions[index] -= size;
}
printf("Processes allocated successfully!\n");
printf("Process index\tSize \tAllocated partition\n");
for(int process=0;process<n_processes;process++) {
printf("%-13d\t%-8d\t%d\n",
process,
allocated_processes[process][0],
allocated_processes[process][1]);
}
return true;
}
int main()
{
int partitions[] = {100,200,300,400,500};
int process[] = {150,350,50,25,50,50};
allocate_process(process, 6, partitions, 5, 'l');
}