0% found this document useful (0 votes)
45 views23 pages

LP

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views23 pages

LP

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

PASS ONE ASSEMBLER

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

public class PassOne {


int lc = 0, libtab_ptr = 0, pooltab_ptr = 0;
int symIndex = 0, litIndex = 0;
LinkedHashMap<String, TableRow> SYMTAB;
ArrayList<TableRow> LITTAB;
ArrayList<Integer> POOLTAB;
private BufferedReader br;

public PassOne() {
SYMTAB = new LinkedHashMap<>();
LITTAB = new ArrayList<>();
POOLTAB = new ArrayList<>();
POOLTAB.add(0);
}

public static void main(String[] args) {


PassOne one = new PassOne();
try {
one.parseFile();
} catch (Exception e) {
System.out.println("Error: " + e);
}
}

public void parseFile() throws Exception {


br = new BufferedReader(new FileReader("sample.asm"));
BufferedWriter bw = new BufferedWriter(new FileWriter("IC.txt"));
INSTtable lookup = new INSTtable();
String line;

while ((line = br.readLine()) != null) {


if (line.trim().isEmpty()) continue;

String[] parts = line.trim().split("\\s+");


if (parts.length < 2) continue;

String label = parts[0];


String instruction = parts[1];

if (!label.equals("-")) {
if (!SYMTAB.containsKey(label)) {
SYMTAB.put(label, new TableRow(label, lc, ++symIndex));
} else {
SYMTAB.get(label).setAddess(lc);
}
}
String code = "";

switch (instruction) {
case "START":
lc = Integer.parseInt(parts[2].replace("'", ""));
code = "(AD,01)\t(C," + lc + ")";
break;

case "ORIGIN":
lc = expr(parts[2]);
code = "(AD,03)\t(C," + lc + ")";
break;

case "EQU":
int loc = expr(parts[2]);
code = "(AD,04)\t(C," + loc + ")";
SYMTAB.put(parts[0], new TableRow(parts[0], loc, ++symIndex));
break;

case "LTORG":
case "END":
int ptr = POOLTAB.get(pooltab_ptr);
for (int j = ptr; j < libtab_ptr; j++) {
lc++;
LITTAB.get(j).setAddess(lc);
bw.write("(DL,01)\t(C," + LITTAB.get(j).getSymbol() + ")\n");
}
pooltab_ptr++;
POOLTAB.add(libtab_ptr);
if (instruction.equals("END")) {
code = "(AD,02)";
}
break;

case "DS":
int size = Integer.parseInt(parts[2].replace("'", ""));
code = "(DL,02)\t(C," + size + ")";
lc += size;
break;

case "DC":
int val = Integer.parseInt(parts[2].replace("'", ""));
code = "(DL,01)\t(C," + val + ")";
lc++;
break;

default:
if ("IS".equals(lookup.getType(instruction))) {
code = "(IS," + lookup.getCode(instruction) + ")\t";
StringBuilder operands = new StringBuilder();
for (int j = 2; j < parts.length; j++) {
String op = parts[j].replace(",", "");
String type = lookup.getType(op);

if ("RG".equals(type)) {
operands.append(lookup.getCode(op)).append("\t");
} else if (op.startsWith("=")) {
String literal = op.replace("=", "").replace("'", "");
boolean exists = false;
for (TableRow row : LITTAB) {
if (row.getSymbol().equals(literal)) {
exists = true;
operands.append("(L,").append(row.getIndex()).append(")");
break;
}
}
if (!exists) {
LITTAB.add(new TableRow(literal, -1, ++litIndex));
operands.append("(L,").append(litIndex).append(")");
libtab_ptr++;
}
} else {
if (!SYMTAB.containsKey(op)) {
SYMTAB.put(op, new TableRow(op, -1, ++symIndex));
}
operands.append("(S,").append(SYMTAB.get(op).getIndex()).append(")");
}
}

code += operands.toString();
lc++;
}
break;
}

if (!code.isEmpty()) bw.write(code + "\n");


}

br.close();
bw.close();
printTables();
System.out.println("Intermediate code (IC.txt) generated.");
System.out.println("SYMTAB, LITTAB, POOLTAB written.");

int expr(String expr) {


expr = expr.replace("'", "");
if (expr.contains("+")) {
String[] parts = expr.split("\\+");
return SYMTAB.get(parts[0]).getAddess() + Integer.parseInt(parts[1]);
} else if (expr.contains("-")) {
String[] parts = expr.split("\\-");
return SYMTAB.get(parts[0]).getAddess() - Integer.parseInt(parts[1]);
} else {
return Integer.parseInt(expr);
}
}

void printTables() throws IOException {


BufferedWriter symOut = new BufferedWriter(new FileWriter("SYMTAB.txt"));
BufferedWriter litOut = new BufferedWriter(new FileWriter("LITTAB.txt"));
BufferedWriter poolOut = new BufferedWriter(new FileWriter("POOLTAB.txt"));

for (TableRow row : SYMTAB.values()) {


symOut.write(row.getIndex() + "\t" + row.getSymbol() + "\t" + row.getAddess() + "\n");
}

for (TableRow row : LITTAB) {


litOut.write(row.getIndex() + "\t" + row.getSymbol() + "\t" + row.getAddess() + "\n");
}

for (int i = 0; i < POOLTAB.size(); i++) {


poolOut.write((i + 1) + "\t" + POOLTAB.get(i) + "\n");
}

symOut.close();
litOut.close();
poolOut.close();
}
}
PASS TWO ASSEMBLER
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;

public class Pass2 {


private static final String INTERMEDIATE_CODE_FILE = "/home/student/IC.txt";
private static final String SYM_TAB_FILE = "/home/student/SYMTAB.txt";
private static final String LITERALS_FILE = "/home/student/LITTAB.txt";
private static final String OUTPUT_FILE = "/home/student/Pass2.txt";

public static void main(String[] args) {


HashMap<Integer, String> symSymbol = new HashMap<>();
HashMap<Integer, String> litAddr = new HashMap<>();

try (
BufferedReader b1 = new BufferedReader(new
FileReader(INTERMEDIATE_CODE_FILE));
BufferedReader b2 = new BufferedReader(new FileReader(SYM_TAB_FILE));
BufferedReader b3 = new BufferedReader(new FileReader(LITERALS_FILE));
FileWriter f1 = new FileWriter(OUTPUT_FILE)
){
String line;

// Read symbol table: format assumed [index] [address]


while ((line = b2.readLine()) != null) {
String[] words = line.trim().split("\\s+");
if (words.length >= 2) {
int index = Integer.parseInt(words[0]);
symSymbol.put(index, words[1].trim());
}
}

// Read literals: format assumed [literal] [address]


int litIndex = 1;
while ((line = b3.readLine()) != null) {
String[] words = line.trim().split("\\s+");
if (words.length >= 2) {
litAddr.put(litIndex++, words[1].trim());
}
}

// Process intermediate code


while ((line = b1.readLine()) != null) {
line = line.trim();
if (line.isEmpty()) continue;

String[] parts = line.split("\\s+");


StringBuilder output = new StringBuilder("+ ");
for (String part : parts) {
part = part.trim();

if (part.startsWith("(IS,")) {
String opcode = part.substring(4, part.length() - 1);
output.append(opcode).append(" ");
} else if (part.matches("\\d+")) {
output.append(part).append(" ");
} else if (part.startsWith("(R,")) {
String reg = part.substring(3, part.length() - 1);
output.append(reg).append(" ");
} else if (part.startsWith("(S,")) {
int index = Integer.parseInt(part.substring(3, part.length() - 1));
output.append(symSymbol.getOrDefault(index, "000")).append(" ");
} else if (part.startsWith("(L,")) {
int index = Integer.parseInt(part.substring(3, part.length() - 1));
output.append(litAddr.getOrDefault(index, "000")).append(" ");
} else if (part.startsWith("(C,")) {
String constant = part.substring(3, part.length() - 1);
output.append(constant).append(" ");
} else if (part.contains("+")) {
// Handles expressions like (S,1)+1
String[] expr = part.split("\\+");
String symbolPart = expr[0].trim();
String offsetPart = expr[1].trim();

if (symbolPart.startsWith("(S,") && symbolPart.endsWith(")")) {


int index = Integer.parseInt(symbolPart.substring(3, symbolPart.length() - 1));
int base = Integer.parseInt(symSymbol.getOrDefault(index, "0"));
int offset = Integer.parseInt(offsetPart);
output.append(base + offset).append(" ");
}
}
}

f1.write(output.toString().trim() + "\n");
}

} catch (IOException e) {
System.err.println("Error occurred while processing files: " + e.getMessage());
}
}
}
MACRO PASS ONE
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;

public class MacroP1 {

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


BufferedReader br=new BufferedReader(new FileReader("macro_input.asm"));

FileWriter mnt=new FileWriter("mnt.txt");


FileWriter mdt=new FileWriter("mdt.txt");
FileWriter kpdt=new FileWriter("kpdt.txt");
FileWriter pnt=new FileWriter("pntab.txt");
FileWriter ir=new FileWriter("intermediate.txt");
LinkedHashMap<String, Integer> pntab=new LinkedHashMap<>();
String line;
String Macroname = null;
int mdtp=1,kpdtp=0,paramNo=1,pp=0,kp=0,flag=0;
while((line=br.readLine())!=null)
{

String parts[]=line.split("\\s+");
if(parts[0].equalsIgnoreCase("MACRO"))
{
flag=1;
line=br.readLine();
parts=line.split("\\s+");
Macroname=parts[0];
if(parts.length<=1)
{
mnt.write(parts[0]+"\t"+pp+"\t"+kp+"\t"+mdtp+"\t"+(kp==0?
kpdtp:(kpdtp+1))+"\n");
continue;
}
for(int i=1;i<parts.length;i++) //processing of parameters
{
parts[i]=parts[i].replaceAll("[&,]", "");
//System.out.println(parts[i]);
if(parts[i].contains("="))
{
++kp;
String keywordParam[]=parts[i].split("=");
pntab.put(keywordParam[0], paramNo++);
if(keywordParam.length==2)
{
kpdt.write(keywordParam[0]+"\
t"+keywordParam[1]+"\n");
}
else
{
kpdt.write(keywordParam[0]+"\t-\n");
}
}
else
{
pntab.put(parts[i], paramNo++);
pp++;
}
}
mnt.write(parts[0]+"\t"+pp+"\t"+kp+"\t"+mdtp+"\t"+(kp==0?kpdtp:
(kpdtp+1))+"\n");
kpdtp=kpdtp+kp;
//System.out.println("KP="+kp);

}
else if(parts[0].equalsIgnoreCase("MEND"))
{
mdt.write(line+"\n");
flag=kp=pp=0;
mdtp++;
paramNo=1;
pnt.write(Macroname+":\t");
Iterator<String> itr=pntab.keySet().iterator();
while(itr.hasNext())
{
pnt.write(itr.next()+"\t");
}
pnt.write("\n");
pntab.clear();
}
else if(flag==1)
{
for(int i=0;i<parts.length;i++)
{
if(parts[i].contains("&"))
{
parts[i]=parts[i].replaceAll("[&,]", "");
mdt.write("(P,"+pntab.get(parts[i])+")\t");
}
else
{
mdt.write(parts[i]+"\t");
}
}
mdt.write("\n");
mdtp++;
}
else
{
ir.write(line+"\n");
}
}
br.close();
mdt.close();
mnt.close();
ir.close();
pnt.close();
kpdt.close();
System.out.println("MAcro PAss1 Processing done. :)");
}

}
MACRO PASS TWO
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Vector;

