Posts

Java program for Safe Sequence (Banker's Algorithm).

//Java program for safe sequence of Banker's algorithm. import java.util.*; class SafeAlgo { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter number of processes:"); int n=sc.nextInt(); int p[]=new int[n]; int flag[]=new int[n]; System.out.println("Enter number of resource variables"); int m=sc.nextInt(); int max[][]=new int[n][m]; int allo[][]=new int[n][m]; int need[][]=new int[n][m]; int avail[]=new int[m]; for(int x=0;x<n;x++) { System.out.println("Enter process number:"); p[x]=sc.nextInt(); flag[x]=0; for(int y=0;y<m;y++) { System.out.println("Max -> P"+p[x]+" ->"+y+" :"); max[x][y]=sc.nextInt(); } for(int y=0;y<m;y++) { System.out.println("Allocation -> P"+p[x]+" ->"+y+" :"); allo[x][y]=sc.nextInt(); need[x][y]=max[x][y]-allo[x]...

SJF Scheduling Algorithm Java Implementation.

//Java program for Shortest Job First Scheduling algorithm. import java.io.*; public class SJF{ public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n; System.out.println("Please enter the number of Processes: "); n = Integer.parseInt(br.readLine()); int proc[][] = new int[n + 1][4]; for(int i = 1; i <= n; i++) { System.out.println("Please enter the Arrival Time for Process " + i + ": "); proc[i][0] = Integer.parseInt(br.readLine()); System.out.println("Please enter the Burst Time for Process " + i + ": "); proc[i][1] = Integer.parseInt(br.readLine()); } System.out.println(); int total_time = 0; for(int i = 1; i <= n; i++) { total_time += proc[i][1]; } int time_chart[] = new int[total_time]; for(int i = 0; i ...

Round Robin Scheduling Algorithm Java Implementation.

//Java Program for Round Robin scheduling algorithm. import java.io.*; class rr { public static void main(String args[])throws IOException { DataInputStream in=new DataInputStream(System.in); int i,j,k,q,sum=0; System.out.println("Enter number of process:"); int n=Integer.parseInt(in.readLine()); int bt[]=new int[n]; int wt[]=new int[n]; int tat[]=new int[n]; int a[]=new int[n]; System.out.println("Enter brust Time:"); for(i=0;i<n;i++) { System.out.println("Enter brust Time for "+(i+1)); bt[i]=Integer.parseInt(in.readLine()); } System.out.println("Enter Time quantum:"); q=Integer.parseInt(in.readLine()); for(i=0;i<n;i++) a[i]=bt[i]; for(i=0;i<n;i++) wt[i]=0; do { for(i=0;i<n;i++) { if(bt[i]>q) { bt[i]-=q; for(j=0;j<n;j++) { if((j!=i)&&(bt[j]!=0)) wt[j]+=q; } } else { for(j=0;j<n;j++) { if((j!=i)&&(bt[j]!=0)) wt[j]+=bt[i]; } bt[i]=0; } } sum=0; for(k=0;k<n;k++) sum=sum+bt[k]; } while(sum!=0); for(i=0;i<...

Priority Scheduling Algorithm Java Program.

import java.util.Scanner; public class priority{ public static void main(String args[]) { Scanner s = new Scanner(System.in); int x,n,p[],pp[],bt[],w[],t[],awt,atat,i; p = new int[10]; pp = new int[10]; bt = new int[10]; w = new int[10]; t = new int[10]; //n is number of process //p is process //pp is process priority //bt is process burst time //w is wait time // t is turnaround time //awt is average waiting time //atat is average turnaround time System.out.print("Enter the number of process : "); n = s.nextInt(); System.out.print("\n\t Enter burst time : time priorities \n"); for(i=0;i<n;i++) { System.out.print("\nProcess["+(i+1)+"]:"); bt[i] = s.nextInt(); pp[i] = s.nextInt(); p[i]=i+1; } //sorting on the basis of priority for(i=0;i<n-1;i++) { for...

Optimal Page Replacement algorithm in Java.

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OptimalReplacement { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int frames, pointer = 0, hit = 0, fault = 0,ref_len; boolean isFull = false; int buffer[]; int reference[]; int mem_layout[][]; System.out.println("Enter the number of Frames: "); frames = Integer.parseInt(br.readLine()); System.out.println("Enter the length of the Reference string: "); ref_len = Integer.parseInt(br.readLine()); reference = new int[ref_len]; mem_layout = new int[ref_len][frames]; buffer = new int[frames]; for(int j = 0; j < frames; j++) buffer[j] = -1; System.out.println("Enter the reference string: "...

LRU Page Replacement algorithm in Java.(Least Recently Used)

import java.util.Scanner; public class lru {     public static int min(int counter[],int nFrames) {     int minimum = counter[0];     int pos = 0;     for(int i=0;i<nFrames;i++) { if(minimum > counter[i])           pos = i;     }  return pos;  }         public static void main(String[] args) {         // TODO code application logic here         Scanner s = new Scanner(System.in);     int n,recent = 0,pageFault = 0,nFrames;           System.out.print("Enter the number of pages: ");       n = s.nextInt();     int pageString[] = new int[n];     System.out.print("Enter the page reference string: ");     for(int i=0;i<n;i++)         pageString[i]=s.nextInt();            System.out.prin...

FIFO Page Replacement algorithm in Java.

import java.io.*; public class FIFO { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int frames, pointer = 0, hit = 0, fault = 0,ref_len; int buffer[]; int reference[]; int mem_layout[][]; System.out.println("Enter the number of Frames: "); frames = Integer.parseInt(br.readLine()); System.out.println("Enter the length of the Reference string: "); ref_len = Integer.parseInt(br.readLine()); reference = new int[ref_len]; mem_layout = new int[ref_len][frames]; buffer = new int[frames]; for(int j = 0; j < frames; j++) buffer[j] = -1; System.out.println("Enter the reference string: "); for(int i = 0; i < ref_len; i++) { reference[i] = Integer.parseInt(br.readLine()); ...