Sunteți pe pagina 1din 13

import java.util.

Scanner;
public class Buddy {
static int tree[]=new int[4080];
static int i;
int j,k;
public static void main(String[] args) {
int totsize,cho,req;
Scanner sc1=new Scanner(System.in);
System.out.println("buddy system requirements");
System.out.println("\n* enter the size of the memory: ");
totsize=sc1.nextInt();
while (true)
{
System.out.println("Buddy System");
System.out.println("\n 1) locate the process into the memory ");
System.out.println("\n 2) remove the process from memory");
System.out.println("\n 3) tree structure for memory allocation map");
System.out.println("\n 4) exit");
System.out.println("\n* Enter your choice: ");
cho=sc1.nextInt();
switch(cho)
{
case 1:
System.out.println("Memory Allocation");
System.out.println("\n* Enter the process size: ");
req = sc1.nextInt();
segmentalloc(totsize, req);
break;
case 2:
System.out.println("Memory Deallocation");
System.out.println("\n* Enter the process size: ");
req = sc1.nextInt();
makefree(req);
break;
case 3:
System.out.println("Memory Allocation Map");
printing(totsize,0);
break;
default: return;
}
}
}
static void segmentalloc(int totsize, int request){
int flevel= 0, size;
size = totsize;
if(request>totsize){
System.out.println("\n* Result: ");
System.out.println("The system don't have enough free memory");
System.out.println("Suggestion: Go for VIRTUAL MEMORY");
return;

}
while(true){
if(request<size && request > (size/2)) break;
else {
size/=2;
flevel ++;
}
}
for(i=power(2, flevel)-1;i<=(power(2, flevel+1)-2); i++)
if(tree[i]==0 && place(i)){
tree[i] = request; makedivided(i);
System.out.println("\n Result : Successfull Allocation");
break;
}
if(i== power(2, flevel+1)-1){
System.out.println("\n %c Result :");
System.out.println("* The System don't have enough free memory");
System.out.println("* Suggestion: Go for VIRTUAL Memory Mode");
}
}
static void makedivided(int node){
while (node!=0)
{
node=node%2==0?(node-1)/2:node/2;
tree[node]=1;
}
}
static boolean place (int node)
{
while (node!=0)
{
node=node%2==0?(node-1)/2:node/2;
if(tree[node]>1)
return false;
}
return true;
}
static void makefree(int request)
{
int node=0;
while (true)
{
if(tree[node]==request)
break;
else
node++;
}
tree[node]=0;

while(node!=0)
{
if(tree[node%2==0?node-1:node+1]==0 && tree[node]==0)
{
tree[node%2==0?(node-1)/2:node/2]=0;
node=node%2==0?(node-1)/2:node/2;
}else break;
}
}
static int power(int x,int y)
{
int z,ans;
if(y==0)
return 1;
ans=x;
for(z=1;z<y;z++)
ans*=x;
return ans;
}
static void printing(int totsize,int node)
{
int llimit,ulimit,tab;
boolean permission= false;
if(node==0)
permission=true;
else if(node%2==0)
permission=tree[(node-1)/2]==1?true:false;
else
permission=tree[node/2]==1?true:false;
if(permission)
{
llimit=ulimit=tab=0;
while(true)
{
if(node>=llimit && node<=ulimit)
break;
else
{
tab++;
llimit=ulimit+1;
ulimit=2*ulimit+2;
}
}
System.out.println((totsize/power(2,tab)));
if (tree[node]>1)
System.out.println("---> Allocated"+tree[node]);
else if(tree[node]==1)
System.out.println("--->divided");
else
System.out.println("---> free");
printing(totsize,2*node+1);

printing(totsize,2*node+2);
}
}
}

OUTPUT
buddy system requirements
* enter the size of the memory:
1048576
Buddy System
1) locate the process into the memory
2) remove the process from memory
3) tree structure for memory allocation map
4) exit
* Enter your choice:
1
Memory Allocation
* Enter the process size:
102400
Result : Successfull Allocation
Buddy System
1) locate the process into the memory
2) remove the process from memory
3) tree structure for memory allocation map
4) exit
* Enter your choice:
3
Memory Allocation Map
1048576
--->divided
524288
--->divided
262144
--->divided

131072
---> Allocated102400
131072
---> free
262144
---> free
524288
---> free
Buddy System
1) locate the process into the memory
2) remove the process from memory
3) tree structure for memory allocation map
4) exit
* Enter your choice:
1
Memory Allocation
* Enter the process size:
245760
Result : Successfull Allocation
Buddy System
1) locate the process into the memory
2) remove the process from memory
3) tree structure for memory allocation map
4) exit
* Enter your choice:
3
Memory Allocation Map
1048576
--->divided
524288
--->divided
262144
--->divided
131072
---> Allocated102400
131072
---> free
262144
---> Allocated245760
524288
---> free
Buddy System

