0% found this document useful (0 votes)
7 views

SPOS

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)
7 views

SPOS

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/ 26

Practical No.

Title :
Design suitable data structures and implement Pass-I and Pass-II of a two-pass assembler for
pseudo-machine. Implementation should consist of a few instructions from each category and a few
assembler directives. The output of Pass-I (intermediate code file and symbol table) should be input
for Pass-II.

Code for Pass-I:

assp.java

package practical;

import java.io.*;
import java.util.*;

class assp
{
public static void main(String args[])throws Exception
{
int loc=0,loc1=0;
int j=0,k,sym=0,lit=0,v=0,w=0;

FileReader f1=new FileReader("in.txt");


BufferedReader b1=new BufferedReader(f1);

FileWriter f3=new FileWriter("symbol.txt");


BufferedWriter b3=new BufferedWriter(f3);

FileWriter f4=new FileWriter("intermediate.txt");


BufferedWriter b4=new BufferedWriter(f4);

FileWriter f5=new FileWriter("literal.txt");


BufferedWriter b5=new BufferedWriter(f5);

StringBuffer sb = new StringBuffer();


String s,s3,s4,tmp,sd,sl;
String s1[]=new String[4];
String s2[]=new String[4];
String s5[][]=new String[4][2];
while((s=b1.readLine())!=null)
{
int flag=0;
int m=0;
StringTokenizer st=new StringTokenizer(s);
while(st.hasMoreTokens())
{
s1[m]=st.nextToken();
m++;
}
if(s1[1].equals("START"))
{
sample3b4.write("AD 01");
b4.write(" C "+s1[2]);
loc=Integer.parseInt(s1[2]);
b4.newLine();
}
else if(s1[1].equals("END"))
{
for(int i=1;i<=lit;i++)
{
b4.write(loc+" ");
b4.write("AD 02");

b4.write(" C "+i);
loc=loc+1;
b4.newLine();
}
}
else
{
b4.write(loc+" ");
if(s1[0].equals("-")!=true)
{
sym++;
b3.write(" "+s1[0]);
b3.write(" "+loc+" ");
}
FileReader f2=new FileReader("op.txt");
BufferedReader b2=new BufferedReader(f2);
while((sl=b2.readLine())!=null)
{
j=0;
StringTokenizer se=new StringTokenizer(sl);
while(se.hasMoreTokens())
{
s2[j]=se.nextToken();
j++;
}
if(s1[1].equals(s2[0])==true)
{
for(int a=1;a<3;a++)
{
b4.write(" ");
b4.write(" "+s2[a]);
}
}
if(s1[2].equals(s2[0])==true)
{
for(int a=1;a<2;a++)
{
b4.write(" ");
b4.write(" "+s2[a]);
}
}

}
if(s1[3].contains("="))
{
int i;flag=0;
for( i=0;i<lit;i++)
{
if(s5[i][1].equals(s1[3]))
{flag=1;
break;}
}
if(flag!=1)
{
lit++;
s5[v][0]=Integer.toString(lit);
s5[v][1]=s1[3];
v++;
b5.newLine();
b5.write(" "+lit);
b5.write(" "+s1[3]);
//b6.write(" "+loc1+" ");
b4.write(" ");
b4.write(" "+"L "+lit);
}
else
{
b4.write(" ");
b4.write(" "+"L "+s5[i][0]);
}

else if((s1[3]).matches("[a-zA-Z]"))
{
//b4.write(" ");
b4.write(" "+"S "+sym);

}
loc=loc+1;

b4.newLine();

b4.newLine();
b3.newLine();
}
}
b4.close();
b3.close();
b5.close();

in.txt
- START 100
L1 MOVER AREG =3
- MOVEM BREG X
- SUB AREG =1
- LTORG
- MOVEM AREG Y
- BC any L1
- ADD CREG,4
X DC 6
Y DS 2
- END

op.txt
STOP IS 01
ADD IS 02
SUB IS 03
MUL IS 04
MOVER IS 05
MOVEM IS 06
START AD 01
END AD 02
ORIGIN AD 03
LTORG AD 04
AREG R1
BREG R2
CREG R3
DREG R4
DC DL 01
DS DL 02

Output:

intermediate.txt

AD 01 C 100
100 IS 05 R1 L 1

101 IS 06 R2 S 1

102 IS 03 R1 L 2

103 AD 04 R1 L 2

104 IS 06 R1 S 1

105

106 IS 02

107 DL 01

108 DL 02

109 AD 02 C 1
110 AD 02 C 2

literal.txt

1 =3
2 =1

symbol.txt
L1 100

X 107
Y 108

}
}

Code for Pass-II


assp.java

package pass2;
import java.io.*;
import java.util.*;

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

String s;
String s1[]=new String[70];
String s2[]=new String[70];
String s3[]=new String[70];

FileWriter f1=new FileWriter("output3.txt");


BufferedWriter b1=new BufferedWriter(f1);

FileReader f2=new FileReader("intermedi1.txt");


BufferedReader b2=new BufferedReader(f2);

FileReader f3=new FileReader("symbol2.txt");


BufferedReader b3=new BufferedReader(f3);

FileReader f4=new FileReader("literal2.txt");


BufferedReader b4=new BufferedReader(f4);

int m=0;
while((s=b2.readLine())!=null)
{

StringTokenizer st=new StringTokenizer(s);


while(st.hasMoreTokens())
{
s1[m]=st.nextToken();
m++;
}
}
int m1=0;
while((s=b3.readLine())!=null)
{

StringTokenizer st=new StringTokenizer(s);


while(st.hasMoreTokens())
{
s2[m1]=st.nextToken();
m1++;
}
}
int m2=0;
while((s=b4.readLine())!=null)
{

StringTokenizer st=new StringTokenizer(s);


while(st.hasMoreTokens())
{
s3[m2]=st.nextToken();
m2++;
}
}

for(int i=0;i<m;i++)
{
if("AD".equals(s1[i]) && "01".equals(s1[i+1]))
{
b1.write("");
}
if("R1".equals(s1[i])||"R2".equals(s1[i])||"R3".equals(s1[i])||"R4".equals(s1[i]))
{
b1.write("\t"+s1[i]+"\n");
b1.write(" ");

}
if("IS".equals(s1[i]))
{
b1.write(s1[i-1]+"\t");
b1.write(s1[i+1]+"\n");
}

if("L".equals(s1[i]))
{

for(int j=0;j<m2;j++)
{
if(s1[i+1].equals(s3[j]))
{
b1.write("\t");
b1.write(s3[j+2]+"\n");
}

}
}

if("S".equals(s1[i]))
{

for(int j=0;j<m1;j++)
{
if(s1[i+1].equals(s2[j]))
{
b1.write("\t");
b1.write(s2[j+2]+"\n");
}

}
}

if("DL".equals(s1[i])&&"01".equals(s1[i+1]))
{
b1.write(s1[i-1]+"\t");
b1.write("-"+"\t");
b1.write("-");
b1.write("\t");
b1.write("00"+s1[i+3]+"\n");

}
if(s1[i].equals("AD") && s1[i+1].equals("02"))
{
b1.write(s1[i-1]+"\t");
b1.write("-"+"\t");
b1.write("-");
b1.write("\t");
b1.write("00"+s1[i+3]+"\n");
}

b1.close();
b2.close();
b3.close();
b4.close();

intermedi1.txt

AD 01 C 100
100 IS 05 R1 L 1

101 IS 06 R2 S 1

102 IS 03 R1 L 2

103 AD 04 R1 L 2

104 IS 06 R1 S 1

105

106 IS 02

107 DL 01

108 DL 02

109 AD 02 C 1

110 AD 02 C 2

literal2.txt

1 =7
2 =2

symbol2.txt

L1 100
X 107
Y 108
}

output3.txt
100 05
R1
2
101 06
R2
102 03
R1
null
R1
null
104 06
R1
106 02
107 - - 00DL
109 - - 001
110 - - 002

}100 05
R1
2
101 06
R2
102 03
R1
null
R1
null