public class MacroP2 {

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


BufferedReader irb=new BufferedReader(new FileReader("intermediate.txt"));
BufferedReader mdtb=new BufferedReader(new FileReader("mdt.txt"));
BufferedReader kpdtb=new BufferedReader(new FileReader("kpdt.txt"));
BufferedReader mntb=new BufferedReader(new FileReader("mnt.txt"));

FileWriter fr=new FileWriter("pass2.txt");

HashMap<String, MNTEntry> mnt=new HashMap<>();


HashMap<Integer, String> aptab=new HashMap<>();
HashMap<String,Integer> aptabInverse=new HashMap<>();

Vector<String>mdt=new Vector<String>();
Vector<String>kpdt=new Vector<String>();

int pp,kp,mdtp,kpdtp,paramNo;
String line;
while((line=mdtb.readLine())!=null)
{
mdt.addElement(line);
}
while((line=kpdtb.readLine())!=null)
{
kpdt.addElement(line);
}
while((line=mntb.readLine())!=null)
{
String parts[]=line.split("\\s+");
mnt.put(parts[0], new MNTEntry(parts[0], Integer.parseInt(parts[1]),
Integer.parseInt(parts[2]), Integer.parseInt(parts[3]), Integer.parseInt(parts[4])));

while((line=irb.readLine())!=null)
{
String []parts=line.split("\\s+");
if(mnt.containsKey(parts[0]))
{
pp=mnt.get(parts[0]).getPp();
kp=mnt.get(parts[0]).getKp();
kpdtp=mnt.get(parts[0]).getKpdtp();
mdtp=mnt.get(parts[0]).getMdtp();
paramNo=1;
for(int i=0;i<pp;i++)
{
parts[paramNo]=parts[paramNo].replace(",", "");
aptab.put(paramNo, parts[paramNo]);
aptabInverse.put(parts[paramNo], paramNo);
paramNo++;
}
int j=kpdtp-1;
for(int i=0;i<kp;i++)
{
String temp[]=kpdt.get(j).split("\t");
aptab.put(paramNo,temp[1]);
aptabInverse.put(temp[0],paramNo);
j++;
paramNo++;
}

for(int i=pp+1;i<parts.length;i++)
{
parts[i]=parts[i].replace(",", "");
String splits[]=parts[i].split("=");
String name=splits[0].replaceAll("&", "");
aptab.put(aptabInverse.get(name),splits[1]);
}
int i=mdtp-1;
while(!mdt.get(i).equalsIgnoreCase("MEND"))
{
String splits[]=mdt.get(i).split("\\s+");
fr.write("+");
for(int k=0;k<splits.length;k++)
{
if(splits[k].contains("(P,"))
{
splits[k]=splits[k].replaceAll("[^0-9]", "");//not
containing number
String
value=aptab.get(Integer.parseInt(splits[k]));
fr.write(value+"\t");
}
else
{
fr.write(splits[k]+"\t");
}
}
fr.write("\n");
i++;
}

aptab.clear();
aptabInverse.clear();
}
else
{
fr.write(line+"\n");
}

fr.close();
mntb.close();
mdtb.close();
kpdtb.close();
irb.close();
}
}
MATHAPP
Import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MathApp {


public static void main(String[] args) {
JFrame frame = new JFrame("Math Operations");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 150);

JPanel panel = new JPanel();


frame.add(panel);

JLabel resultLabel = new JLabel("Result: ");


JTextField num1Field = new JTextField(10);
JTextField num2Field = new JTextField(10);
JButton addButton = new JButton("Add");
JButton subtractButton = new JButton("Subtract");

panel.add(new JLabel("Number 1:"));


panel.add(num1Field);
panel.add(new JLabel("Number 2:"));
panel.add(num2Field);
panel.add(addButton);
panel.add(subtractButton);
panel.add(resultLabel);

addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
double num1 = Double.parseDouble(num1Field.getText());
double num2 = Double.parseDouble(num2Field.getText());
double result = MathOperation.add(num1, num2);
resultLabel.setText("Result: " + result);
} catch (NumberFormatException ex) {
resultLabel.setText("Invalid input.");
}
}
});

subtractButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
double num1 = Double.parseDouble(num1Field.getText());
double num2 = Double.parseDouble(num2Field.getText());
double result = MathOperation.subtract(num1, num2);
resultLabel.setText("Result: " + result);
} catch (NumberFormatException ex) {
resultLabel.setText("Invalid input.");
}
}
});

frame.setVisible(true);
}
}
FCFS
import java.io.*;
import java.util.Scanner;
public class FCFS
{
public static void main(String args[])
{
int i,no_p,burst_time[],TT[],WT[];
float avg_wait=0,avg_TT=0;
burst_time=new int[50];
TT=new int[50];
WT=new int[50];
WT[0]=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter the number of process: ");
no_p=s.nextInt();
System.out.println("\nEnter Burst Time for processes:");
for(i=0;i<no_p;i++)
{
System.out.print("\tP"+(i+1)+": ");
burst_time[i]=s.nextInt();
}

for(i=1;i<no_p;i++)
{
WT[i]=WT[i-1]+burst_time[i-1];
avg_wait+=WT[i];
}
avg_wait/=no_p;

for(i=0;i<no_p;i++)
{
TT[i]=WT[i]+burst_time[i];
avg_TT+=TT[i];
}
avg_TT/=no_p;
System.out.println("\
n****************************************************************");
System.out.println("\tProcesses:");

System.out.println("****************************************************************
");
System.out.println(" Process\tBurst Time\tWaiting Time\tTurn Around Time");
for(i=0;i<no_p;i++)
{
System.out.println("\tP"+(i+1)+"\t "+burst_time[i]+"\t\t "+WT[i]+"\t\t
"+TT[i]);

}
System.out.println("\n----------------------------------------------------------------");
System.out.println("\nAverage waiting time : "+avg_wait);
System.out.println("\nAverage Turn Around time : "+avg_TT+"\n");
}
}
SJF

import java.util.*;

public class SJF {


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 at[] = new int[n]; // at means arrival time
int bt[] = new int[n]; // bt means burst time
int ct[] = new int[n]; // ct means complete time
int ta[] = new int[n]; // ta means turn around time
int wt[] = new int[n]; //wt means waiting time
int f[] = new int[n]; // f means it is flag it checks process is completed or not
int st=0, tot=0;
float avgwt=0, avgta=0;

for(int i=0;i<n;i++)
{
System.out.println ("enter process " + (i+1) + " arrival time:");
at[i] = sc.nextInt();
System.out.println ("enter process " + (i+1) + " brust time:");
bt[i] = sc.nextInt();
pid[i] = i+1;
f[i] = 0;
}
boolean a = true;
while(true)
{
int c=n, min=999;
if (tot == n) // total no of process = completed process loop will be terminated
break;
for (int i=0; i<n; i++)
{
/*
* If i'th process arrival time <= system time and its flag=0 and burst<min
* That process will be executed first
*/
if ((at[i] <= st) && (f[i] == 0) && (bt[i]<min))
{
min=bt[i];
c=i;
}
}
/* If c==n means c value can not updated because no process arrival time< system time so we
increase the system time */
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++;
}
}
System.out.println("\npid arrival brust 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 ("\naverage tat is "+ (float)(avgta/n));
System.out.println ("average wt is "+ (float)(avgwt/n));
sc.close();
}
}
ROUND
import java.io.*;

class Round {
public static void main(String args[]) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int i, j, k, q, sum = 0;

System.out.println("Enter number of processes:");


int n = Integer.parseInt(in.readLine());

int bt[] = new int[n];


int wt[] = new int[n];
int tat[] = new int[n];
int a[] = new int[n];

System.out.println("Enter Burst Time:");


for (i = 0; i < n; i++) {
System.out.println("Enter Burst Time for process " + (i + 1) + ":");
bt[i] = Integer.parseInt(in.readLine());
}

System.out.println("Enter Time Quantum:");


q = Integer.parseInt(in.readLine());

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


a[i] = bt[i];
}

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


wt[i] = 0;
}

