Os Lab Programs 1 9
Os Lab Programs 1 9
Sc (MPCS/MSCS)
Experiment: 1
Experiment Name: Round Robin CPU Scheduling algorithm
rem_bt[i] = 0;
}
sq = sq + temp;
tat[i] = sq;
}
if(n == count)
break;
}
System.out.print("--------------------------------------------------------------------------------");
System.out.print("\nProcess\t Burst Time\t Turnaround Time\t Waiting Time\n");
System.out.print("--------------------------------------------------------------------------------");
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt[i];
awt=awt+wt[i];
atat=atat+tat[i];
System.out.print("\n "+(i+1)+"\t \t"+bt[i]+"\t\t "+tat[i]+"\t\t\t "+wt[i]+"\n");
}
awt=awt/n;
atat=atat/n;
System.out.println("\nAverage waiting Time = "+awt+"\n");
System.out.println("Average turnaround time = "+atat);
}
}
Output:
Quantum =4
E:\>javac RoundRobin.java Process Burst Time
E:\>java RoundRobin P1 24
Enter the number of process P2 3
(maximum 10) = 3
P3 3
Enter the burst time of the process
P0 = 24
P1 = 3
P2 = 3
Enter the quantum time: 4
--------------------------------------------------------------------------------
Process Burst Time Turnaround Time Waiting Time
--------------------------------------------------------------------------------
1 24 30 6
2 3 7 4
3 3 10 7
Experiment: 2
Experiment Name: SJF CPU scheduling algorithm
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)
ta[c]=ct[c]-at[c];
wt[c]=ta[c]-bt[c];
f[c]=1;
tot++;
}
}
System.out.println("\nPid Arrival Burst 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 ("Average wt is "+ (float)(avgwt/n));
System.out.println ("\nAverage tat is "+ (float)(avgta/n));
sc.close();
}
}
Output: Process Arrival Burst Time
E:\>javac Sjf.java Time
P1 0 6
E:\>java Sjf P2 0 8
Enter No of Process: 4 P3 0 7
Enter process 1 Arrival time: 0 P4 0 3
Enter process 1 Burst time: 6
Enter process 2 Arrival time: 0
Enter process 2 Burst time: 8
Enter process 3 Arrival time: 0
Enter process 3 Burst time: 7
Enter process 4 Arrival time: 0
Enter process 4 Burst time: 3
Average wt is 7.0
Average tat is 13.0
Experiment: 3
Experiment Name: FCFS CPU Scheduling algorithm
Program:
import java.util.*;
public class Fcfs
{
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 ar[]=new int[n];
int bt[]=new int[n];
int ct[]=new int[n];
int ta[]=new int[n];
int wt[]=new int[n];
int temp;
int avgwt=0,avgta=0;
for(int i=0;i<n;i++)
{
System.out.println("Enter process"+(i+1)+"\t Arrival time:");
ar[i]=sc.nextInt();
System.out.println("Enter process"+(i+1)+"\t Burst time: ");
bt[i]=sc.nextInt();
pid[i]=i+1;
}
for(int i=0;i<n;i++) {
for(int j=0;j<n-(i+1);j++) {
if(ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
temp=bt[j];
bt[j]=bt[j+1];
bt[j+1]=temp;
temp=pid[j];
pid[j]=pid[j+1];
pid[j+1]=temp;
}}}
for(int i=0;i<n;i++)
{
if(i==0)
{
ct[i]=ar[i]+bt[i];
}
else
{
if(ar[i]>ct[i-1])
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)
{
ct[i]=ar[i]+bt[i];
}
else
ct[i]=ct[i-1]+bt[i];
}
ta[i]=ct[i]-ar[i];
wt[i]=ta[i]-bt[i];
avgwt +=wt[i];
avgta +=ta[i];
}
System.out.println("\n P_id \t Arrivel_Time \t Burst_Time \t Complete_Time \t
Turnaround_Time \tWaiting_Time");
for(int i=0;i<n;i++)
{
System.out.println("\t"+pid[i]+"\t"+ar[i]+"\t\t"+bt[i]+"\t\t"+ct[i]+"\t\t"+ta[i]+"\t\t"+wt[i]);
}
System.out.println("\n average waiting time:"+(avgwt/n));
System.out.println("\n average turn around time:"+(avgta/n));
sc.close();
}}
Output: Process Arrival Time Burst Time
E:\>javac Fcfs.java
P1 0 24
E:\>java Fcfs
P2 0 3
Enter no of process: 3
P3 0 3
Enter process 1 arrival time: 0
Enter process 1 burst time: 24
Enter process 2 arrival time: 0
Enter process 2 burst time: 3
Enter process 3 arrival time: 0
Enter process 3 burst time: 3
PID Arrival Burst complete turn waiting
1 0 24 24 24 0
2 0 3 27 27 24
3 0 3 30 30 27
Average waiting time: 17.0
Average turnaround time: 27.0
Experiment: 4
Experiment Name: Priority CPU Scheduling algorithm
Program:
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)
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[],i;
float awt,atat;
p = new int[10];
pp = new int[10];
bt = new int[10];
w = new int[10];
t = new int[10];
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(int j=i+1;j<n;j++)
{
if(pp[i]>pp[j])
{
x=pp[i];
pp[i]=pp[j];
pp[j]=x;
x=bt[i];
bt[i]=bt[j];
bt[j]=x;
x=p[i];
p[i]=p[j];
p[j]=x;
}
}
}
w[0]=0;
awt=0;
t[0]=bt[0];
atat=t[0];
for(i=1;i<n;i++)
{
w[i]=t[i-1];
awt+=w[i];
t[i]=w[i]+bt[i];
atat+=t[i];
}
System.out.print("\n\nProcess \t Burst Time \t Wait Time \t Turn Around Time Priority \
n");
for(i=0;i<n;i++)
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)
Output:
E:\>javac Priority.java
E:\>java Priority
Enter the number of process: 5
Enter burst time: time priorities Process [1]:10
3
Process[2]:1 Process Burst Time Priority
1 P1 10 3
Process[3]:2
P2 1 1
4
Process[4]:1 P3 2 4
5 P4 1 5
Process[5]:5 P5 5 2
2
Process Burst Time Wait Time Turn_around_Time Priority
2 1 0 1 1
5 5 1 6 2
1 10 6 16 3
3 2 16 18 4
4 1 18 19 5
Experiment: 5
Experiment Name: Bankers Algorithm for Dead Lock Avoidance.
Aim: Write a program for Bankers Algorithm for Dead Lock Avoidance.
Program:
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)
import java.util.*;
import java.io.*;
import java.util.Scanner;
class BankersAlgoExample
{
static void findNeedValue(int needArray[][], int maxArray[][], int allocationArray[][], int totalProcess,
int totalResources)
{
for (int i = 0 ; i < totalProcess ; i++){ // for each process
for (int j = 0 ; j < totalResources ; j++){ //for each resource
needArray[i][j] = maxArray[i][j] - allocationArray[i][j];
}
}
}
static boolean checkSafeSystem(int processes[], int availableArray[], int maxArray[][], int
allocationArray[][], int totalProcess, int totalResources)
{
int [][]needArray = new int[totalProcess][totalResources];
findNeedValue(needArray, maxArray, allocationArray, totalProcess, totalResources);
boolean []finishProcesses = new boolean[totalProcess];
int []safeSequenceArray = new int[totalProcess];
int []workArray = new int[totalResources];
for (int i = 0; i < totalResources ; i++)
workArray[i] = availableArray[i];
int counter = 0;
while (counter < totalProcess)
{
boolean foundSafeSystem = false;
for (int m = 0; m < totalProcess; m++)
{
if (finishProcesses[m] == false)
{
int j;
for (j = 0; j < totalResources; j++)
if (needArray[m][j] > workArray[j])
break;
if (j == totalResources)
{
for (int k = 0 ; k < totalResources ; k++)
workArray[k] += allocationArray[m][k];
safeSequenceArray[counter++] = m;
finishProcesses[m] = true;
foundSafeSystem = true;
}
}
}
if (foundSafeSystem == false)
{
System.out.print("The system is not in the safe state because lack of resources");
return false;
}
}
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)
Output:
E:\>javac BankersAlgoExample.java
E:\>java BankersAlgoExample
Enter total number of processes: 5
Enter total number of resources: 3
Enter the availability of resource0:3
Enter the availability of resource1:3
Enter the availability of resource2:2
Enter the maximum resource0 that can be allocated to process0:7
Enter the maximum resource1 that can be allocated to process0:5
Enter the maximum resource2 that can be allocated to process0:3
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)
Experiment: 6
Experiment Name: simulate FIFO Page replacement algorithms.
}
Output :
output:-
Please enter the number of Frames:
3
Please enter the length of the Reference string:
20
Please enter the reference string:
7
0
1
2
0
3
0
4
2
3
0
3
2
1
2
0
1
7
0
1
7 7 7 2 2 2 2 4 4 4 0 0 0 0 0 0 0 7 7 7
-1 0 0 0 0 3 3 3 2 2 2 2 2 1 1 1 1 1 0 0
-1 -1 1 1 1 1 0 0 0 3 3 3 3 3 2 2 2 2 2 1
The number of Hits: 5
Hit Ratio: 0.25
The number of Faults: 15
Experiment: 7
Experiment Name: simulate LRU Page replacement algorithms.
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int frames,pointer = 0, hit = 0, fault = 0,ref_len;
Boolean isFull = false;
int buffer[];
ArrayList<Integer> stack = new ArrayList<Integer>();
int reference[];
int mem_layout[][];
System.out.println("Please enter the number of Frames: ");
frames = Integer.parseInt(br.readLine());
System.out.println("Please 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("Please 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++)
{
if(stack.contains(reference[i]))
{
stack.remove(stack.indexOf(reference[i]));
}
stack.add(reference[i]);
int search = -1;
for(int j = 0; j < frames; j++)
{
if(buffer[j] == reference[i])
{
search = j;
hit++;
break;
}
}
if(search == -1)
{
if(isFull)
{
int min_loc = ref_len;
for(int j = 0; j < frames; j++)
{
if(stack.contains(buffer[j]))
{
int temp = stack.indexOf(buffer[j]);
if(temp < min_loc)
{
min_loc = temp;
pointer = j;
}
}
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)
}
}
buffer[pointer] = reference[i];
fault++;
pointer++;
if(pointer == frames)
{
pointer = 0;
isFull = true;
}
}
for(int j = 0; j < frames; j++)
mem_layout[i][j] = buffer[j];
}
}
Output:-
Please enter the number of Frames:
3
Please enter the length of the Reference string:
20
Please enter the reference string:
7
0
1
2
0
3
0
4
2
3
0
3
2
1
2
0
1
7
0
1
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)
7 7 7 2 2 2 2 4 4 4 0 0 0 1 1 1 1 1 1 1
-1 0 0 0 0 0 0 0 0 3 3 3 3 3 3 0 0 0 0 0
-1 -1 1 1 1 3 3 3 2 2 2 2 2 2 2 2 2 7 7 7
The number of Hits: 8
Hit Ratio: 0.4
The number of Faults: 12
Experiment: 8
Experiment Name: simulate LFU Page replacement algorithms.
package LFU;
//author: Nighut Chirag Ashok (@qawbecrdteyf)
import java.util.HashSet;
if(cache[j] == -1)
{
cache[j] = pages[i];
frequency[j]++;
done = 1;
break;
}
}
}
if(done == 0)
{
for(int k = 0; k< capacity; k++)
{
if(frequency[k] < minf)
{
minf = frequency[k];
minfpos = k;
}
}
cache[minfpos] = pages[i];
frequency[minfpos] = 1;
}
for(int m = 0; m< capacity; m++){
if(cache[m] != -1)
System.out.print(cache[m] + " -> ");
}
System.out.print("END");
System.out.println();
}
}
}
Output:
E:\>javac Lfu.java
E:\>java Lfu
1 -> END
1 -> 2 -> END
1 -> 2 -> END
1 -> 2 -> END
1 -> 2 -> 4 -> END
1 -> 2 -> 4 -> 7 -> END
1 -> 2 -> 0 -> 7 -> END
1 -> 2 -> 3 -> 7 -> END
1 -> 2 -> 3 -> 7 -> END
1 -> 2 -> 4 -> 7 -> END
1 -> 2 -> 4 -> 7 -> END
1 -> 2 -> 0 -> 7 -> END
1 -> 2 -> 0 -> 7 -> END
Experiment: 9
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)
st=200;
ctr=ctr-1;
status[2]=0;
}
}
}
return flg;
}
public static int findmaxtime()
{
int m=time[0];
for(int i=0;i<no;i++)
{
if(time[i]>m && flag[i]==1)
{
m=time[i];
}
}
return m;
}
public static void display()
{
System.out.println("\nProcess\tTime\tSize\tStart\tEnd\tFlag");
for(int i=0;i<no;i++)
{
System.out.println(proc[i]+"\t"+time[i]+"\t"+size[i]+"\t"+start[i]+"\t"+end[i]+"\t"+flag[i]);
}
}
public static void allocate(int i)
{
int temp=0;
for(int j=1;j<=3;j++)
{
if(flag[i]==0 && ctr<3)
{
temp=part*j;
if(size[i]<=temp)
{
if(status[ctr]==0)
{
for(int k=1;k<=j;k++)
{
mem[k-1]=proc[i];
status[k-1]=1;
ctr=ctr+(k);
}
start[i]=st;
end[i]=st+temp;
st=end[i];
System.out.println("Process:= "+ proc[i]+" allocated");
flag[i]=1;
break;
}
}
}
}
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)
}
public static void main(String args[]) throws IOException
{
int maxtime,z,gflag=0;
Scanner sc=new Scanner(System.in);
System.out.println("Please Enter the number of processes:- ");
no=sc.nextInt();
proc=new char[no];
size=new int[no];
time=new int[no];
flag=new int[no];
start=new int[no];
end=new int[no];
for(int i=0;i<no;i++)
{
System.out.println("Enter process "+ (i+1) +" information:- ");
proc[i]=(char)System.in.read();
size[i]=sc.nextInt();
time[i]=sc.nextInt();
flag[i]=0;
}
display();
for(int i=0;i<no;i++)
{
if(memory<size[i])
{
System.out.println("Process:= "+proc[i]+" too large for memory......Skipping");
flag[i]=1;
continue;
}
allocate(i);
}
display();
maxtime=findmaxtime();
while(gflag==0)
{
for(int i=1;i<=maxtime;i++)
{
System.out.println("Time:- "+i);
z=checktodeallocate(i);
if(z==1)
{
display();
for(int k=0;k<no;k++)
{
if(flag[k]==0)
{
allocate(k);
display();
}
else
{
gflag=1;
}
}
}
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)
}
}
}
}
Output:
E:\>javac Mft.java
E:\>java Mft
Please Enter the number of processes:-
3
Enter process 1 information:-
1
2
3
Enter process 2 information:-
4
5
6
Enter process 3 information:-
7
6
8
Time:- 8