Practical No. 2
Title: Design suitable data structures and implement Pass-I and Pass-II of a two pass-macro macro-
processor. The output of Pass-I (MNT, MDT and intermediate code file without any macro
definitions) should be input for Pass-II.

// PASS-1

import java.util.*;
import java.io.*;

class MntTuple {
String name;
int index;

MntTuple(String s, int i) {
name = s;
index = i;

public String toString() {


return("[" + name + ", " + index + "]");
}
}

class m1 {
static List<MntTuple> mnt;
static List<String> mdt;
static int mntc;
static int mdtc;
static int mdtp;
static BufferedReader input;
static List<List <String>> ala;
static Map<String, Integer> ala_macro_binding;

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


initializeTables();
System.out.println("===== PASS 1 =====\n");
pass1();

static void pass1() throws Exception {


String s = new String();
input = new BufferedReader(new InputStreamReader(new
FileInputStream("input2.txt")));
PrintWriter output = new PrintWriter(new FileOutputStream("output_pass1.txt"),
true);
while((s = input.readLine()) != null) {
if(s.equalsIgnoreCase("MACRO")) {
processMacroDefinition();
} else {
output.println(s);
}
}
System.out.println("ALA:");
showAla(1);
System.out.println("\nMNT:");
showMnt();
System.out.println("\nMDT:");
showMdt();
}

static void processMacroDefinition() throws Exception {


String s = input.readLine();
String macro_name = s.substring(0, s.indexOf(" "));
mnt.add(new MntTuple(macro_name, mdtc));
mntc++;
pass1Ala(s);
StringTokenizer st = new StringTokenizer(s, " ,", false);
String x = st.nextToken();
for(int i=x.length() ; i<12 ; i++) {
x += " ";
}
String token = new String();
int index;
token = st.nextToken();
x += token;
while(st.hasMoreTokens()) {
token = st.nextToken();
x += "," + token;
}
mdt.add(x);
mdtc++;
addIntoMdt(ala.size()-1);
}

static void pass1Ala(String s) {


StringTokenizer st = new StringTokenizer(s, " ,", false);
String macro_name = st.nextToken();
List<String> l = new ArrayList<>();
int index;
while(st.hasMoreTokens()) {
String x = st.nextToken();
if((index = x.indexOf("=")) != -1) {
x = x.substring(0, index);
}
l.add(x);
}
ala.add(l);
ala_macro_binding.put(macro_name, ala_macro_binding.size());
}

static void addIntoMdt(int ala_number) throws Exception {


String temp = new String();
String s = new String();
List l = ala.get(ala_number);
boolean isFirst;
while(!s.equalsIgnoreCase("MEND")) {
isFirst = true;
s = input.readLine();
String line = new String();
StringTokenizer st = new StringTokenizer(s, " ,", false);
temp = st.nextToken();
for(int i=temp.length() ; i<12 ; i++) {
temp += " ";
}
line += temp;
while(st.hasMoreTokens()) {
temp = st.nextToken();
if(temp.startsWith("&")) {
int x = l.indexOf(temp);
temp = ",#" + x;
isFirst = false;
} else if(!isFirst) {
temp = "," + temp;
}
line += temp;
}
mdt.add(line);
mdtc++;
}
}

static void showAla(int pass) throws Exception {


PrintWriter out = new PrintWriter(new FileOutputStream("out_ala_pass" + pass +
".txt"), true);
for(List l : ala) {
System.out.println(l);
out.println(l);
}
}

static void showMnt() throws Exception {


PrintWriter out = new PrintWriter(new FileOutputStream("out_mnt.txt"), true);
for(MntTuple l : mnt) {
System.out.println(l);
out.println(l);
}
}

static void showMdt() throws Exception {


PrintWriter out = new PrintWriter(new FileOutputStream("out_mdt.txt"), true);
for(String l : mdt) {
System.out.println(l);
out.println(l);
}
}

static void initializeTables() {


mnt = new LinkedList<>();
mdt = new ArrayList<>();
ala = new LinkedList<>();
mntc = 0;
mdtc = 0;
ala_macro_binding = new HashMap<>();
}
}

/* -------- INPUT -----------


MACRO
INCR &X,&Y,&REG=AREG
MOVER &REG,&X
ADD &REG,&Y
MOVEM &REG,&X
MEND
MACRO
DECR &A,&B,&REG=BREG
MOVER &REG,&A
SUB &REG,&B
MOVEM &REG,&A
MEND
START 100
READ N1
READ N2
INCR N1,N2,REG=CREG
DECR N1,N2
STOP

--------- OUTPUT ---------------

C:\Program Files\Java\jdk1.8.0_161\bin>java m1
===== PASS 1 =====

ALA:
[&X, &Y, &REG]
[&A, &B, &REG]

MNT:
[INCR, 0]
[DECR, 5]

MDT:
INCR &X,&Y,&REG=AREG
MOVER ,#2,#0
ADD ,#2,#1
MOVEM ,#2,#0
MEND
DECR &A,&B,&REG=BREG
MOVER ,#2,#0
SUB ,#2,#1
MOVEM ,#2,#0
MEND

*/

//PASS-II
package practical2;
import java.io.*;
import java.util.*;

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

String s;
String s1[]=new String[70];
String s2[]=new String[70];
String s3[]=new String[70];

FileWriter f1=new FileWriter("output3.txt");


BufferedWriter b1=new BufferedWriter(f1);

FileReader f2=new FileReader("intermedi1.txt");


BufferedReader b2=new BufferedReader(f2);

FileReader f3=new FileReader("symbol2.txt");


BufferedReader b3=new BufferedReader(f3);

FileReader f4=new FileReader("literal2.txt");


BufferedReader b4=new BufferedReader(f4);

int m=0;
while((s=b2.readLine())!=null)
{

StringTokenizer st=new StringTokenizer(s);


while(st.hasMoreTokens())
{
s1[m]=st.nextToken();
m++;
}
}
int m1=0;
while((s=b3.readLine())!=null)
{

StringTokenizer st=new StringTokenizer(s);


while(st.hasMoreTokens())
{
s2[m1]=st.nextToken();
m1++;
}
}
int m2=0;
while((s=b4.readLine())!=null)
{

StringTokenizer st=new StringTokenizer(s);


while(st.hasMoreTokens())
{
s3[m2]=st.nextToken();
m2++;
}
}

for(int i=0;i<m;i++)
{
if("AD".equals(s1[i]) && "01".equals(s1[i+1]))
{
b1.write("");
}
if("R1".equals(s1[i])||"R2".equals(s1[i])||"R3".equals(s1[i])||"R4".equals(s1[i]))
{
b1.write("\t"+s1[i]+"\n");
b1.write(" ");

}
if("IS".equals(s1[i]))
{
b1.write(s1[i-1]+"\t");
b1.write(s1[i+1]+"\n");
}

if("L".equals(s1[i]))
{

for(int j=0;j<m2;j++)
{
if(s1[i+1].equals(s3[j]))
{
b1.write("\t");
b1.write(s3[j+2]+"\n");
}

}
}

if("S".equals(s1[i]))
{

for(int j=0;j<m1;j++)
{
if(s1[i+1].equals(s2[j]))
{
b1.write("\t");
b1.write(s2[j+2]+"\n");
}

}
}

if("DL".equals(s1[i])&&"01".equals(s1[i+1]))
{
b1.write(s1[i-1]+"\t");
b1.write("-"+"\t");
b1.write("-");
b1.write("\t");
b1.write("00"+s1[i+3]+"\n");

}
if(s1[i].equals("AD") && s1[i+1].equals("02"))
{
b1.write(s1[i-1]+"\t");
b1.write("-"+"\t");
b1.write("-");
b1.write("\t");
b1.write("00"+s1[i+3]+"\n");
}

b1.close();
b2.close();
b3.close();
b4.close();
}
}

