0% found this document useful (0 votes)
60 views57 pages

Krishna Os Practical

This document contains the details of 10 practical assignments completed by the student Krishna Rajbhar for the subject Operating System Practicals. It includes the index, objectives and outputs of each practical assignment. The practical assignments cover topics like process communication using shared memory, remote method invocation in client-server systems, implementing different CPU scheduling algorithms like FCFS, SJF, RR in Java, Banker's algorithm, page replacement algorithms and designing a file system.

Uploaded by

Kirti Rajbhar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views57 pages

Krishna Os Practical

This document contains the details of 10 practical assignments completed by the student Krishna Rajbhar for the subject Operating System Practicals. It includes the index, objectives and outputs of each practical assignment. The practical assignments cover topics like process communication using shared memory, remote method invocation in client-server systems, implementing different CPU scheduling algorithms like FCFS, SJF, RR in Java, Banker's algorithm, page replacement algorithms and designing a file system.

Uploaded by

Kirti Rajbhar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

NAME:- KRISHNA RAJBHAR

STD.:-. SYBSC. C.S.


DIV :-. B
ROLL NO. :- CS-5128
SUBJECT :- OPERATING SYSTEM PPRACTICA
INDEX
Practical PRACTICAL DATE
No.

1 Process Communication 23Sep 2021

2 Threads 25Sep 2021

3 Synchronization 30Sep 2021

4 Implement FCFS scheduling algorithm in Java. 05Aug 2021

5 Implement SJF (with no preemption) scheduling 26Aug 2021


algorithm in Java
6 Implement RR scheduling algorithm in Java 02Sep 2021

7 Write a Java program that implements the banker’s 23Sep 2021


algorithm
8 Write a Java program that implements the FIFO page- 09Sep 2021
replacement algorithm
9 Write a Java program that implements the LRU page-
replacement algorithm 09Sep 2021

10 Design a File System in Java. 25Sep 2021


Practical – 1. Process Communication

(i) Producer-consumer problem using shared memory

import java.util.LinkedList;

