Professional Documents
Culture Documents
PART B
Grade:
Code:
import java.util.Scanner;
class Priority
{
private int burst;
private int arrival;
private int id;
private int wait;
private int priority;
private int turnaround;
static int time;
void setProc(int i)
{
Scanner sc = new Scanner(System.in);
id=i;
System.out.println("\nEnter Burst Time: ");
burst = sc.nextInt();
System.out.println("\nEnter Arrival Time: ");
arrival = sc.nextInt();
System.out.println("\nEnter Priority: ");
priority = sc.nextInt();
}
static void sortProc(Priority p[],int start,int en)
{
if(start == -1 && en == -1)
SVKM’S NMIMS Deemed-to-be-University
Mukesh Patel School of Technology Management & Engineering
Department of Computer Engineering
Course Code MCNB02002 Program MCA
Semester II Year I
Name of the Faculty Hiral Modi Class
Course Title Operating Systems Academic year 2016-17
{
for(int i=0;i<p.length-1;i++)
for(int j=i+1;j<p.length;j++)
if(p[i].arrival>p[j].arrival)
{
Priority temp=p[i];
p[i]=p[j];
p[j]=temp;
}
time = p[0].arrival;
for(int i=0;i<p.length-1;i++)
for(int j=i+1;j<p.length;j++)
if(p[i].arrival==p[j].arrival && p[i].priority>p[j].priority)
{
Priority temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
else
{
for(int i=start;i<en;i++)
for(int j=i+1;j<=en;j++)
if(p[i].priority>p[j].priority)
{
Priority temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
static void execute(Priority p[])
{
System.out.println("\n\nProcess Arrival Burst Priority Waiting Turnaround
\n");
for(int i=0;i<p.length;i++)
{
p[i].wait = time - p[i].arrival;
p[i].turnaround = p[i].wait + p[i].burst;
SVKM’S NMIMS Deemed-to-be-University
Mukesh Patel School of Technology Management & Engineering
Department of Computer Engineering
Course Code MCNB02002 Program MCA
Semester II Year I
Name of the Faculty Hiral Modi Class
Course Title Operating Systems Academic year 2016-17
System.out.println("P"+p[i].id+"\t\t"+p[i].arrival+"\t"+p[i].burst+"\t"+p[i].priority+"\t"+p[i].wait
+"\t"+p[i].turnaround);
for(int burst=p[i].burst;burst>0;burst--)
++time;
int start=-1,en=-1;
int j=i+1,k=i+1;
for(;k<p.length;k++)
{
if(j==k && p[k].arrival<=time)
{
start=j;
en=j;
}
else if(p[k].arrival<=time)
en=k;
}
if(en != -1 && start != -1)
sortProc(p, start, en);
}
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
do
{
System.out.println("\nEnter 1 for Non-Preemptive Priority and 2 for
Preemptive Priority and anything else to Exit: ");
int ch = sc.nextInt();
int l;
switch(ch)
{
case 1:
System.out.println("Enter Number Of Processes: ");
l = sc.nextInt();
Priority p[] = new Priority[l];
for(int i=0;i<l;i++)
{
System.out.println("Enter Details for Process P"+
(i+1));
SVKM’S NMIMS Deemed-to-be-University
Mukesh Patel School of Technology Management & Engineering
Department of Computer Engineering
Course Code MCNB02002 Program MCA
Semester II Year I
Name of the Faculty Hiral Modi Class
Course Title Operating Systems Academic year 2016-17
import java.util.Scanner;
class PrePriority
{
private int burst;
private int burstRem;
private int arrival;
private int id;
private int wait;
private int priority;
private int waitTemp;
private int turnaround;
static int time;
SVKM’S NMIMS Deemed-to-be-University
Mukesh Patel School of Technology Management & Engineering
Department of Computer Engineering
Course Code MCNB02002 Program MCA
Semester II Year I
Name of the Faculty Hiral Modi Class
Course Title Operating Systems Academic year 2016-17
void setProc(int i)
{
Scanner sc = new Scanner(System.in);
id=i;
System.out.println("\nEnter Burst Time: ");
burst = sc.nextInt();
burstRem=burst;
System.out.println("\nEnter Arrival Time: ");
arrival = sc.nextInt();
waitTemp=arrival;
System.out.println("\nEnter Priority: ");
priority = sc.nextInt();
}
static void sortProc(PrePriority p[],int start,int en)
{
if(start == -1 && en == -1)
{
for(int i=0;i<p.length-1;i++)
for(int j=i+1;j<p.length;j++)
if(p[i].arrival>p[j].arrival)
{
PrePriority temp=p[i];
p[i]=p[j];
p[j]=temp;
}
time = p[0].arrival;
for(int i=0;i<p.length-1;i++)
for(int j=i+1;j<p.length;j++)
if(p[i].arrival==p[j].arrival && p[i].priority>p[j].priority)
{
PrePriority temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
else
{
for(int i=start;i<en;i++)
for(int j=i+1;j<=en;j++)
if(p[i].priority>p[j].priority)
{
SVKM’S NMIMS Deemed-to-be-University
Mukesh Patel School of Technology Management & Engineering
Department of Computer Engineering
Course Code MCNB02002 Program MCA
Semester II Year I
Name of the Faculty Hiral Modi Class
Course Title Operating Systems Academic year 2016-17
PrePriority temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
static void execute(PrePriority p[])
{
System.out.println("\n\nProcess Arrival Burst Priority Waiting Turnaround
\n");
for(int i=0;i<p.length;i++)
{
for(;p[i].burstRem>0;(p[i].burstRem)--)
{
int start=-1,en=-1;
int j=i,k=i;
for(;k<p.length;k++)
{
if(j==k && p[k].arrival<=time)
{
start=j;
en=j;
}
else if(p[k].arrival<=time)
en=k;
}
p[i].wait = p[i].wait + (time-(p[i].waitTemp));
p[i].waitTemp = time+1;
if(en != -1 && start != -1)
sortProc(p, start, en);
++time;
}
p[i].turnaround = p[i].wait + p[i].burst;
int start=-1,en=-1;
int j=i+1,k=i+1;
for(;k<p.length;k++)
{
if(j==k && p[k].arrival<=time)
{
start=j;
SVKM’S NMIMS Deemed-to-be-University
Mukesh Patel School of Technology Management & Engineering
Department of Computer Engineering
Course Code MCNB02002 Program MCA
Semester II Year I
Name of the Faculty Hiral Modi Class
Course Title Operating Systems Academic year 2016-17
en=j;
}
else if(p[k].arrival<=time)
en=k;
}
if(en != -1 && start != -1)
sortProc(p, start, en);
}
for(int i=0;i<p.length;i++)
System.out.println("P"+p[i].id+"\t\t"+p[i].arrival+"\t"+p[i].burst+"\t"+p[i].priority+"\t"+p[i].wait
+"\t"+p[i].turnaround);
}
}
SVKM’S NMIMS Deemed-to-be-University
Mukesh Patel School of Technology Management & Engineering
Department of Computer Engineering
Course Code MCNB02002 Program MCA
Semester II Year I
Name of the Faculty Hiral Modi Class
Course Title Operating Systems Academic year 2016-17
Output:
PRIORITY:
PREEMPTIVE PRIORITY:
B.2 Conclusion:
Q1. What is the Average Waiting time and Turnaround time for the implemented Non-
Preemptive Priority Scheduling example?
Q2. What is the Average Waiting time and Turnaround time for the implemented Preemptive
Priority Scheduling example?
Q3. Which algorithm out of above two is more advantageous? Justify your answer.
Just by looking at the average waiting time and average turnaround time of the two
implementations of the same processes it cannot be determined which is better. But since the
preemptive priority scheduling allocates processes in a better manner according to their priority,
it can be said that it is the better algorithm out of the two.