//intermedi1.txt

AD 01 C 100
100 IS 05 R1 L 1

101 IS 06 R2 S 1

102 IS 03 R1 L 2

103 AD 04 R1 L 2

104 IS 06 R1 S 1

105

106 IS 02
107 DL 01

108 DL 02

109 AD 02 C 1

110 AD 02 C 2

//literal2.txt

1 =7
2 =2

//symbol2.txt

L1 100
X 107
Y 108

//output

100 05
R1
2
101 06
R2
102 03
R1
null
R1
null
104 06
R1
106 02
107 - - 00DL
109 - - 001
110 - - 002

104 Practical No. 3


06
R1
Title: Write a program to solve Classical Problems pf Synchronization using Mutex and
Semaphore

Code:

import java.util.*;
import java.util.concurrent.Semaphore;
public class ReaderWriter {
static int mutex=1;
static int database=1;
static int Read_Count=0;
static void Reader() throws Exception
{
while(true)
{
mutex=wait(mutex);
Read_Count=Read_Count+1;
if(Read_Count==1){
database=signal(database);
}
mutex=signal(mutex);
System.out.println(Read_Count+ " User Reading the Data.........");
mutex=wait(mutex);
Read_Count=Read_Count-1;
if(Read_Count==0)
{
database=signal(database);
}
mutex=signal(mutex);
System.out.println("Reading Finished!!!!!!");
break;
}
}
static int wait(int mutex){
while(mutex<=0)
break ;
mutex=mutex-1;
return mutex;
}
static int signal(int database)
{
database=database+1;
return database;
}
static void Writer() throws Exception
{
while(true)
{
database=wait(database);
System.out.println("Writing on the database......");
database=signal(database);
System.out.println("Writing Finished!!!!!.");
break;
}
}
public static void main(String[] args)throws Exception {
Writer();
Reader();
Reader();
}
}

