Krishna Os Practical
Krishna Os Practical
import java.util.LinkedList;
throws InterruptedException
try{
pc.produce();
catch(InterruptedException e){
e.printStackTrace();
}
});
@Override
try{
pc.consume();
catch(InterruptedException e){
e.printStackTrace();
});
t1.start();
t2.start();
t1.join();
t2.join();
int capacity = 2;
int value = 0;
while (true) {
synchronized (this)
wait();
list.add(value++);
notify();
Thread.sleep(1000);
while (true) {
synchronized (this)
while (list.size()== 0)
wait();
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.*;
Naming.rebind("ADD", obj);
System.out.println("Server Started");
CLIENT.JAVA :·-----
import java.rmi.*;
import java.util.*;
while(true){
System.out.println("\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Exit");
if (opt == 5){
break;
int b = sc.nextInt();
int n;
switch (opt){
case 1:
n = obj.add(a, b);
System.out.println("Addition= "+n);
break;
case 2:
n = obj1.sub(a, b);
System.out.println("Subtraction= "+n);
break;
case 3:
n = obj2.mul(a, b);
System.out.println("Multiplication= "+n);
break;
case 4:
n = obj3.div(a, b);
System.out.println("Division= "+n);
break;
}
IMPL.JAVA:-------
import java.rmi.*;
import java.rmi.server.*;
ADDINTERFACE.JAVA -------
import java.rmi.Remote;
SUBINTERFACE.JAVA -------
import java.rmi.Remote;
MULINTERFACE.JAVA ------
import java.rmi.Remote;
DIVINTERFACE.JAVA --------
import java.rmi.Remote;
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
return sum;
this.sum = sum;
this.upper = upper;
this.sumValue = sumValue;
}
public void run()
int sum = 0;
sum += i;
sumValue.setSum(sum);
if (args.length > 0) {
if (Integer.parseInt(args[0]) < 0)
else {
thrd.start();
try {
thrd.join();
System.out.println
else
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.*;
int ct=0,n=0,i=1,j=1,m;
Prime(int n)
this.m=n;
try
int counter=0;
if(i%num==0)
counter = counter + 1;
if (counter ==2)
System.out.println(i);
ex.printStackTrace();
}
class MainThread1
try
fib.start();
System.out.println(args[0]);
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.*;
try
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);
ex.printStackTrace();
class MainThread
try
fib.start();
ex.printStackTrace();
}
OUTPUT:---
3. Synchronization:
(i) Give Java solution to Bounded buffer problem.
import java.util.LinkedList;
throws InterruptedException
try{
pc.produce();
}
catch(InterruptedException e){
e.printStackTrace();
});
@Override
try{
pc.consume();
catch(InterruptedException e){
e.printStackTrace();
});
t1.start();
t2.start();
t1.join();
t2.join();
int capacity = 2;
public void produce() throws InterruptedException
int value = 0;
while (true) {
synchronized (this)
wait();
list.add(value++);
notify();
Thread.sleep(1000);
while (true) {
synchronized (this)
while (list.size()== 0)
wait();
notify();
Thread.sleep(1000);
Output --------
import java.io.*;
import java.util.*;
class room
InterruptedException
System.out.println(name + "enters....");
Thread.sleep(250);
Thread.sleep(500);
System.out.println(name + "exits....");
Thread.sleep(250);
String name;
room r;
Thread t;
lecturer(String n,room r)
name=n;
t=new Thread(this,n);
this.r=r;
t.start();
try
r.takesLecture(name);
catch(InterruptedException e)
{
class shared
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 int N = 6;
new Thread(()->{
try{
while(true){
log("B: sleeping");
customer.acquire();
accessSeats.acquire();
barber.release();
seats++;
accessSeats.release();
Thread.sleep(1000);
}
}
catch(InterruptedException e){}
}).start();
new Thread(()->{
try{
accessSeats.acquire();
if(seats<=0){
accessSeats.release();
return;
seats --;
customer.release();
accessSeats.release();
barber.acquire();
catch(InterruptedException e){}
}).start();
barber();
sleep(1000 * Math.random());
customer(i);
try{Thread.sleep((long)t);}
catch(InterruptedException e){}
System.out.println(x);
OUTPUT:-----
4. Implement FCFS scheduling algorithm in Java.
import java.util.Scanner;
ar[k] = in.nextInt();
bt[k] = in.nextInt();
pid[k] = k + 1;
}
int temp;
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();
avg_TA += ta[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("*******************************************");
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]);
}
}
Output --------
5. Implement SJF (with no preemption) scheduling algorithm in Java
import java.util.*;
int n = sc.nextInt();
for(int i=0;i<n;i++)
at[i] = sc.nextInt();
bt[i] = sc.nextInt();
pid[i] = i+1;
f[i] = 0;
boolean a = true;
while(true)
if (tot == n)
break;
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++;
}
}
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]);
sc.close();
}
OUTPUT:----
6. Implement RR scheduling algorithm in Java
import java.util.Scanner;
tq = inp.nextInt();
n = inp.nextInt();
arrival[i] = inp.nextInt();
burst[i] = inp.nextInt();
temp_burst[i] = burst[i];
complete[i] = false;
queue[i] = 0;
timer++;
queue[0] = 1;
while(true) {
if(temp_burst[i] != 0) {
flag = false;
break;
if(flag)
break;
for(int i = 0; (i < n) && (queue[i] != 0); i++) {
int ctr = 0;
temp_burst[queue[0]-1] -= 1;
timer += 1;
ctr++;
turn[queue[0]-1] = timer;
complete[queue[0]-1] = true;
if(queue[n-1] == 0) {
if(complete[queue[k]-1] == false) {
idle = false;
else
idle = false;
if(idle) {
timer++;
queueMaintainence(queue, n);
System.out.println("\nPN\t\tAT\t\tBT\t\tTAT\t\tWT" + "\n");
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) {
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(maxProcessIndex < j) {
maxProcessIndex = j;
newArrival = true;
if(newArrival)
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;
int max[][];
int need[][];
int available[][];
int allocation[][];
np = input.nextInt();
nr = input.nextInt();
System.out.print(c + "\t");
System.out.println();
allocation[i][j] = input.nextInt();
System.out.print(c + "\t");
System.out.println();
max[i][j] = input.nextInt();
System.out.print(c + "\t");
System.out.println();
available[0][i] = input.nextInt();
input.close();
return false;
return true;
cal_need();
int c = 0;
status[i] = true;
allocated = true;
c++;
if(!allocated) break;
if(c == np)
System.out.println("\nSafely allocated");
else
obj.input();
obj.algorithm();
Output ->
8. Write a Java program that implements the FIFO page-replacement
algorithm.
import java.io.BufferedReader;
import java.io.*;
int buffer[];
int reference[];
int mem_layout[][];
frames = Integer.parseInt(br.readLine());
ref_len = Integer.parseInt(br.readLine());
buffer[j] = -1;
reference[i] = Integer.parseInt(br.readLine());
}
System.out.println();
if(buffer[j] == reference[i]) {
search = j;
hit++;
break;
if(search == -1) {
buffer[pointer] = reference[i];
fault++;
pointer++;
if(pointer == frames)
pointer = 0;
mem_layout[i][j] = buffer[j];
System.out.println();
OUTPUT:--------
int pos = 0;
pos = i;
return pos;
n = s.nextInt();
pageString[i] = s.nextInt();
nFrames = s.nextInt();
frames[i] = 0;
counter[i] = 0;
int flag = 0;
if(frames[j] == pageString[i]) {
flag = 1;
counter[j] = recent++;
break;
if(flag == 0) {
if(frames[j] == 0) {
frames[j] = pageString[i];
counter[j] = recent++;
flag = 1;
pageFault++;
break;
if(flag == 0) {
frames[PositionToReplace] = pageString[i];
counter[PositionToReplace] = recent++;
pageFault++;
System.out.println();
OUTPUT:---
10. Design a File System in Java.
import java.io.FileWriter;
import java.io.IOException;
class Wfile
fw.write(str.charAt(i));
fw.close();
OUTPUT:------