Monday, 7 August 2017

fifo.java (OS)

import java.io.*;
class Stack
{
private int array[], frameSize, top, hit, fault;

Stack(int n)
{
frameSize = n;
array=new int[frameSize];
hit=0;
fault=0;
top=0;
for(int i=0;i<frameSize;i++)
array[i]=-1;
}
void print()
{
for(int i:array)
System.out.print(i+" ");
System.out.println();
}
boolean isHit(int n)
{
for(int i:array)
if(i==n)
{
hit++;
return true;
}
return false;
}
void insert(int n)
{
print();
if(!isHit(n))
{
array[top]=n;
top=(top+1)%frameSize;
fault++;
}
}
int getHit()
{
return hit;
}
int getFault()
{
return fault;
}
}
class FIFO
{
public static void main(String args[])
{
Stack s=new Stack(3);
int n=20;
int inp[]={7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};
for(int i=0;i<n;i++)
s.insert(inp[i]);
System.out.println("HIT : "+s.getHit()+" FAULT : "+s.getFault());
}
}

Output:

-1 -1 -1
7 -1 -1
7 0 -1
7 0 1
2 0 1
2 0 1
2 3 1
2 3 0
4 3 0
4 2 0
4 2 3
0 2 3
0 2 3
0 2 3
0 1 3
0 1 2
0 1 2
0 1 2
7 1 2
7 0 2
HIT : 5 FAULT : 15

fcfs.java (OS)

import java.util.Scanner;

class fcfs
{
public static void main(String ... args)
{
int n,curTime=0,sumTAT=0,sumWT=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of process : ");
n = sc.nextInt();
int AT[]=new int[n];
int BT[]=new int[n];
int WT[]=new int[n];
int TAT[]=new int[n];
for(int i=0; i<n; i++)
{
System.out.println("Enter AT of #"+(i+1)+"No.: ");
AT[i]=sc.nextInt();
System.out.println("Enter BT of #"+(i+1)+"No.: ");
BT[i]=sc.nextInt();
}
for(int i=0; i<n; i++)
{
WT[i]=curTime-AT[i];
curTime+=BT[i];
TAT[i]=curTime-AT[i];
sumWT+=WT[i];
sumTAT+=TAT[i];
}
System.out.println("AT\tBT\tWT\tTAT\n-------------------------------");
for(int i=0; i<n; i++)
System.out.println(AT[i]+"\t"+BT[i]+"\t"+WT[i]+"\t"+TAT[i]);
System.out.println("AvgWT = " + ((float)  sumWT/n) +"\nAvgTAT = " +  ((float) sumTAT/n));
}
}

Output:

E:\os write ups>javac fcfs.java

E:\os write ups>java fcfs

Enter number of process :
5
Enter AT of #1No.:
5
Enter BT of #1No.:
6
Enter AT of #2No.:
2
Enter BT of #2No.:
1
Enter AT of #3No.:
7
Enter BT of #3No.:
8
Enter AT of #4No.:
5
Enter BT of #4No.:
4
Enter AT of #5No.:
6
Enter BT of #5No.:
2
AT      BT      WT      TAT
-------------------------------
5       6       -5      1
2       1       4       5
7       8       0       8
5       4       10      14
6       2       13      15
AvgWT = 4.4
AvgTAT = 8.6