/* *********************** OUTPUT *************************


Writing on the database......
Writing Finished!!!!!.
1 User Reading the Data.........
Reading Finished!!!!!!
1 User Reading the Data.........
Reading Finished!!!!!!
*********************************************************** */
106 02
107 - - 00DL
109 - - 001
110 - - 002

06 Practical No. 4
Title: Write a Java program (using OOP features) to implement following scheduling algorithms:
FCFS , SJF (Preemptive), Priority (Non-Preemptive) and Round Robin (Preemptive)

FCFS
Code:
package algorithms;
import java.util.Scanner;
class Fcfs
{
public static void main(String args[]){
int bst[],process[],wt[],tat[],i,j,n,total=0,pos,temp;
float wait_avg, TAT_avg;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of process: ");
n = s.nextInt();
process = new int[n];
bst = new int[n];
wt = new int[n];
tat = new int[n];
System.out.println("\nEnter CPU time:");
for(i=0;i<n;i++)
{
System.out.print("\nProcess["+(i+1)+"]: ");
bst[i] = s.nextInt();;
process[i]=i+1;
}

System.out.println("\t\t\t**********FCFS Scheduling*********");

wt[0]=0;

for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bst[j];
total+=wt[i];
}

wait_avg=(float)total/n;
total=0;
System.out.println("-----------------------------------------------------------------------");
System.out.println("\nProcess\t\t| Burst Time \t\t|Waiting Time\t\t|Turn Time");
System.out.println("-----------------------------------------------------------------------");
for(i=0;i<n;i++)
{
tat[i]=bst[i]+wt[i];
total+=tat[i];
System.out.println("\np"+process[i]+"\t\t|\t"+bst[i]+"\t\t|\t"+wt[i]+"\t\t|\t"+tat[i]);
System.out.println("-----------------------------------------------------------------------");
}
TAT_avg=(float)total/n;
System.out.println("\n\nAverage Waiting Time: "+wait_avg);
System.out.println("\nAverage Turnaround Time: "+TAT_avg);
}
}
OUTPUT:

