Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This assignment addresses the implementation of CPU-scheduling algorithms in an

ID: 3840117 • Letter: T

Question

This assignment addresses the implementation of CPU-scheduling algorithms in an operating system.

Each process in an operating system is managed by using a data structure called the Process Control Block (PCB). A PCB contains the process ID, arrival timestamp, total burst time, execution start time, execution end time, remaining burst time and the priority of the process in the system. The PCB class is defined as follows and is accessible from the rest of the code in this project:

    public class PCB {

        public int processID;

        public long arrivalTimeStamp;

        public long totalBurstTime;

        public long executionStartTime;

        public long executionEndTime;

        public long remainingBurstTime;

        public int processPriority;

    }

The set of processes in the operating system that are ready to execute are maintained in a data structure called the Ready Queue. This data structure is a List of PCBs of the processes that are waiting for the CPU to become available so that they can execute.

To determine the schedule of execution for the processes in an operating system, we consider three policies:

   1. Priority-based Preemptive Scheduling (PP)

   2. Shortest-Remaining-Time-Next Preemptive Scheduling (SRTP)

   3. Round-Robin Scheduling (RR)

In order to implement the above policies, we need to develop methods that handle arrival of processes for execution and the completion of process execution. Whenever a process arrives for execution, it can either join the ready queue and wait for its chance to execute or execute immediately (if there is no other process currently executing or if the currently-running process can be preempted). Whenever a process completes execution, another process from the ready queue is chosen to execute next, based on the scheduling policy. The details of these methods are described below in the specification and you need to develop code for these methods that follows the specification

Explanation / Answer

package cpuscheduling;
import javax.swing.JOptionPane;
public class operation {

int n;
int Bu[] =new int[20];
float Twt,Awt,w;
float A[]=new float[20];
float Wt[]=new float[20];
void getData()
{
int i;
n=Integer.parseInt(JOptionPane.showInputDialog("Enter Number of Processes:"));
for(i=1;i<=n;i++)
{
Bu[i]=Integer.parseInt(JOptionPane.showInputDialog("Enter The BurstTime for Process p"+i+""));
}
}
void SjfNp()
{
int i,Tt=0,temp,j;
int B[]=new int[10];
char S[]=new char[10];
float temp1,t;
Twt=0;
w=0;
for(i=1;i<=n;i++)
{
B[i]=Bu[i];
S[i]='T';
Tt=Tt+B[i];
A[i]=Integer.parseInt(JOptionPane.showInputDialog("Enter The Arrival Time for Process p"+i+""));
}

for(i=n;i>=1;i--)
{
for(j=3;j<=n;j++)
{
if(B[j-1]>B[j])
{
temp=B[j-1];
temp1=A[j-1];
B[j-1]=B[j];
A[j-1]=A[j];
B[j]=temp;
A[j]=temp1;
}
}
}
Wt[1]=0;
w=w+B[1];
t=w;
S[1]='F';

while(w<Tt)
{
i=3;
while(i<=n)
{
if(S[i]=='T'&&A[i]<=t)
{
Wt[i]=w;
S[i]='F';
w=w+B[i];
t=w;
i=3;
}
else
i++;
}
}
for(i=1;i<=n;i++)
for(i=1;i<=n;i++)
Twt=Twt+(Wt[i]-A[i]);
Awt=Twt/n;
System.out.println("Total Waiting Time:"+Twt);
System.out.println("Average Waiting Time:"+Awt);
}
void RoundRobin()
{
int i,j,tq,k;
int B[]=new int[08];
int Rrobin[][]=new int[08][08];
int count[]=new int[08];
int max=0;
int m;
Twt=0;
n=Integer.parseInt(JOptionPane.showInputDialog("Enter Number of Processes:"));
for(i=1;i<=n;i++)
{
B[i]=Integer.parseInt(JOptionPane.showInputDialog("Enter The BurstTime for Process p"+i+""));
}
tq=Integer.parseInt(JOptionPane.showInputDialog("Enter The Time Quantum:"));
m=max/tq+1;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
Rrobin[i][j]=0;
}
}
//placing value in the Rrobin array
i=1;
while(i<=n)
{
j=1;
while(B[i]>0)
{
if(B[i]>=tq)
{
B[i]=B[i]-tq;
Rrobin[i][j]=tq;
j++;
}
else
{
Rrobin[i][j]=B[i];
B[i]=0;
j++;
}
}
count[i]=j-1;
i++;
}
int x=1;
i=1;
while(x<=n)
{
for(int a=1;a<x;a++)
{
Wt[x]=Wt[x]+Rrobin[a][i];
}
i=1;
int z=x;
j=count[z];
k=1;
while(k<=j-1)
{
if(i==n+1)
{
i=1;
k++;
}
else
{
if(i!=z)
{
Wt[z]=Wt[z]+Rrobin[i][k];
}
i++;
}
}
x++;
}
for(i=1;i<=n;i++)
{
System.out.println("Waiting Time for process p"+i+":"+Wt[i]);
}
//calculating Average Weighting Time
for(i=1;i<=n;i++)
{
Twt=Twt+Wt[i];
Awt=Twt/n;
}
System.out.print("Total Waiting Time :"+Twt);
System.out.print("Average Waiting Time :"+Awt);
}
&nbsp;
}