C.
Algorithm Simulation
First Come First Serve (FCFS)
Source Code :
package osfinals;
import java.util.Scanner;
public class Fcfs {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Enter Number of Process : ");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int pd[] = new int [n];
int at[] = new int[n];
int bt[] = new int [n];
int ct[] = new int [n];
int tat[] = new int [n];
int wt[] = new int [n];
float tatave = 0, wtave = 0;
for (int i = 0; i < n; i++) {
System.out.println("Enter Arrival Time");
System.out.print("P" + (i+1) + ":");
at[i] = in.nextInt();
System.out.println("Enter Burst Time");
System.out.print("P" + (i+1) + ":");
bt[i] = in.nextInt();
pd[i] = i + 1;
}
int temp;
for (int j = 0; j < n; j++) {
for (int k = j+1; k < n; k++) {
if (at[j] > at[k]) {
temp = pd[j];
pd[j] = pd[k];
pd[k] = temp;
temp = at[j];
at[j] = at[k];
at[k] = temp;
temp = bt[j];
bt[j] = bt[k];
bt[k] = temp;
}
}
}
for (int i = 0; i < n; i++) {
if (i == 0) {
ct[i] = at[i] + bt[i];
}
else {
if(at[i] > ct[i-1]) {
ct[i] = at[i] + bt[i];
}
else {
ct[i] = ct[i-1] + bt[i];
}
}
tat[i] = ct[i] - at[i];
wt[i] = tat[i] - bt[i];
wtave += wt[i];
tatave += tat[i];
}
System.out.println("Process \t Arrival Time \t Burst
Time \t Completion Time \t Turn-Around Time \t Waiting Time");
for(int z = 0; z < n; z++) {
System.out.println(" " + pd[z] + "\t\t\t" + at[z]
+ "\t \t" + bt[z] + "\t\t" + ct[z] + "\t \t\t" + tat[z] + "\t\t\t" + wt[z]);
}
System.out.println("Gantt Chart");
for (int y = 0; y < n; y++) {
System.out.print("P" + pd[y] + "\t");
}
System.out.println();
System.out.println("Turn-Around Time : " + (tatave));
System.out.println("Average Turn-Around Time : " +
(tatave/n) + "ms");
System.out.println("Waiting Time : " + (wtave));
System.out.println("Average Waiting Time : " + (wtave/n) +
"ms");
}
}
FCFS Flowchart
Start
int pd [ ] = 0
int at [ ] = 0
int bt [ ] = 0
int ct [ ] = 0
int ta [ ] = 0
int wt [ ] = 0
int f [ ] = 0
int st [ ] = 0
int tot [ ] = 0
float avg wt = 0
float avg ta = 0
Input n
i=0
i<n
Enter burst time i=i+1
bt = [ i ]
Enter arrival time
at = A[ i ]
Display pid at bt ct ta wt
i=0 i = i + tt
Display
pid [ i ] + “\t” + at [ i ] + “\t” + bt [ i ] + “\t” + ct [ i ] + “\t” +
ta [ i ] + “\t” + wt [ i ] );
avg wt /n
avg ta /n
Display
avg wt
avg ta
STOP
Shortest Job First (SJF)
Source Code :
package osfinals;
import java.util.Scanner;
public class Sjf {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner (System.in);
System.out.print("Enter Number of Process : ");
int n = in.nextInt();
int pd[] = new int [n];
int at[] = new int [n];
int bt[] = new int [n];
int ct[] = new int [n];
int tat[] = new int [n];
int wt[] = new int [n];
int f[] = new int [n];
int st = 0, tot = 0;
float tatave = 0, wtave = 0;
for(int i = 0; i < n; i++) {
System.out.println("Enter Arrival Time");
System.out.print("P" + (i+1) + ":");
at[i] = in.nextInt();
System.out.println("Enter Burst Time");
System.out.print("P" + (i+1) + ":");
bt[i] = in.nextInt();
pd[i] = i + 1;
f[i] = 0;
}
boolean a = true;
while(true) {
int c = n, min = 9999;
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];
tat[c] = ct[c] - at[c];
wt[c] = tat[c] - bt[c];
f[c] = 1;
tot++;
}
}
System.out.println("Process \t Arrival Time \t Burst
Time \t Completion Time\t Turn-Around Time\t Waiting Time");
for(int i = 0; i < n; i++) {
wtave += wt[i];
tatave += tat[i];
System.out.println(" " + pd[i] + "\t\t\t" + at[i]
+ "\t\t" + bt[i] + "\t\t" + ct[i] + "\t\t\t" + tat[i] + "\t\t\t" + wt[i]);
}
System.out.println("Turn-Around Time :" + (tatave));
System.out.println("Average Turn-Around Time : " +
(tatave/n) + "ms");
System.out.println("Waiting Time :" + (wtave));
System.out.println("Average Waiting Time : " + (wtave/n) +
"ms");
}
}
SJF Flowchart
Start
int pid [ ] = 0
int ar [ ] = 0
int bt [ ] = 0
int ct [ ] = 0
int ta [ ] = 0
int wt [ ] = 0
float avg wt = 0
float avg ta = 0
Input n
i=0
i=i+1
i<n
Enter arrival time
ar =1 ;
Enter burst time
bt = 1;
A
temp = ar [ j ];
ar [ j ] = ar [ j + 1 ];
ar[ j + 1] = temp;
temp = b[ j + 1 ];
bt [ j ] = bt [ j + 1 ];
bt [ j + 1 ] = temp;
temp = pid [ j ];
pid [ j ] = pid [ j + 1 ];
pid [ j + 1 ] = temp;
i=0 i = i++
c t [ i ] = ar [ i ] + bt [ i ];
ar [ i ] > ct [ i – 1] c t [ i ] =ct [ i - 1] + bt [ i ];
c t [ i ] = ar [ i ] + bt [ i ]
ta [ i ] = ct [ i ] – ar [ i ];
wt [ i ] = at [ i ] – bt [ i ];
avgwt + = wt [ i ];
avgta + = ta [ i ];
pid [ i ] = i + 1;
f [ i ] = 0;
ct [ a ] = st + bt [ a ];
a=n min = 999
st = bt [ a ];
ta [ a ] = ct [ a ] – at [ a ];
wt [ a ] = ta [ a ] – bt [ a ];
f[a]=1
atA[ i ] < =st &&
f [i] ==0 && bt [ i ] < min
pid at BT CT WT
avg wt + = wt [ i ]
i++
avg ta + = ta [ i ]
pid [ i ] + at [ i ] + bt [ i ] + ct [ i ] + ta [ i ] + wt [ i ]
avg ta\n
avg wt \n
Display avgta , avg wt
Preemptive
Source Code
package osfinals;
import java.util.*;
public class Preemptive {
public static void main(String[] args) {
int aa, b, c, i, ii, iii, iiii;
int no_Process, total_Burst_Time = 0;
int totalwaitingTime = 0, totalturnaroundtime = 0;;
int count = 0;
double large = 0, averagewaitingTime = 0,
averageturnaroundtime = 0;
double num;
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of process = ");
no_Process = in.nextInt();
ArrayList<process> process = new ArrayList<>();
for (i = 0; i < no_Process; i++) {
process.add(new process());
}
System.out.println("Enter the number");
for (ii = 0; ii < no_Process; ii++) {
total_Burst_Time += process.get(ii).burstTime;
process.get(ii).burstTime1 =
process.get(ii).burstTime;
process.get(ii).completion = 0;
}
while (count <= total_Burst_Time) {
b = 0;
large = 0;
for (aa = 0; aa < no_Process; aa++) {
if (process.get(aa).arrivalTime > count||
process.get(aa).burstTime<=0) {
b++;
}
}
if (b == no_Process) {
count++;
continue;
}
for (iii = 0; iii < no_Process; iii++) {
if (process.get(iii).arrivalTime > count ||
process.get(iii).burstTime <= 0) {
continue;
}
else {
if (process.get(iii).priorityvalue > large) {
large = process.get(iii).priorityvalue;
}
}
}
for (int iiiii = 0; iiiii < no_Process; iiiii++) {
if (large == process.get(iiiii).priorityvalue) {
process.get(iiiii).burstTime =
process.get(iiiii).burstTime - 1;
count++;
System.out.println(" this one
"+process.get(iiiii).processName);
if (process.get(iiiii).burstTime <= 0) {
process.get(iiiii).completion = count;
System.out.println(" this one
"+process.get(iiiii).processName+""+process.get(iiiii).completion);
}
}
}
}
for (int tt = 0; tt < no_Process; tt++) {
process.get(tt).waitingTime =
process.get(tt).completion - process.get(tt).burstTime1 -
process.get(tt).arrivalTime;
System.out.println(" waiting time of " +
process.get(tt).processName + " = " + process.get(tt).waitingTime);
System.out.println(" completion time of " +
process.get(tt).processName + " = " + process.get(tt).completion);
process.get(tt).turnaroundtime = process.get(tt).completion -
process.get(tt).arrivalTime;
System.out.println(" turnaround time of " +
process.get(tt).processName + " = " + process.get(tt).turnaroundtime);
}
for (int ttt = 0; ttt < no_Process; ttt++) {
totalwaitingTime = +totalwaitingTime +
process.get(ttt).waitingTime;
totalturnaroundtime = totalturnaroundtime +
process.get(ttt).turnaroundtime;
}
averagewaitingTime = totalwaitingTime / no_Process;
averageturnaroundtime = totalturnaroundtime / no_Process;
System.out.println("Average waiting time = " +
averagewaitingTime);
System.out.println("Average turnaround time = " +
averageturnaroundtime);
}
static class process {
String processName;
int arrivalTime;
int burstTime;
int burstTime1;
int completion;
int waitingTime;
int startTime;
double ratio;
double num;
int turnaroundtime;
int priorityvalue;
Scanner input = new Scanner(System.in);
process() {
System.out.print("Enter the name of the
process = ");
processName = input.next();
System.out.print("Enter the Arrival time of
the process = ");
arrivalTime = input.nextInt();
System.out.print("Enter the Burst time of
the process = ");
burstTime = input.nextInt();
System.out.print("Enter the pririty value of
the process = ");
priorityvalue = input.nextInt();
System.out.println();
}
}
}
Non-Preemptive
Source Code
package osfinals;
import java.util.Scanner;
public class Nonpreemptive{
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int x,n,p[],pp[],bt[],w[],t[],i;
float awt = 0;
float atat = 0;
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];
}
//Displaying the process
System.out.print("\n\nProcess \t Burst Time \t Wait
Time \t Turn Around Time Priority \n");
for(i=0;i<n;i++){
System.out.print("\n "+p[i]+"\t\t "+bt[i]
+"\t\t "+w[i]+"\t\t "+t[i]+"\t\t "+pp[i]+"\n");
awt/=n;
atat/=n;
System.out.print("\n Average Wait Time : "+awt);
System.out.print("\n Average Turn Around Time :
"+atat);
}
}
}
NPP Flowchart
Start
int pid [ ]
int at [ ]
int bt [ ]
int ct [ ]
int ta [ ]
int wt [ ]
int f [ ]
int st = 0
int tot = 0
float avg wt = 0
float avg ta = 0
i=0
i<=n i=i+2
print enter process for arrival time
print enter process for burst time
pid a [ i ] = i + 1
f[i]=0
A
c = n min = 999
i=0
tot = = 0
i ++ i<n
at [ i ] < = st &&
f [ i ] == 0 &&
bt [ i ] < min
min = bt [ i ]
c=1
c==n
c++
ct [ c ] = st + bt [ c ];
st [ ] = bt [ c ]
ta [ c ] = ct [ c ] – at [ c ];
wt [ c ] = ta [ c ] – bt [ c ];
f[c]=1
tot ++
Print pi arrival burst complete turn
waiting
i=0
A
i<n i=0
avg wt = wt [ i ]
avg tat = ta [ i ]
Print pi [ i ] + at[ i ] + bt [ i ] + ct [ i ] + ta [ i ] + wt [ i ]
Display “ average turn around time is :’’ + avg ta /n
“ average wt is “ + avg wt /n
Stop
Round Robin
Source Code
package osfinals;
public static void roundRobin(String p[], int a[],
int b[], int n){
// result of average times
int res = 0;
int resc = 0;
// for sequence storage
String seq = new String();
// copy the burst array and arrival array
// for not effecting the actual array
int res_b[] = new int[b.length];
int res_a[] = new int[a.length];
for (int i = 0; i < res_b.length; i++) {
res_b[i] = b[i];
res_a[i] = a[i];
// critical time of system
int t = 0;
// for store the waiting time
int w[] = new int[p.length];
// for store the Completion time
int comp[] = new int[p.length];
while (true) {
boolean flag = true;
for (int i = 0; i < p.length; i++) {
// these condition for if
// arrival is not on zero
// check that if there come before qtime
if (res_a[i] <= t) {
if (res_a[i] <= n) {
if (res_b[i] > 0) {
flag = false;
if (res_b[i] > n) {
// make decrease the b time
t = t + n;
res_b[i] = res_b[i] - n;
res_a[i] = res_a[i] + n;
seq += "->" + p[i];
else {
// for last time
t = t + res_b[i];
// store comp time
comp[i] = t - a[i];
// store wait time
w[i] = t - b[i] - a[i];
res_b[i] = 0;
// add sequence
seq += "->" + p[i];
else if (res_a[i] > n) {
// is any have less arrival time
// the coming process then execute them
for (int j = 0; j < p.length; j++) {
// compare
if (res_a[j] < res_a[i]) {
if (res_b[j] > 0) {
flag = false;
if (res_b[j] > n){
t = t + n;
res_b[j] = res_b[j] - n;
res_a[j] = res_a[j] + n;
seq += "->" + p[j];
else {
t = t + res_b[j];
comp[j] = t - a[j];
w[j] = t - b[j] - a[j];
res_b[j] = 0;
seq += "->" + p[j];
}
}
// now the previous porcess according to
// ith is process
if (res_b[i] > 0) {
flag = false;
// Check for greaters
if (res_b[i] > n) {
t = t + n;
res_b[i] = res_b[i] - n;
res_a[i] = res_a[i] + n;
seq += "->" + p[i];
else {
t = t + res_b[i];
comp[i] = t - a[i];
w[i] = t - b[i] - a[i];
res_b[i] = 0;
seq += "->" + p[i];
// if no process is come on thse critical
else if (res_a[i] > t) {
t++;
i--;
}
// for exit the while loop
if (flag) {
break;
System.out.println("name ctime wtime");
for (int i = 0; i < p.length; i++) {
System.out.println(" " + p[i] + " " + comp[i]
+ " " + w[i]);
res = res + w[i];
resc = resc + comp[i];
System.out.println("Average waiting time is "
+ (float)res / p.length);
System.out.println("Average compilation time is "
+ (float)resc / p.length);
System.out.println("Sequence is like that " + seq);
// Driver Code
public static void main(String args[])
// name of the process
String name[] = { "p1", "p2", "p3", "p4" };
// arrival for every process
int arrivaltime[] = { 0, 1, 2, 3 };
// burst time for every process
int bursttime[] = { 10, 4, 5, 3 };
// quantum time of each process
int q = 3;
// cal the function for output
roundRobin(name, arrivaltime, bursttime, q);