do {
for (i = 0; i < n; i++) {
if (bt[i] > q) {
bt[i] -= q;
for (j = 0; j < n; j++) {
if ((j != i) && (bt[j] != 0)) {
wt[j] += q;
}
}
} else if (bt[i] > 0) {
for (j = 0; j < n; j++) {
if ((j != i) && (bt[j] != 0)) {
wt[j] += bt[i];
}
}
bt[i] = 0;
}
}
sum = 0;
for (k = 0; k < n; k++) {
sum += bt[k];
}
} while (sum != 0);

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


tat[i] = wt[i] + a[i];
}

System.out.println("Process\t\tBT\tWT\tTAT");
for (i = 0; i < n; i++) {
System.out.println("Process " + (i + 1) + "\t" + a[i] + "\t" + wt[i] + "\t" + tat[i]);
}

float avg_wt = 0;
float avg_tat = 0;
for (j = 0; j < n; j++) {
avg_wt += wt[j];
}
for (j = 0; j < n; j++) {
avg_tat += tat[j];
}

System.out.println("Average Waiting Time: " + (avg_wt / n));


System.out.println("Average Turnaround Time: " + (avg_tat / n));
}
}
PRIORITY SCHEDULING
import java.util.Scanner;

class PriorityScheduling {

public static void main(String[] args) {

System.out.println("*** Priority Scheduling (Preemptive) ***");


System.out.print("Enter Number of Process: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();

// All arrays are of size n + 1 to prevent out-of-bounds


int process[] = new int[n + 1];
int arrivaltime[] = new int[n + 1];
int burstTime[] = new int[n + 1];
int completionTime[] = new int[n + 1];
int priority[] = new int[n + 1];
int TAT[] = new int[n + 1];
int waitingTime[] = new int[n + 1];
int burstTimecopy[] = new int[n + 1];

int min = 0, count = 0;


int temp, time = 0, end;
double avgWT = 0, avgTAT = 0;

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


process[i] = (i + 1);
System.out.println("");
System.out.print("Enter Arrival Time for processor " + (i + 1) + ": ");
arrivaltime[i] = sc.nextInt();
System.out.print("Enter Burst Time for processor " + (i + 1) + " : ");
burstTime[i] = sc.nextInt();
System.out.print("Enter Priority for " + (i + 1) + " process: ");
priority[i] = sc.nextInt();
}

// Sort based on arrival time and priority


for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (arrivaltime[i] > arrivaltime[j] ||
(arrivaltime[i] == arrivaltime[j] && priority[j] > priority[i])) {

// Swap process IDs


temp = process[i];
process[i] = process[j];
process[j] = temp;

// Swap arrival time


temp = arrivaltime[i];
arrivaltime[i] = arrivaltime[j];
arrivaltime[j] = temp;
// Swap burst time
temp = burstTime[i];
burstTime[i] = burstTime[j];
burstTime[j] = temp;

// Swap priority
temp = priority[i];
priority[i] = priority[j];
priority[j] = temp;
}
}
}