Enter number of process: 4

Enter Burst time:

Process[1]: 20

Process[2]: 10

Process[3]: 5

Process[4]: 12

*************** Shortest Job First Scheduling*********


----------------------------------------------------------

Process | Burst Time |Waiting Time |Turnaround Time |


----------------------------------------------------------

p3 | 5 | 0 | 5 |
----------------------------------------------------------

p2 | 10 | 5 | 15 |
----------------------------------------------------------

p4 | 12 | 15 | 27 |
----------------------------------------------------------

p1 | 20 | 27 | 47 |
----------------------------------------------------------

Average Waiting Time: 11.75

Average Turnaround Time: 23.5

SJF(Preemptive):
CODE:
package algorithms;
import java.util.Scanner;
class SJF
{
public static void main(String args[])
{
int burst_time[],process[],waiting_time[],tat[],i,j,n,total=0,pp,temp;
float wait_avg,TAT_avg;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of process: ");
n = s.nextInt();
process = new int[n];
burst_time = new int[n];
waiting_time = new int[n];
tat = new int[n];
System.out.println("\nEnter Burst time:");
for(i=0;i<n;i++)
{
System.out.print("\nProcess["+(i+1)+"]: ");
burst_time[i] = s.nextInt();;
process[i]=i+1;
}
System.out.println("\n \t \t*************** Shortest Job First Scheduling*********");

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

for(j=i+1;j<n;j++)
{
if(burst_time[j]<burst_time[pp])
pp=j;
}
temp=burst_time[i];
burst_time[i]=burst_time[pp];
burst_time[pp]=temp;
temp=process[i];
process[i]=process[pp];
process[pp]=temp;
}

waiting_time[0]=0;

for(i=1;i<n;i++)
{
waiting_time[i]=0;
for(j=0;j<i;j++)
waiting_time[i]+=burst_time[j];
total+=waiting_time[i];
}

wait_avg=(float)total/n;
total=0;
System.out.println("----------------------------------------------------------");
System.out.println("\nProcess\t| Burst Time \t|Waiting Time\t|Turnaround Time |");
System.out.println("----------------------------------------------------------");
for(i=0;i<n;i++)
{
tat[i]=burst_time[i]+waiting_time[i];

total+=tat[i];
System.out.println("\n p"+process[i]+"\t|\t "+burst_time[i]+"\t|\t "+waiting_time[i]+"\t|\t"+tat[i]+"\t|\t ");
System.out.println("----------------------------------------------------------");
}

TAT_avg=(float)total/n;
System.out.println("\n\nAverage Waiting Time: "+wait_avg);
System.out.println("\nAverage Turnaround Time: "+TAT_avg);
}
}