1) locate the process into the memory


2) remove the process from memory
3) tree structure for memory allocation map
4) exit
* Enter your choice:
1
Memory Allocation
* Enter the process size:
64000
Result : Successfull Allocation
Buddy System
1) locate the process into the memory
2) remove the process from memory
3) tree structure for memory allocation map
4) exit
* Enter your choice:
3
Memory Allocation Map
1048576
--->divided
524288
--->divided
262144
--->divided
131072
---> Allocated102400
131072
--->divided
65536
---> Allocated64000
65536
---> free
262144
---> Allocated245760
524288
---> free
Buddy System
1) locate the process into the memory
2) remove the process from memory

3) tree structure for memory allocation map


4) exit
* Enter your choice:
1
Memory Allocation
* Enter the process size:
256000
Result : Successfull Allocation
Buddy System
1) locate the process into the memory
2) remove the process from memory
3) tree structure for memory allocation map
4) exit
* Enter your choice:
3
Memory Allocation Map
1048576
--->divided
524288
--->divided
262144
--->divided
131072
---> Allocated102400
131072
--->divided
65536
---> Allocated64000
65536
---> free
262144
---> Allocated245760
524288
--->divided
262144
---> Allocated256000
262144
---> free
Buddy System
1) locate the process into the memory
2) remove the process from memory
3) tree structure for memory allocation map

4) exit
* Enter your choice:
2
Memory Deallocation
* Enter the process size:
245760
Buddy System
1) locate the process into the memory
2) remove the process from memory
3) tree structure for memory allocation map
4) exit
* Enter your choice:
3
Memory Allocation Map
1048576
--->divided
524288
--->divided
262144
--->divided
131072
---> Allocated102400
131072
--->divided
65536
---> Allocated64000
65536
---> free
262144
---> free
524288
--->divided
262144
---> Allocated256000
262144
---> free
Buddy System
1) locate the process into the memory
2) remove the process from memory
3) tree structure for memory allocation map
4) exit

* Enter your choice:


2
Memory Deallocation
* Enter the process size:
102400
Buddy System
1) locate the process into the memory
2) remove the process from memory
3) tree structure for memory allocation map
4) exit
* Enter your choice:
3
Memory Allocation Map
1048576
--->divided
524288
--->divided
262144
--->divided
131072
---> free
131072
--->divided
65536
---> Allocated64000
65536
---> free
262144
---> free
524288
--->divided
262144
---> Allocated256000
262144
---> free
Buddy System
1) locate the process into the memory
2) remove the process from memory
3) tree structure for memory allocation map
4) exit
* Enter your choice:
1
Memory Allocation

* Enter the process size:


75000
Result : Successfull Allocation
Buddy System
1) locate the process into the memory
2) remove the process from memory
3) tree structure for memory allocation map
4) exit
* Enter your choice:
3
Memory Allocation Map
1048576
--->divided
524288
--->divided
262144
--->divided
131072
---> Allocated75000
131072
--->divided
65536
---> Allocated64000
65536
---> free
262144
---> free
524288
--->divided
262144
---> Allocated256000
262144
---> free
Buddy System
1) locate the process into the memory
2) remove the process from memory
3) tree structure for memory allocation map
4) exit
* Enter your choice:
2
Memory Deallocation

* Enter the process size:


64000
Buddy System
1) locate the process into the memory
2) remove the process from memory
3) tree structure for memory allocation map
4) exit
* Enter your choice:
3
Memory Allocation Map
1048576
--->divided
524288
--->divided
262144
--->divided
131072
---> Allocated75000
131072
---> free
262144
---> free
524288
--->divided
262144
---> Allocated256000
262144
---> free
Buddy System
1) locate the process into the memory
2) remove the process from memory
3) tree structure for memory allocation map
4) exit
* Enter your choice:
2
Memory Deallocation
* Enter the process size:
75000
Buddy System
1) locate the process into the memory
2) remove the process from memory

3) tree structure for memory allocation map


4) exit
* Enter your choice:
3
Memory Allocation Map
1048576
--->divided
524288
---> free
524288
--->divided
262144
---> Allocated256000
262144
---> free
Buddy System
1) locate the process into the memory
2) remove the process from memory
3) tree structure for memory allocation map
4) exit
* Enter your choice:
2
Memory Deallocation
* Enter the process size:
256000
Buddy System
1) locate the process into the memory
2) remove the process from memory
3) tree structure for memory allocation map
4) exit
* Enter your choice:
3
Memory Allocation Map
1048576
---> free
Buddy System
1) locate the process into the memory
2) remove the process from memory

3) tree structure for memory allocation map


4) exit
* Enter your choice:
4

S-ar putea să vă placă și