// Program 6 - Bankers algorithm
import java.util.Scanner;
public class Bankers{
private int need[][],allocate[][],max[][],avail[][],np,nr;
private void input(){
Scanner sc=new Scanner(System.in);
System.out.print("Enter no. of processes and resources : ");
np=sc.nextInt(); //no. of process
nr=sc.nextInt(); //no. of resources
need=new int[np][nr]; //initializing arrays
max=new int[np][nr];
allocate=new int[np][nr];
avail=new int[1][nr];
System.out.println("Enter allocation matrix -->");
for(int i=0;i<np;i++)
for(int j=0;j<nr;j++)
allocate[i][j]=sc.nextInt(); //allocation matrix
System.out.println("Enter max matrix -->");
for(int i=0;i<np;i++)
for(int j=0;j<nr;j++)
max[i][j]=sc.nextInt(); //max matrix
System.out.println("Enter available matrix -->");
for(int j=0;j<nr;j++)
avail[0][j]=sc.nextInt(); //available matrix
sc.close();
}
private int[][] calc_need(){
for(int i=0;i<np;i++)
for(int j=0;j<nr;j++) //calculating need matrix
need[i][j]=max[i][j]-allocate[i][j];
return need;
private boolean check(int i){
//checking if all resources for ith process can be allocated
for(int j=0;j<nr;j++)
if(avail[0][j]<need[i][j])
return false;
return true;
public void isSafe(){
input();
calc_need();
boolean done[]=new boolean[np];
int j=0;
while(j<np){ //until all process allocated
boolean allocated=false;
for(int i=0;i<np;i++)
if(!done[i] && check(i)){ //trying to allocate
for(int k=0;k<nr;k++)
avail[0][k]=avail[0][k]-need[i][k]+max[i][k];
System.out.println("Allocated process : "+i);
allocated=done[i]=true;
j++;
if(!allocated) break; //if no allocation
if(j==np) //if all processes are allocated
System.out.println("\nSafely allocated");
else
System.out.println("All proceess cant be allocated safely");
public static void main(String[] args) {
new Bankers().isSafe();
// safety Algorithm - Program 7
public class Safe_state {
public static void main(String[] args) {
int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
int[][] alloc = { { 0, 1, 0 }, // P0 // Allocation Matrix
{ 2, 0, 0 }, // P1
{ 3, 0, 2 }, // P2
{ 2, 1, 1 }, // P3
{ 0, 0, 2 } };
int[][] max = { { 7, 5, 3 }, // P0 // MAX Matrix
{ 3, 2, 2 }, // P1
{ 9, 0, 2 }, // P2
{ 2, 2, 2 }, // P3
{ 4, 3, 3 } };
int[] avail = { 3, 3, 2 }; // Available Resources
int[] f = new int[n];
int[] ans = new int[n];
int ind = 0;
for (k = 0; k < n; k++) {
f[k] = 0;
int[][] need = new int[n][m];
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
int y = 0;
for (k = 0; k < 5; k++) {
for (i = 0; i < n; i++) {
if (f[i] == 0) {
int flag = 0;
for (j = 0; j < m; j++) {
if (need[i][j] > avail[j]){
flag = 1;
break;
if (flag == 0) {
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
int flag = 1;
// To check if sequence is safe or not
for(i = 0;i<n;i++)
if(f[i]==0)
flag = 0;
System.out.println("The given sequence is not safe");
break;
if(flag==1)
System.out.println("Following is the SAFE Sequence");
for (i = 0; i < n - 1; i++)
System.out.print(" P" + ans[i] + " ->");
System.out.print(" P" + ans[n - 1]);
8. Write a program to simulate page replacement algorithms
a) FIFO b) LRU c) LFU
import java.io.*;
class Page_Rep
public static void main(String args[])throws IOException
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
int f,page=0,ch,pgf=0,n,chn=0;
boolean flag;
int pages[]; //pgf-page fault
do{
System.out.println("Menu");
System.out.println("1.FIFO");
System.out.println("2.LRU");
System.out.println("3.LFU");
System.out.println("4.EXIT");
System.out.println("ENTER YOUR CHOICE: ");
ch=Integer.parseInt(obj.readLine());
switch(ch)
case 1:
int pt=0;
System.out.println("enter no. of frames: ");
f=Integer.parseInt(obj.readLine());
int frame[]=new int[f];
for(int i=0;i<f;i++)
frame[i]=-1;
}
System.out.println("enter the no of pages ");
n=Integer.parseInt(obj.readLine());
pages=new int[n];
System.out.println("enter the page no ");
for(int j=0;j<n;j++)
pages[j]=Integer.parseInt(obj.readLine());
do{
int pg=0;
for(pg=0;pg<n;pg++)
page=pages[pg];
flag=true;
for(int j=0;j<f;j++)
if(page==frame[j])
flag=false;
break;
if(flag)
frame[pt]=page;
pt++;
if(pt==f)
pt=0;
System.out.print("frame :");
for(int j=0;j<f;j++)
System.out.print(frame[j]+" ");
System.out.println();
pgf++;
}
else
System.out.print("frame :");
for(int j=0;j<f;j++)
System.out.print(frame[j]+" ");
System.out.println();
chn++;
}while(chn!=n);
System.out.println("Page fault:"+pgf);
break;
case 2:
int k=0;
System.out.println("enter no. of frames: ");
f=Integer.parseInt(obj.readLine());
int frame1[]=new int[f];
int a[]=new int[f];
int b[]=new int[f];
for(int i=0;i<f;i++)
frame1[i]=-1;
a[i]=-1;
b[i]=-1;
System.out.println("enter the no of pages ");
n=Integer.parseInt(obj.readLine());
pages=new int[n];
System.out.println("enter the page no ");
for(int j=0;j<n;j++)
pages[j]=Integer.parseInt(obj.readLine());
do{
int pg=0;
for(pg=0;pg<n;pg++)
page=pages[pg];
flag=true;
for(int j=0;j<f;j++)
if(page==frame1[j])
{flag=false; break;}
for(int j=0;j<f && flag;j++)
if(frame1[j]==a[f-1])
{k=j;
break;}
if(flag)
frame1[k]=page;
System.out.println("frame :" );
for(int j=0;j<f;j++)
System.out.print(frame1[j]+" ");
pgf++;
System.out.println();
else
System.out.println("frame :" );
for(int j=0;j<f;j++)
System.out.print(frame1[j]+" ");
System.out.println();
int p=1;
b[0]=page;
for(int j=0;j<a.length;j++)
if(page!=a[j] && p<f)
b[p]=a[j];
p++;
for(int j=0;j<f;j++)
a[j]=b[j];
chn++;
}while(chn!=n);
System.out.println("Page fault:"+pgf);
break;
case 3:
k=0;
pgf=0;
int sml;
System.out.println("enter no. of frames: ");
f=Integer.parseInt(obj.readLine());
int frame2[]=new int[f];
int cnt[]=new int [f];
flag=true;
for(int i=0;i<f;i++)
frame2[i]=-1;
cnt[i]=0;
System.out.println("enter the no of pages ");
n=Integer.parseInt(obj.readLine());
pages=new int[n];
System.out.println("enter the page no ");
for(int j=0;j<n;j++)
pages[j]=Integer.parseInt(obj.readLine());
do
int pg=0;
for(pg=0;pg<n;pg++)
page=pages[pg];
flag=true;
for(int j=0;j<f;j++)
if(page==frame2[j])
flag=false;
cnt[j]++;
break;
if(flag)
{
sml=cnt[0];
for(int j=0;j<f;j++)
if(cnt[j]<sml)
sml=cnt[j];
break;
for(int j=0;j<f;j++)
if(sml==cnt[j] )
frame2[j]=page;
k=j;
break;
cnt[k]=1;
System.out.print("frame :");
for(int j=0;j<f;j++)
System.out.print(frame2[j]+" ");
System.out.println();
pgf++;
else
System.out.print("frame :");
for(int j=0;j<f;j++)
System.out.print(frame2[j]+" ");
System.out.println();
chn++;
}while(chn!=n);
System.out.println("Page fault:"+pgf);
break;
case 4:
break;
} while(ch!=4);
9. Write a program to simulate paging technique of memory management.
import java.util.Scanner;
public class Paging_Tech {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int ms, ps, nop, np, rempages, i, j, x, y, pa, offset;
int[] s = new int[10];
int[][] fno = new int[10][20];
System.out.print("\nEnter the memory size -- ");
ms = scanner.nextInt();
System.out.print("\nEnter the page size -- ");
ps = scanner.nextInt();
nop = ms / ps;
System.out.println("\nThe no. of pages available in memory are -- " + nop);
System.out.print("\nEnter number of processes -- ");
np = scanner.nextInt();
rempages = nop;
for (i = 1; i <= np; i++) {
System.out.printf("\nEnter no. of pages required for p[%d]-- ", i);
s[i] = scanner.nextInt();
if (s[i] > rempages) {
System.out.println("\nMemory is Full");
break;
rempages = rempages - s[i];
System.out.printf("\nEnter pagetable for p[%d] -- ", i);
for (j = 0; j < s[i]; j++)
fno[i][j] = scanner.nextInt();
System.out.println("\nEnter Logical Address to find Physical Address ");
System.out.println("\nEnter process no. and pagenumber and offset ");
x = scanner.nextInt();
y = scanner.nextInt();
offset = scanner.nextInt();
if (x > np || y >= s[x] || offset >= ps)
System.out.println("\nInvalid Process or Page Number or offset");
else {
pa = fno[x][y] * ps + offset;
System.out.println("\nThe Physical Address is -- " + pa);
scanner.close();
10 a) FCFS - Disk scheduling algorithm */
import java.util.Scanner;
public class FCFS_Disk{
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
int[] t = new int[20];
int n, tot = 0;
int[] tohm = new int[20];
float avhm;
System.out.println("enter the no.of tracks");
n = scanner.nextInt();
System.out.println("enter the tracks to be traversed");
for (int i = 2; i < n + 2; i++) {
t[i] = scanner.nextInt();
for (int i = 1; i < n + 1; i++) {
tohm[i] = t[i + 1] - t[i];
if (tohm[i] < 0) {
tohm[i] = tohm[i] * (-1);
for (int i = 1; i < n + 1; i++) {
tot += tohm[i];
avhm = (float) tot / n;
System.out.println("Tracks traversed\tDifference between tracks");
for (int i = 1; i < n + 1; i++) {
System.out.printf("%d\t\t\t%d\n", t[i], tohm[i]);
System.out.printf("\nAverage header movements:%f", avhm);
}
}
10 b)Scan algorithm
public class SCAN {
public static int callSCAN(int arr[],int init,int maxreq){
int sum=0,len=arr.length;
Arrays.sort(arr);
int pos= Arrays.binarySearch(arr, init);
pos=-pos-1;
// System.out.println(pos);
int left=pos-1,right=pos;
sum+=Math.abs(init-arr[right]);
System.out.println(arr[right]);
while(right<len-1){
sum+=Math.abs(arr[right]-arr[right+1]);
System.out.println(arr[right+1]);
right++;
sum+=Math.abs(arr[len-1]-maxreq);
System.out.println(maxreq);
sum+=Math.abs(maxreq-arr[left]);
System.out.println(arr[left]);
while(left!=0){
sum+=Math.abs(arr[left]-arr[left-1]);
System.out.println( arr[left-1]);
left--;
return sum;
}
public static void main(String[] args) {
//maxreq maximum possible disk request
int maxreq = 200;
//request array
int arr[]={98,183,37,122,14,124,65,67};
// disk head starts at init
int init=53;
int res=callSCAN(arr,init,maxreq);
System.out.println("Total Head Movement= "+res);
/* 10.c. C-SCAN DISK SCHEDULING ALGORITHM*/
import java.util.Scanner;
import java.util.Arrays;
public class CScan_Disk {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] queue = new int[20];
int n, head, max, seek = 0, diff;
float avg;
System.out.println("Enter the max range of disk");
max = scanner.nextInt();
System.out.println("Enter the initial head position");
head = scanner.nextInt();
System.out.println("Enter the size of queue request");
n = scanner.nextInt();
System.out.println("Enter the queue of disk positions to be read");
int[] queue1 = new int[20];
int[] queue2 = new int[20];
int temp1 = 0, temp2 = 0;
for (int i = 1; i <= n; i++) {
int temp = scanner.nextInt();
if (temp >= head) {
queue1[temp1] = temp;
temp1++;
} else {
queue2[temp2] = temp;
temp2++;
Arrays.sort(queue1, 0, temp1);
Arrays.sort(queue2, 0, temp2);
int i = 1, j = 0;
for (j = 0; j < temp1; i++, j++) {
queue[i] = queue1[j];
queue[i] = max;
queue[i + 1] = 0;
for (i = temp1 + 3, j = 0; j < temp2; i++, j++) {
queue[i] = queue2[j];
queue[0] = head;
for (j = 0; j <= n + 1; j++) {
diff = Math.abs(queue[j + 1] - queue[j]);
seek += diff;
System.out.println("Disk head moves from " + queue[j] + " to " + queue[j + 1] + " with seek " +
diff);
System.out.println("Total seek time is " + seek);
avg = (float) seek / n;
System.out.println("Average seek time is " + avg);
scanner.close();