public class PCProblem{

public static void main(String[] args)

throws InterruptedException

final PC pc = new PC();

Thread t1 = new Thread(new Runnable(){

public void run()

try{

pc.produce();

catch(InterruptedException e){

e.printStackTrace();

}
});

Thread t2 = new Thread(new Runnable(){

@Override

public void run()

try{

pc.consume();

catch(InterruptedException e){

e.printStackTrace();

});

t1.start();

t2.start();

t1.join();

t2.join();

public static class PC{

LinkedList<Integer> list = new LinkedList<>();

int capacity = 2;

public void produce() throws InterruptedException

int value = 0;
while (true) {

synchronized (this)

while (list.size()== capacity)

wait();

System.out.println("Producer produced- "+value);

list.add(value++);

notify();

Thread.sleep(1000);

public void consume() throws InterruptedException

while (true) {

synchronized (this)

while (list.size()== 0)

wait();

int val = list.removeFirst();

System.out.println("Consumer consumed- "+val);

notify();

Thread.sleep(1000);

}
}

Output ->

(ii) One form of communication in a Client–Server Systems environment is Remote method invocation
(RMI). RMI is a Java feature similar to RPCs. RMI allows a thread to invoke a method on a remote object.
Objects are considered remote if they reside in a different Java virtual machine (JVM). Demonstrate RMI
program for adding/subtracting/multiplying/dividing two numbers.
SERVER.JAVA:------

import java.rmi.*;

import java.rmi.registry.*;

public class Server{

public static void main(String[] args) throws Exception

Impl obj = new Impl();

Naming.rebind("ADD", obj);

System.out.println("Server Started");

CLIENT.JAVA :·-----

import java.rmi.*;

import java.util.*;

public class Client{

public static void main(String[] args) throws Exception

Scanner sc = new Scanner(System.in);

while(true){

System.out.println("\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Exit");

System.out.println("Enter Your Choice: ");

int opt = sc.nextInt();

if (opt == 5){

break;

System.out.println("Enter First Number: ");


int a = sc.nextInt();

System.out.println("Enter Second Number: ");

int b = sc.nextInt();

int n;

switch (opt){

case 1:

AddInterface obj = (AddInterface)Naming.lookup("ADD");

n = obj.add(a, b);

System.out.println("Addition= "+n);

break;

case 2:

SubInterface obj1 = (SubInterface)Naming.lookup("ADD");

n = obj1.sub(a, b);

System.out.println("Subtraction= "+n);

break;

case 3:

MulInterface obj2 = (MulInterface)Naming.lookup("ADD");

n = obj2.mul(a, b);

System.out.println("Multiplication= "+n);

break;

case 4:

DivInterface obj3 = (DivInterface)Naming.lookup("ADD");

n = obj3.div(a, b);

System.out.println("Division= "+n);

break;

}
IMPL.JAVA:-------

import java.rmi.*;

import java.rmi.server.*;

public class Impl extends UnicastRemoteObject

implements AddInterface, SubInterface, MulInterface, DivInterface

public Impl() throws Exception {super();}

public int add(int x, int y){ return x + y;}

public int sub(int x, int y){ return x - y;}

public int mul(int x, int y){ return x * y;}

public int div(int x, int y){ return x / y;}

ADDINTERFACE.JAVA -------

import java.rmi.Remote;

public interface AddInterface extends Remote{

public int add(int x, int y) throws Exception;

SUBINTERFACE.JAVA -------

import java.rmi.Remote;

public interface SubInterface extends Remote{

public int sub(int x, int y) throws Exception;

MULINTERFACE.JAVA ------
import java.rmi.Remote;

public interface MulInterface extends Remote{

public int mul(int x, int y) throws Exception;

DIVINTERFACE.JAVA --------

import java.rmi.Remote;

public interface DivInterface extends Remote{

public int div(int x, int y) throws Exception;

Output ->

SERVER OUTPUT:-----
CLIENT OUTPUT:-------

.
(2)Threads:--
(I) The Java version of a multithreaded program that determines the summation of anon-negative
integer. The Summation class implements the Runnable interface. Thread Creation is performed by
creating an object instance of the Thread class and passing the Constructor a Runnable object.

import java.io.*;

import java.util.*;

class Sum

private int sum;

public int getSum()

return sum;

public void setSum(int sum)

this.sum = sum;

class Summation implements Runnable

private int upper;

private Sum sumValue;

public Summation(int upper, Sum sumValue)

this.upper = upper;

this.sumValue = sumValue;

}
public void run()

int sum = 0;

for (int i = 0; i <= upper; i++)

sum += i;

sumValue.setSum(sum);

public class Driver

public static void main(String[] args) {

if (args.length > 0) {

if (Integer.parseInt(args[0]) < 0)

System.err.println(args[0] + " must be >= 0.");

else {

// create the object to be shared

Sum sumObject = new Sum();

int upper = Integer.parseInt(args[0]);

Thread thrd = new Thread(new Summation(upper, sumObject));

thrd.start();

try {

thrd.join();

System.out.println

("The sum of "+upper+" is "+sumObject.getSum());

} catch (InterruptedException ie) { }

else

System.err.println("Usage: Summation <integer value>"); }


}

Output -------

(2)Write a multithreaded Java program that outputs prime numbers. This program should Work as
follows: The user will run the program and will enter a number on the Command line. The program will
then create a separate thread that outputs all the prime Numbers less than or equal to the number
entered by the user.

import java.io.*;

class Prime extends Thread

int ct=0,n=0,i=1,j=1,m;

Prime(int n)

this.m=n;

public void run()

try

System.out.println("Prime numbers from 1 to"+m+" are:");


int i =0;

int num =0;

for (i = 1; i <= m; i++)

int counter=0;

for(num =i; num>=1; num--)

if(i%num==0)

counter = counter + 1;

if (counter ==2)

System.out.println(i);

catch (Exception ex)

ex.printStackTrace();

}
class MainThread1

public static void main(String[] args)

try

Prime pr = new Prime(Integer.parseInt(args[0]));

fib.start();

System.out.println(args[0]);

catch (Exception ex)

ex.printStackTrace();

OUTPUT:-----
(3)The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5. 8, … Formally, it can be expressed as:
fib0 = 0, fib1 = 1, fibn = fibn-1 + fibn-2 Write a multithreaded program that generates the Fibonacci
sequence using either the Java,

import java.io.*;

class Fibonacci extends Thread

public void run()

try

int a=0, b=1, c=0;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the Limit for fibonacci: ");

int n = Integer.parseInt(br.readLine());

System.out.println("\n=================================");

System.out.println("Fibonacci series:");

while (n>0)

System.out.print(c+" ");

a=b;

b=c;

c=a+b;

n=n-1;
sleep(1000);

catch (Exception ex)

ex.printStackTrace();

class MainThread

public static void main(String[] args)

try

Fibonacci fib = new Fibonacci();

fib.start();

catch (Exception ex)

ex.printStackTrace();

}
OUTPUT:---

3. Synchronization:
(i) Give Java solution to Bounded buffer problem.

import java.util.LinkedList;

public class PCProblem{

public static void main(String[] args)

throws InterruptedException

final PC pc = new PC();

Thread t1 = new Thread(new Runnable(){

public void run()

try{

pc.produce();

}
catch(InterruptedException e){

e.printStackTrace();

});

Thread t2 = new Thread(new Runnable(){

@Override

public void run()

try{

pc.consume();

catch(InterruptedException e){

e.printStackTrace();

});

t1.start();

t2.start();

t1.join();

t2.join();

public static class PC{

LinkedList<Integer> list = new LinkedList<>();

int capacity = 2;
public void produce() throws InterruptedException

int value = 0;

while (true) {

synchronized (this)

while (list.size()== capacity)

wait();

System.out.println("Producer produced- "+value);

list.add(value++);

notify();

Thread.sleep(1000);

public void consume() throws InterruptedException

while (true) {

synchronized (this)

while (list.size()== 0)

wait();

int val = list.removeFirst();

System.out.println("Consumer consumed- "+val);

notify();
Thread.sleep(1000);

Output --------

(ii) Give solution to the readers–writers problem using Java synchronization.

import java.io.*;

import java.util.*;

class room

public synchronized void takesLecture(String name) throws

InterruptedException

System.out.println(name + "enters....");
Thread.sleep(250);

System.out.println(name + "starts lec....");

Thread.sleep(500);

System.out.println(name + "exits....");

Thread.sleep(250);

class lecturer implements Runnable

String name;

room r;

Thread t;

lecturer(String n,room r)

name=n;

t=new Thread(this,n);

this.r=r;

public void start()

t.start();

public void run()

try

r.takesLecture(name);

catch(InterruptedException e)
{

System.out.println(name + "operation interrupted....");

class shared

public static void main(String s[])

room r=new room();

lecturer Nita =new lecturer("Nita madam " ,r);

lecturer Punit =new lecturer("Punit sir " ,r);

lecturer Neha =new lecturer("Neha madam " ,r);

Nita.start();

Punit.start();

Neha.start();

Output --------
(iii) The Sleeping-Barber Problem: A barber shop consists of awaiting room with n chairs and a barber
room with one barber chair. If there are no customers to be served, the barber goes to sleep. If a
customer enters the barbershop and all chairs are occupied, then the customer leaves the shop. If the
barber is busy but chairs are available, then the customer sits in one of the free chairs. If the barber is
asleep, the customer wakes up the barber. Write a program to coordinate the barber and the
customers using Java synchronization

import java.util.concurrent.*;

class SB {

static Semaphore barber;

static Semaphore customer;

static Semaphore accessSeats;

static int seats = 2;

static int N = 6;

static void barber(){

new Thread(()->{

try{

while(true){

log("B: sleeping");

customer.acquire();

log("B: got customer");

accessSeats.acquire();

barber.release();

seats++;

accessSeats.release();

log("B: cutting hair");

Thread.sleep(1000);

log("B:done cutting hair");

}
}

catch(InterruptedException e){}

}).start();

static void customer(int i){

new Thread(()->{

try{

log("C"+i+": checking seats");

accessSeats.acquire();

if(seats<=0){

log("C"+i+": no seats,left the shop");

accessSeats.release();

return;

seats --;

customer.release();

accessSeats.release();

log("C"+i+": sat,seats= "+seats);

barber.acquire();

log("C"+i+": having hair cut");

catch(InterruptedException e){}

}).start();

public static void main(String[] args){

log("Starting barber (B) with "+seats+" seats "+" and 5 customers...");


barber = new Semaphore(0);

customer = new Semaphore(0);

accessSeats = new Semaphore(1);

barber();

for(int i = 1; i<N; i++){

sleep(1000 * Math.random());

customer(i);

static void sleep(double t){

try{Thread.sleep((long)t);}

catch(InterruptedException e){}

static void log (String x){

System.out.println(x);

OUTPUT:-----
4. Implement FCFS scheduling algorithm in Java.

import java.util.Scanner;

public class FCFS {

public static void main(String[] args) {

System.out.println("Enter the number of Process -> ");

Scanner in = new Scanner(System.in);

int numberOfProcess = in.nextInt();

int pid[] = new int[numberOfProcess];

int bt[] = new int[numberOfProcess];

int ar[] = new int[numberOfProcess];

int ct[] = new int[numberOfProcess];

int ta[] = new int[numberOfProcess];

int wt[] = new int[numberOfProcess];

float avgWait = 0, avg_TA = 0;

for(int k = 0; k < numberOfProcess; k++) {

System.out.println("Enter Process " + (k + 1) + " arrival time -> ");

ar[k] = in.nextInt();

System.out.println("Enter Process " + (k + 1) + " burst time -> ");

bt[k] = in.nextInt();

pid[k] = k + 1;
}

int temp;

for (int i = 0; i < numberOfProcess; i++) {

for (int j = i + 1; j < numberOfProcess; j++) {

if (ar[i] > ar[j]) {

temp = ar[i];

ar[i] = ar[j];

ar[j] = temp;

temp = pid[i];

pid[i] = pid[j];

pid[j] = temp;

temp = bt[i];

bt[i] = bt[j];

bt[j] = temp;

System.out.println();

ct[0] = bt[0] + ar[0];


for(int i = 1; i<numberOfProcess; i++) {

ct[i] = ct[i-1] + bt[i];

for(int i = 0; i<numberOfProcess; i++) {

ta[i] = ct[i] - ar[i];

avg_TA += ta[i];

wt[i] = ta[i] - bt[i];

avgWait += wt[i];

avgWait /= numberOfProcess;

avg_TA /= numberOfProcess;

System.out.println("******************************************");

System.out.println("Process\t\tAT\t\tBT\t\tCT\t\tTAT\t\tWT");

System.out.println("*******************************************");

for(int i = 0; i<numberOfProcess; i++) {

System.out.println(pid[i] + "\t\t" + ar[i] + "\t\t" + bt[i] + "\t\t" + ct[i] + "\t\t" + ta[i] + "\t\t" +
wt[i]);

System.out.println("\nAverage Waiting Time -> " + avgWait);

System.out.println("\nAverage Turn Around Time -> " + avg_TA + "\n");

}
}

Output --------
5. Implement SJF (with no preemption) scheduling algorithm in Java

import java.util.*;

public class SJF {

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println ("enter no of process:");

int n = sc.nextInt();

int pid[] = new int[n];

int at[] = new int[n];

int bt[] = new int[n];

int ct[] = new int[n];

int ta[] = new int[n];

int wt[] = new int[n];

int f[] = new int[n];

int st=0, tot=0;

float avgwt=0, avgta=0;

for(int i=0;i<n;i++)

System.out.println ("enter process " + (i+1) + " arrival time:");

at[i] = sc.nextInt();

System.out.println ("enter process " + (i+1) + " brust time:");

bt[i] = sc.nextInt();

pid[i] = i+1;
f[i] = 0;

boolean a = true;

while(true)

int c=n, min=999;

if (tot == n)

break;

for (int i=0; i<n; i++)

if ((at[i] <= st) && (f[i] == 0) && (bt[i]<min))

min=bt[i];

c=i;

if (c==n)

st++;

else

ct[c]=st+bt[c];

st+=bt[c];

ta[c]=ct[c]-at[c];

wt[c]=ta[c]-bt[c];

f[c]=1;

tot++;

}
}

System.out.println("\npid arrival brust complete turn waiting");

for(int i=0;i<n;i++)

avgwt+= wt[i];

avgta+= ta[i];

System.out.println(pid[i]+"\t"+at[i]+"\t"+bt[i]+"\t"+ct[i]+"\t"+ta[i]+"\t"+wt[i]);

System.out.println ("\naverage tat is "+ (float)(avgta/n));

System.out.println ("average wt is "+ (float)(avgwt/n));

sc.close();

}
OUTPUT:----
6. Implement RR scheduling algorithm in Java
import java.util.Scanner;

public class RoundRobin {

private static Scanner inp = new Scanner(System.in);

public static void main(String[] args) {

int n, tq, timer = 0, maxProcessIndex = 0;

float avgWait = 0, avgTT = 0;

System.out.println("\nEnter the time quantum -> ");

tq = inp.nextInt();

System.out.println("\nEnter the number of processes -> ");

n = inp.nextInt();

int arrival[] = new int[n];

int burst[] = new int[n];

int wait[] = new int[n];

int turn[] = new int[n];

int queue[] = new int[n];

int temp_burst[] = new int[n];

boolean complete[] = new boolean[n];

System.out.println("\nEnter the arrival time of the processes -> ");


for(int i = 0; i < n; i++) {

arrival[i] = inp.nextInt();

System.out.println("\nEnter the burst time of the processes -> ");

for(int i = 0; i < n; i++) {

burst[i] = inp.nextInt();

temp_burst[i] = burst[i];

for(int i = 0; i < n; i++) {

complete[i] = false;

queue[i] = 0;

while(timer < arrival[0])

timer++;

queue[0] = 1;

while(true) {

boolean flag = true;

for(int i = 0; i < n; i++) {

if(temp_burst[i] != 0) {

flag = false;

break;

if(flag)

break;
for(int i = 0; (i < n) && (queue[i] != 0); i++) {

int ctr = 0;

while((ctr < tq) && (temp_burst[queue[0]-1] > 0)) {

temp_burst[queue[0]-1] -= 1;

timer += 1;

ctr++;

checkNewArrival(timer, arrival, n, maxProcessIndex, queue);

if((temp_burst[queue[0]-1] == 0) && (complete[queue[0]-1] == false)) {

turn[queue[0]-1] = timer;

complete[queue[0]-1] = true;

boolean idle = true;

if(queue[n-1] == 0) {

for(int k = 0; k < n && queue[k] != 0; k++) {

if(complete[queue[k]-1] == false) {

idle = false;

else

idle = false;

if(idle) {

timer++;

checkNewArrival(timer, arrival, n, maxProcessIndex, queue);


}

queueMaintainence(queue, n);

for(int i = 0; i < n; i++) {

turn[i] = turn[i] - arrival[i];

wait[i] = turn[i] - burst[i];

System.out.println("\nPN\t\tAT\t\tBT\t\tTAT\t\tWT" + "\n");

for(int i = 0; i < n; i++) {

System.out.println(i+1 + "\t\t" + arrival[i] + "\t\t" + burst[i] + "\t\t" + turn[i] + "\t\t" + wait[i] +


"\n");

for(int i = 0; i < n; i++) {

avgWait += wait[i];

avgTT += turn[i];

System.out.println("\nAverage wait time : " + (avgWait/n) + "\naverage Turn Around Time : " +
(avgTT/n));

public static void queueUpdation(int queue[], int timer, int arrival[], int n, int maxProcessIndex) {

int zeroIndex = -1;


for(int i = 0; i < n; i++) {

if(queue[i] == 0) {

zeroIndex = i;

break;

if(zeroIndex == -1)

return;

queue[zeroIndex] = maxProcessIndex + 1;

public static void checkNewArrival(int timer, int arrival[], int n, int maxProcessIndex, int queue[]) {

if(timer <= arrival[n-1]) {

boolean newArrival = false;

for(int j = (maxProcessIndex+1); j < n; j++) {

if(arrival[j] <= timer) {

if(maxProcessIndex < j) {

maxProcessIndex = j;

newArrival = true;

if(newArrival)

queueUpdation(queue, timer, arrival, n, maxProcessIndex);

public static void queueMaintainence(int queue[], int n) {


for(int i = 0; (i < n-1) && (queue[i+1] != 0); i++) {

int temp = queue[i];

queue[i] = queue[i+1];

queue[i+1] = temp;

OUTPUT:-------
7. Write a Java program that implements the banker’s algorithm
import java.util.Scanner;

public class BankersAlgo {

int max[][];

int need[][];

int available[][];
int allocation[][];

int np, nr;

public void input() {

Scanner input = new Scanner(System.in);

System.out.println("Enter no. of processes and no. of resources -> ");

np = input.nextInt();

nr = input.nextInt();

max = new int[np][nr];

need = new int[np][nr];

available = new int[1][nr];

allocation = new int[np][nr];

System.out.println("Enter the allocation matrix");

for(int i = 0; i < nr; i++) {

char c = (char) ((char)65+i);

System.out.print(c + "\t");

System.out.println();

for(int i = 0; i < np; i++) {

for(int j = 0; j < nr; j++) {

allocation[i][j] = input.nextInt();

System.out.println("Enter max matrix");


for(int i = 0; i < nr; i++) {

char c = (char) ((char)65+i);

System.out.print(c + "\t");

System.out.println();

for(int i = 0; i < np; i++) {

for(int j = 0; j < nr; j++) {

max[i][j] = input.nextInt();

System.out.println("Enter the Available matrix");

for(int i = 0; i < nr; i++) {

char c = (char) ((char)65+i);

System.out.print(c + "\t");

System.out.println();

for(int i = 0; i < nr; i++) {

available[0][i] = input.nextInt();

input.close();

public void cal_need() {

for(int i = 0; i < np; i++) {

for(int j = 0; j < nr; j++) {


need[i][j] = max[i][j] - allocation[i][j];

public boolean check(int p) {

for(int i = 0; i < nr; i++) {

if(available[0][i] < need[p][i]);

return false;

return true;

public void algorithm() {

cal_need();

int c = 0;

boolean status[] = new boolean[np];

while(c < np) {

boolean allocated = false;

for(int i = 0; i < np; i++) {

if(!status[i] && check(i)) {

status[i] = true;

allocated = true;

c++;

System.out.println("Allocated process -> " + i);

for(int j = 0; j < nr; j++) {


available[0][j] = available[0][j] + allocation[i][j];

if(!allocated) break;

if(c == np)

System.out.println("\nSafely allocated");

else

System.out.println("\nAll processes cannot be allocated safely");

public static void main(String[] args) {

BankersAlgo obj = new BankersAlgo();

obj.input();

obj.algorithm();

Output ->
8. Write a Java program that implements the FIFO page-replacement
algorithm.

import java.io.BufferedReader;
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());

}
System.out.println();

for(int i = 0; i < ref_len; i++) {

int search = -1;

for(int j = 0; j < frames; j++) {

if(buffer[j] == reference[i]) {

search = j;

hit++;

break;

if(search == -1) {

buffer[pointer] = reference[i];

fault++;

pointer++;

if(pointer == frames)

pointer = 0;

for(int j = 0; j < frames; j++)

mem_layout[i][j] = buffer[j];

for(int i = 0; i < frames; i++) {

for(int j = 0; j < ref_len; j++)

System.out.printf("%3d ", mem_layout[j][i]);

System.out.println();

System.out.println("The number of Hits -> " + hit);

System.out.println("Hit Ratio -> " + (float)((float)hit/ref_len));

System.out.println("The number of faults -> " + fault);


}

OUTPUT:--------

9. Write a Java program that implements the LRU page-replacement


algorithm.
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) {

Scanner s = new Scanner(System.in);

int n, recent = 0, pageFault = 0, nFrames;

System.out.println("Enter the number of pages -> ");

n = s.nextInt();

int pageString[] = new int[n];

System.out.println("Enter the page reference string -> ");

for(int i = 0; i < n; i++)

pageString[i] = s.nextInt();

System.out.println("\nEnter the number of frames -> ");

nFrames = s.nextInt();

int frames[] = new int[nFrames];

int counter[] = new int[nFrames];


for(int i = 0; i < nFrames; i++) {

frames[i] = 0;

counter[i] = 0;

for(int i = 0; i<n; i++) {

int flag = 0;

for(int j = 0; j < nFrames; j++) {

if(frames[j] == pageString[i]) {

flag = 1;

counter[j] = recent++;

break;

if(flag == 0) {

for(int j = 0; j < nFrames; j++) {

if(frames[j] == 0) {

frames[j] = pageString[i];

counter[j] = recent++;

flag = 1;

pageFault++;

break;

if(flag == 0) {

int PositionToReplace = min(counter, nFrames);

frames[PositionToReplace] = pageString[i];
counter[PositionToReplace] = recent++;

pageFault++;

System.out.println();

for(int j = 0; j < nFrames; j++) {

System.out.print(frames[j] + " ");

System.out.print("\nPage Fault: " + pageFault);

OUTPUT:---
10. Design a File System in Java.
import java.io.FileWriter;

import java.io.IOException;

class Wfile

public static void main(String[] args) throws IOException

String str = "Creating file with JAVA "+"My name is Krishna";

FileWriter fw = new FileWriter("output.txt");

for(int i=0; i<str.length(); i++)

fw.write(str.charAt(i));

System.out.println("Writing Successful ! ");

fw.close();

OUTPUT:------

You might also like