Skip to content

Commit 92d95ab

Browse files
authored
MVT first,best,worst fit added
1 parent dcf2bab commit 92d95ab

File tree

3 files changed

+478
-0
lines changed

3 files changed

+478
-0
lines changed

LAB7/MVT/mvt_best_fit.cpp

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <stdbool.h>
5+
6+
#define N 1000
7+
typedef struct
8+
{
9+
int pid;
10+
int size;
11+
bool allocated;
12+
}process;
13+
14+
typedef struct
15+
{
16+
int pid; //-1 if free
17+
int size;
18+
}slots;
19+
20+
process p[N];
21+
slots s[N];
22+
int mm_size, n, tot;
23+
24+
int external_fragment()
25+
{
26+
int ret = 0;
27+
for(int i=0; i<tot; i++)
28+
if(s[i].pid == -1)
29+
ret += s[i].size;
30+
31+
return ret;
32+
}
33+
34+
void insert(int pos, int pid)
35+
{
36+
for(int i=tot; i>pos; i--)
37+
s[i] = s[i-1];
38+
39+
s[pos].pid = pid;
40+
s[pos].size = p[pid].size;
41+
p[pid].allocated = true;
42+
43+
s[pos+1].size -= p[pid].size;
44+
tot++;
45+
}
46+
47+
int get_slot(int pid)
48+
{
49+
int pos=-1, mini = 1e9;
50+
for(int i=0; i<tot; i++)
51+
if(s[i].pid == -1 && s[i].size >= p[pid].size && mini > s[i].size)
52+
{
53+
pos = i;
54+
mini = s[i].size;
55+
}
56+
57+
return pos;
58+
}
59+
60+
int free_slot(int pos)
61+
{
62+
s[pos].pid=-1;
63+
64+
if(pos!=tot-1 && s[pos+1].pid==-1)
65+
{
66+
s[pos].size += s[pos+1].size;
67+
for(int i=pos+1; i<tot-1; i++)
68+
s[i] = s[i+1];
69+
70+
tot--;
71+
}
72+
73+
if(pos>0 && s[pos-1].pid==-1)
74+
{
75+
s[pos-1].size += s[pos].size;
76+
for(int i=pos; i<tot-1; i++)
77+
s[i] = s[i+1];
78+
79+
tot--;
80+
}
81+
}
82+
83+
void print_memory_table()
84+
{
85+
printf("\n\n====== Memory Table ==========\n");
86+
printf("Size\t|\tProcess\n");
87+
printf("============================== \n");
88+
for(int i=0; i<tot; i++)
89+
{
90+
printf("%d\t|\t", s[i].size);
91+
if(s[i].pid == -1)
92+
printf("None\n");
93+
else
94+
printf("%d\n", s[i].pid+1);
95+
}
96+
97+
printf("============================== \n");
98+
}
99+
100+
int main()
101+
{
102+
//system("clear");
103+
int mm_size, n=0, ch;
104+
105+
printf("Enter the total size of memory : ");
106+
scanf("%d", &mm_size);
107+
108+
tot=1;
109+
s[0].pid = -1;
110+
s[0].size = mm_size;
111+
112+
while(1)
113+
{
114+
printf("enter your choice\n");
115+
116+
printf("1. Add new processs\n2.Remove existing process\n3.Exit\n");
117+
118+
scanf("%d", &ch);
119+
120+
if(ch == 1)
121+
{
122+
printf("\nEnter new process size : ");
123+
scanf("%d", &p[n].size);
124+
p[n].pid = n;
125+
p[n].allocated = false;
126+
127+
int pos = get_slot(n);
128+
if(pos == -1)
129+
printf("\nNot enough contiguous memory to allocate memory to process\n");
130+
else if(s[pos].size == p[n].size)
131+
{
132+
p[n].allocated = true;
133+
s[pos].pid = n;
134+
}
135+
else
136+
insert(pos, n);
137+
138+
print_memory_table();
139+
n++;
140+
}
141+
else if(ch == 2)
142+
{
143+
int prem;
144+
printf("\nWhich process would you like to remove? ");
145+
scanf("%d", &prem);
146+
prem--;
147+
148+
for(int i=0; i<tot; i++)
149+
if(s[i].pid == prem)
150+
free_slot(i);
151+
152+
print_memory_table();
153+
}
154+
else if(ch==3)
155+
{
156+
exit(1);
157+
}
158+
159+
}
160+
}