System.arraycopy(burstTime, 0, burstTimecopy, 0, n);


priority[n] = 999; // Sentinel value

for (time = 0; count != n; time++) {


min = n;
for (int i = 0; i < n; i++) {
if (arrivaltime[i] <= time && priority[i] < priority[min] && burstTime[i] > 0) {
min = i;
}
}

burstTime[min]--;

if (burstTime[min] == 0) {
count++;
end = time + 1;
completionTime[min] = end;
waitingTime[min] = end - arrivaltime[min] - burstTimecopy[min];
TAT[min] = end - arrivaltime[min];
}
}

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


avgTAT += TAT[i];
avgWT += waitingTime[i];
}

System.out.println("\n*** Priority Scheduling (Preemptive) ***");


System.out.println("Processor\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\
tWaiting Time");

System.out.println("--------------------------------------------------------------------------------------------");

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


System.out.println("P" + process[i] + "\t\t" +
arrivaltime[i] + "ms\t\t" +
burstTimecopy[i] + "ms\t\t" +
completionTime[i] + "ms\t\t" +
TAT[i] + "ms\t\t" +
waitingTime[i] + "ms");
}

avgWT /= n;
avgTAT /= n;

System.out.println("\nAverage Waiting Time: " + avgWT + "ms");


System.out.println("Average Turnaround Time: " + avgTAT + "ms");

sc.close();
}
}

You might also like