OUTPUT:
Enter number of process: 4

Enter Burst time:

Process[1]: 4

Process[2]: 15

Process[3]: 12

Process[4]: 16

*************** Shortest Job First Scheduling*********


----------------------------------------------------------

Process | Burst Time |Waiting Time |Turnaround Time |


----------------------------------------------------------

p1 | 4 | 0 | 4 |
----------------------------------------------------------

p3 | 12 | 4 | 16 |
----------------------------------------------------------

p2 | 15 | 16 | 31 |
----------------------------------------------------------

p4 | 16 | 31 | 47 |
----------------------------------------------------------

Average Waiting Time: 12.75

Average Turnaround Time: 24.5

ROUND ROBIN (Preemptive):


CODE:

package algorithms;
import java.util.Scanner;
public class rr {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int wtime[],btime[],rtime[],num,quantum,total;
wtime = new int[10];
btime = new int[10];
rtime = new int[10];
System.out.print("Enter number of processes(MAX 10): ");
num = s.nextInt();
System.out.print("Enter burst time");
for(int i=0;i<num;i++) { System.out.print("\nP["+(i+1)+"]: "); btime[i] = s.nextInt(); rtime[i] =
btime[i]; wtime[i]=0; } System.out.print("\n\nEnter quantum: "); quantum = s.nextInt(); int rp =
num; int i=0; int time=0; System.out.print("0"); wtime[0]=0; while(rp!=0) { if(rtime[i]>quantum)
{
rtime[i]=rtime[i]-quantum;
System.out.print(" | P["+(i+1)+"] | ");
time+=quantum;
System.out.print(time);
}
else if(rtime[i]<=quantum && rtime[i]>0)
{time+=rtime[i];
rtime[i]=rtime[i]-rtime[i];
System.out.print(" | P["+(i+1)+"] | ");
rp--;
System.out.print(time);
}
i++;
if(i==num)
{
i=0;
}
}

}
}

OUTPUT:

Enter number of processes(MAX 10): 5


Enter burst time
P[1]: 12

P[2]: 14

P[3]: 15

P[4]: 18

P[5]: 20

Enter quantum: 4
0 | P[1] | 4 | P[2] | 8 | P[3] | 12 | P[4] | 16 | P[5] | 20 | P[1] | 24 | P[2] | 28 | P[3] | 32 | P[4] | 36 | P[5] | 40 | P[1] |
44 | P[2] | 48 | P[3] | 52 | P[4] | 56 | P[5] | 60 | P[2] | 62 | P[3] | 65 | P[4] | 69 | P[5] | 73 | P[4] | 75 | P[5]
Priority(Non-Preemptive):
package algorithms;

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[],awt,atat,i;
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 CPU time---priority \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;
}

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.println("-----------------------------------------------------------------------");
System.out.print("\n\nProcess \t\t |Burst Time \t\t |Wait Time \t\t |Turn Time \n");
System.out.println("-----------------------------------------------------------------------");
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");
System.out.println("-----------------------------------------------------------------------");
awt/=n;
atat/=n;
System.out.print("\n Average Wait Time : "+awt);
System.out.print("\n Average Turn Around Time : "+atat);
}
}

OUTPUT:
Process[1]:3
4
Process[2]:2
1
Process[3]:1
2
Process[4]:3
4
Process[5]:2
1

-----------------------------------------------------------------------
Process |Burst Time |Wait Time |TurnTime
-----------------------------------------------------------------------
1 |3 |0 |3 |4
4 |3 |3 |6 |4
3 |1 |6 |7 |2
2 |2 |7 |9 |1
5 |2 |9 |11 |1
-----------------------------------------------------------------------
Average Wait Time : 5
Average Turn Around Time : 7

You might also like