Sie sind auf Seite 1von 8

SVKMS 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

PART B

(PART B: TO BE COMPLETED BY STUDENTS)

Roll No: A008 Name: Pronoy Debdas

Class: MCA, SEM 2 Batch: B1

Date of Experiment: Date of Submission: 19/03/17

Grade:

B.1 Work done by student

Code:

import java.util.Scanner;
class Sjf
{
private int burst;
private int arrival;
private int id;
private int wait;
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();
}
static void sortProc(Sjf 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++)
SVKMS 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

if(p[i].arrival>p[j].arrival)
{
Sjf 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].burst>p[j].burst)
{
Sjf 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].burst>p[j].burst)
{
Sjf temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
static void execute(Sjf p[])
{
System.out.println("\n\nProcess\t Arrival Time\t Burst Time\t Waiting
Time\tTurnaround Time\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;

System.out.println("P"+p[i].id+"\t\t"+p[i].arrival+"\t\t"+p[i].burst+"\t\t"+p[i].wait+"\t\t"+p[i].tur
naround);
for(int burst=p[i].burst;burst>0;burst--)
SVKMS 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

++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 SJF and 2 for SRTF 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();
Sjf p[] = new Sjf[l];
for(int i=0;i<l;i++)
{
System.out.println("Enter Details for Process P"+
(i+1));
p[i] = new Sjf();
p[i].setProc(i+1);
}
Sjf.sortProc(p,-1,-1);
SVKMS 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

Sjf.execute(p);
break;
case 2:
System.out.println("Enter Number Of Processes: ");
l = sc.nextInt();
Srtf q[] = new Srtf[l];
for(int i=0;i<l;i++)
{
System.out.println("Enter Details for Process P"+
(i+1));
q[i] = new Srtf();
q[i].setProc(i+1);
}
Srtf.sortProc(q,-1,-1);
Srtf.execute(q);
break;
default:
System.out.println("Bye!!");
System.exit(0);
}
}
while(true);
}
}

import java.util.Scanner;
class Srtf
{
private int burst;
private int burstRem;
private int arrival;
private int id;
private int wait;
private int waitTemp;
private int turnaround;
static int time;
void setProc(int i)
{
Scanner sc = new Scanner(System.in);
id=i;
System.out.println("\nEnter Burst Time: ");
SVKMS 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

burst = sc.nextInt();
burstRem=burst;
System.out.println("\nEnter Arrival Time: ");
arrival = sc.nextInt();
waitTemp=arrival;
wait=0;
}
static void sortProc(Srtf 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)
{
Srtf 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].burst>p[j].burst)
{
Srtf 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].burstRem>p[j].burstRem)
{
Srtf temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
SVKMS 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

static void execute(Srtf p[])


{
System.out.println("\n\nProcess\t Arrival Time\t Burst Time\t Waiting
Time\tTurnaround Time\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;
en=j;
}
else if(p[k].arrival<=time)
en=k;

}
SVKMS 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

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\t"+p[i].burst+"\t\t"+p[i].wait+"\t\t"+p[i].tur
naround);
}
}

Output:

SJF:

SRTF:
SVKMS 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

B.2 Conclusion:

Understood the basics of Process & Process Scheduling. Implemented SJF & SRT Process
Scheduling Algorithm using java.

B.3 Questions of Curiosity:

Q1. What is the Average Waiting time and Turnaround time for the implemented SJF example?

The Average Waiting Time is 5.4ms.

The Average Turnaround Time is 10ms.

Q2. What is the Average Waiting time and Turnaround time for the implemented SRT example?

The Average Waiting Time is 4.4ms.

The Average Turnaround Time is 7ms.

Q3. Which algorithm out of SJF & SRT is more advantageous? Justify your answer.

SRTF is more advantageous because the average waiting time and the average turnaround
time is lesser than the average waiting time and turnaround time for the same processes when
implemented with SJF.

Das könnte Ihnen auch gefallen