-
Notifications
You must be signed in to change notification settings - Fork 0
/
first_fit.c
94 lines (82 loc) · 2.77 KB
/
first_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
/* C implementation of First Fit algorithm
in memory management. The First Fit algorithm
is a memory allocation algorithm used by operating
systems. It is used to allocate memory blocks to
processes as they are requested. The process is
given access to the first block of memory that is
sufficiently large to accomodate the required
amount of memory.
The algorithm works as follows: many fixed-size
blocks make up the memory. The technique looks for
the first memory block that is big enough to hold
the required memory when a process requests it.
The block is then split into two pieces if it
exceeds the memory request. The first portion
is alloted to the process, while the second is
returned to the pool of memory that is readily
available. If no suitable block is found, the
process is put on a waiting list until a block
becomes available. When a process releases memory,
the algorithm merges adjacent free blocks to form
larger blocks. Time complexity is O(n*m) when n is
the number of processes and m is the numner of
memory blocks. The outer for loop runs for n
times and the inner for loop runs for m times.
Space complexity is O(n( where n is again the
number of processes. The allocation array is used
to store the block number allocated to the process,
which takes a space of O(n). */
#include<stdio.h>
// Function to allocate memory to
// blocks as per First fit algorithm
void firstFit(int blockSize[], int m, int processSize[], int n)
{
int i, j;
// Stores block id of the
// block allocated to a process
int allocation[n];
// Initially no block is assigned to any process
for(i = 0; i < n; i++)
{
allocation[i] = -1;
}
// pick each process and find suitable blocks
// according to its size ad assign to it
for (i = 0; i < n; i++) //here, n -> number of processes
{
for (j = 0; j < m; j++) //here, m -> number of blocks
{
if (blockSize[j] >= processSize[i])
{
// allocating block j to the ith process
allocation[i] = j;
// Reduce available memory in this block.
blockSize[j] -= processSize[i];
break; //go to the next process in the queue
}
}
}
printf("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < n; i++)
{
printf(" %i\t\t\t", i+1);
printf("%i\t\t\t\t", processSize[i]);
if (allocation[i] != -1)
printf("%i", allocation[i] + 1);
else
printf("Not Allocated");
printf("\n");
}
}
// Driver code
int main()
{
int m; //number of blocks in the memory
int n; //number of processes in the input queue
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
m = sizeof(blockSize) / sizeof(blockSize[0]);
n = sizeof(processSize) / sizeof(processSize[0]);
firstFit(blockSize, m, processSize, n);
return 0 ;
}