LAB7/MVT/mvt_first_fit.cpp

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <unistd.h>
5+
#include <stdbool.h>
6+
7+
#define N 1000
8+
typedef struct
9+
{
10+
int pid;
11+
int size;
12+
bool allocated;
13+
}process;
14+
15+
typedef struct
16+
{
17+
int pid; //-1 if free
18+
int size;
19+
}slots;
20+
21+
process p[N];
22+
slots s[N];
23+
int mm_size, n, tot;
24+
25+
int external_fragment()
26+
{
27+
int ret = 0;
28+
for(int i=0; i<tot; i++)
29+
if(s[i].pid == -1)
30+
ret += s[i].size;
31+
32+
return ret;
33+
}
34+
35+
void insert(int pos, int pid)
36+
{
37+
for(int i=tot; i>pos; i--)
38+
s[i] = s[i-1];
39+
40+
s[pos].pid = pid;
41+
s[pos].size = p[pid].size;
42+
p[pid].allocated = true;
43+
44+
s[pos+1].size -= p[pid].size;
45+
tot++;
46+
}
47+
48+
int get_slot(int pid)
49+
{
50+
for(int i=0; i<tot; i++)
51+
if(s[i].pid == -1 && s[i].size >= p[pid].size)
52+
return i;
53+
54+
return -1;
55+
}
56+
57+
int free_slot(int pos)
58+
{
59+
s[pos].pid=-1;
60+
61+
if(pos!=tot-1 && s[pos+1].pid==-1)
62+
{
63+
s[pos].size += s[pos+1].size;
64+
for(int i=pos+1; i<tot-1; i++)
65+
s[i] = s[i+1];
66+
67+
tot--;
68+
}
69+
70+
if(pos>0 && s[pos-1].pid==-1)
71+
{
72+
s[pos-1].size += s[pos].size;
73+
for(int i=pos; i<tot-1; i++)
74+
s[i] = s[i+1];
75+
76+
tot--;
77+
}
78+
}
79+
80+
void print_memory_table()
81+
{
82+
printf("\n\n====== Memory Table ==========\n");
83+
printf("Size\t|\tProcess\n");
84+
printf("============================== \n");
85+
for(int i=0; i<tot; i++)
86+
{
87+
printf("%d\t|\t", s[i].size);
88+
if(s[i].pid == -1)
89+
printf("None\n");
90+
else
91+
printf("%d\n", s[i].pid+1);
92+
}
93+
94+
printf("============================== \n");
95+
}
96+
97+
int main()
98+
{
99+
//system("clear");
100+
int mm_size, n=0, ch;
101+
102+
printf("Enter the total size of memory :\n");
103+
scanf("%d", &mm_size);
104+
105+
tot=1;
106+
s[0].pid = -1;
107+
s[0].size = mm_size;
108+
109+
while(1)
110+
{
111+
printf("enter your choice\n");
112+
113+
printf("1. Add new processs\n2.Remove existing process\n3.Exit\n");
114+
115+
scanf("%d", &ch);
116+
117+
if(ch == 1)
118+
{
119+
printf("\nEnter new process size : ");
120+
scanf("%d", &p[n].size);
121+
p[n].pid = n;
122+
p[n].allocated = false;
123+
124+
int pos = get_slot(n);
125+
if(pos == -1)
126+
printf("\nNot enough contiguous memory to allocate memory to process\n");
127+
else if(s[pos].size == p[n].size)
128+
{
129+
p[n].allocated = true;
130+
s[pos].pid = n;
131+
}
132+
else
133+
insert(pos, n);
134+
135+
print_memory_table();
136+
n++;
137+
}
138+
else if(ch == 2)
139+
{
140+
int prem;
141+
printf("\nWhich process would you like to remove? ");
142+
scanf("%d", &prem);
143+
prem--;
144+
145+
for(int i=0; i<tot; i++)
146+
if(s[i].pid == prem)
147+
free_slot(i);
148+
149+
print_memory_table();
150+
}
151+
else if(ch==3)
152+
{
153+
exit(1);
154+
}
155+
156+
}
157+
return 0;
158+
}

0 commit comments

Comments
 (0)