18
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)

aos buddy

Embed Size (px)

DESCRIPTION

Lab program advanced operating system in Java

Citation preview

Page 1: aos buddy

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");

Page 2: aos buddy

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");

Page 3: aos buddy

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)

{

Page 4: aos buddy

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

Page 5: aos buddy

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;

}

Page 6: aos buddy

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)

Page 7: aos buddy

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);

}

}

}

OUTPUTbuddy system requirements

* enter the size of the memory: 1048576Buddy 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: 1Memory Allocation

* Enter the process size: 102400

Result : Successfull AllocationBuddy System

1) locate the process into the memory

Page 8: aos buddy

2) remove the process from memory

3) tree structure for memory allocation map

4) exit

* Enter your choice: 3Memory Allocation Map1048576--->divided524288--->divided262144--->divided131072---> Allocated102400131072---> free262144---> free524288---> freeBuddy 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: 1Memory Allocation

* Enter the process size: 245760

Result : Successfull AllocationBuddy 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

Page 9: aos buddy

Memory Allocation Map1048576--->divided524288--->divided262144--->divided131072---> Allocated102400131072---> free262144---> Allocated245760524288---> freeBuddy 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: 1Memory Allocation

* Enter the process size: 64000

Result : Successfull AllocationBuddy 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: 3Memory Allocation Map1048576--->divided524288--->divided262144--->divided131072---> Allocated102400

Page 10: aos buddy

131072--->divided65536---> Allocated6400065536---> free262144---> Allocated245760524288---> freeBuddy 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: 1Memory Allocation

* Enter the process size: 256000

Result : Successfull AllocationBuddy 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: 3Memory Allocation Map1048576--->divided524288--->divided262144--->divided131072---> Allocated102400131072--->divided65536---> Allocated6400065536

Page 11: aos buddy

---> free262144---> Allocated245760524288--->divided262144---> Allocated256000262144---> freeBuddy 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: 2Memory Deallocation

* Enter the process size: 245760Buddy 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: 3Memory Allocation Map1048576--->divided524288--->divided262144--->divided131072---> Allocated102400131072--->divided65536---> Allocated6400065536---> free262144---> free

Page 12: aos buddy

524288--->divided262144---> Allocated256000262144---> freeBuddy 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: 2Memory Deallocation

* Enter the process size: 102400Buddy 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: 3Memory Allocation Map1048576--->divided524288--->divided262144--->divided131072---> free131072--->divided65536---> Allocated6400065536---> free262144---> free524288--->divided262144

Page 13: aos buddy

---> Allocated256000262144---> freeBuddy 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: 1Memory Allocation

* Enter the process size: 75000

Result : Successfull AllocationBuddy 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: 3Memory Allocation Map1048576--->divided524288--->divided262144--->divided131072---> Allocated75000131072--->divided65536---> Allocated6400065536---> free262144---> free524288--->divided262144---> Allocated256000

Page 14: aos buddy

262144---> freeBuddy 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: 2Memory Deallocation

* Enter the process size: 64000Buddy 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: 3Memory Allocation Map1048576--->divided524288--->divided262144--->divided131072---> Allocated75000131072---> free262144---> free524288--->divided262144---> Allocated256000262144---> freeBuddy System

1) locate the process into the memory

2) remove the process from memory

Page 15: aos buddy

3) tree structure for memory allocation map

4) exit

* Enter your choice: 2Memory Deallocation

* Enter the process size: 75000Buddy 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: 3Memory Allocation Map1048576--->divided524288---> free524288--->divided262144---> Allocated256000262144---> freeBuddy 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: 2Memory Deallocation

* Enter the process size: 256000Buddy System

1) locate the process into the memory

Page 16: aos buddy

2) remove the process from memory

3) tree structure for memory allocation map

4) exit

* Enter your choice: 3Memory Allocation Map1048576---> freeBuddy 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