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

Assignment 3 Codes

The document describes three scheduling algorithms: FCFS, Round Robin, and SJF. For each algorithm, it provides code samples in Java to implement the algorithm, sample input/output, and an explanation of how the algorithm works.

Uploaded by

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

Assignment 3 Codes

The document describes three scheduling algorithms: FCFS, Round Robin, and SJF. For each algorithm, it provides code samples in Java to implement the algorithm, sample input/output, and an explanation of how the algorithm works.

Uploaded by

simib70624
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Instructions:

1 .Class name and java file name should be same.

2. Us the below command to run the programs

javac filename.java

java filename

1. FCFS Scheduling Algorithm:


import java.util.*;
class fcfs
{
public static void main(String[] args)
{
int id[]=new int[20];
int etime[]=new int[20];
int stime[]=new int[20];
int wtime[]=new int[20];
int tat[]=new int[20];
int total=0,total1=0;
float avg,avg1;
Scanner sn = new Scanner(System.in);
System.out.print("\nEnter the number of processes : ");
int n = sn.nextInt();
for (int i=0;i<n;i++) {
System.out.println();
System.out.print("Enter the process ID of process "+(i+1)+" : ");
id[i]=sn.nextInt();
System.out.print("Enter the execution time of process "+(i+1)+" : ");
etime[i]=sn.nextInt();
}
stime[0]=0;
for (int i=1;i<n;i++) {
stime[i]=stime[i-1]+etime[i-1];
}
wtime[0]=0;
for (int i=0;i<n;i++) {
wtime[i]=stime[i]-id[i];
total=total+wtime[i];
}
for(int i=0;i<n;i++)
{
tat[i]=wtime[i]+etime[i];
total1=total1+tat[i];
}
avg=(float)total/n;
avg1=(float)total1/n;
System.out.println("\nArrival_time\tExecution_time\ tService_time\tWait_time\
tturn_around time");
for(int i=0;i<n;i++)
{
System.out.println(id[i]+"\t\t"+etime[i]+"\t\t"+stime[i] +"\t\t"+wtime[i]+"\t\
t"+tat[i]);
}
System.out.println("\nAverage turn around time: "+avg1+"\nAverage wait time:
"+avg);
}

OUTPUT:

Enter the number of processes : 4

Enter the process ID of process 1 : 0


Enter the execution time of process 1 : 8

Enter the process ID of process 2 : 1


Enter the execution time of process 2 : 4

Enter the process ID of process 3 : 2


Enter the execution time of process 3 : 9

Enter the process ID of process 4 : 3


Enter the execution time of process 4 : 5

Arrival_time Exe._time Service_time Wait_time turn_around time


0 8 0 0 8
1 4 8 7 11
2 9 12 10 19
3 5 21 18 23

Average turn around time: 15.25


Average wait time: 8.75
2. Round Robin Scheduling Algorithm:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class RR {
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Time Quantum: ");
int q = Integer.parseInt(br.readLine());
System.out.println("Please enter the number of Processes: ");
int n = Integer.parseInt(br.readLine());
int proc[][] = new int[n + 1][4]; //proc[][0] is the AT array,[][1] - RT,[][2] - WT,[][3] -
TT
for(int i = 1; i <= n; i++)
{
System.out.println("Please enter the Burst Time for Process " + i + ": ");
proc[i][1] = Integer.parseInt(br.readLine());
}
System.out.println();
//Calculation of Total Time and Initialization of Time Chart array
int total_time = 0;
for(int i = 1; i <= n; i++)
{
total_time += proc[i][1];
}
int time_chart[] = new int[total_time];
int sel_proc = 1;
int current_q = 0;
for(int i = 0; i < total_time; i++)
{
//Assign selected process to current time in the Chart
time_chart[i] = sel_proc;
//Decrement Remaining Time of selected process by 1 since it has been assigned the CPU
for 1 unit of time
proc[sel_proc][1]--;
//WT and TT Calculation
for(int j = 1; j <= n; j++)
{
if(proc[j][1] != 0)
{
proc[j][3]++;//If process has not completed execution its TT is incremented by 1
if(j != sel_proc)//If the process has not been currently assigned the CPU its WT is
incremented by 1
proc[j][2]++;
}
else if(j == sel_proc)//This is a special case in which the process has been assigned CPU
and has completed its execution
proc[j][3]++;
}

//Printing the Time Chart


if(i != 0)
{
if(sel_proc != time_chart[i - 1])
//If the CPU has been assigned to a different Process we need to print the current value
of time and the name of
//the new Process
{
System.out.print("--" + i + "--P" + sel_proc);
}
}
else//If the current time is 0 i.e the printing has just started we need to print the name of
the First selected Process
System.out.print(i + "--P" + sel_proc);
if(i == total_time - 1)//All the process names have been printed now we have to print the
time at which execution ends
System.out.print("--" + (i + 1));
//Updating value of sel_proc for next iteration
current_q++;
if(current_q == q || proc[sel_proc][1] == 0)//If Time slice has expired or the current
process has completed execution
{
current_q = 0;
//This will select the next valid value for sel_proc
for(int j = 1; j <= n; j++)
{
sel_proc++;
if(sel_proc == (n + 1))
sel_proc = 1;
if(proc[sel_proc][1] != 0)
break;
}
}
}
System.out.println();
System.out.println();
//Printing the WT and TT for each Process
System.out.println("P\t WT \t TT ");
for(int i = 1; i <= n; i++)
{
System.out.printf("%d\t%3dms\t%3dms",i,proc[i][2],proc[i][3]);
System.out.println();
}
System.out.println();
//Printing the average WT & TT
float WT = 0,TT = 0;
for(int i = 1; i <= n; i++)
{
WT += proc[i][2];
TT += proc[i][3];
}
WT /= n;
TT /= n;
System.out.println("The Average WT is: " + WT + "ms");
System.out.println("The Average TT is: " + TT + "ms");
}
}

OUTPUT:

Enter the Time Quantum:


3
Please enter the number of Processes:
5
Please enter the Burst Time for Process 1:
5
Please enter the Burst Time for Process 2:
3
Please enter the Burst Time for Process 3:
8
Please enter the Burst Time for Process 4:
6
Please enter the Burst Time for Process 5:
9

0--P1--3--P2--6--P3--9--P4--12--P5--15--P1--17--P3--20--P4--23--P5--26--P3--28--P5--31

P WT TT
1 12ms 17ms
2 3ms 6ms
3 20ms 28ms
4 17ms 23ms
5 22ms 31ms
The Average WT is: 14.8ms

The Average TT is: 21.0ms


3. SJF Scheduling Algorithm :

import java.util.*;
class Sjf_primitive
{
public static void main(String[] args)
{
int id[]=new int[20];
int etime[]=new int[20];
int stime[]=new int[20];
int wtime[]=new int[20];
int btime[]=new int[20];
int ctime[]=new int[20];
int flag[]=new int[20];
//check process is completed or not.
int ta[]=new int[20];
int total=0;
int st=0;
int temp,i;
float avgwt=0,avgta=0;
Scanner sn = new Scanner(System.in);
System.out.print("\nEnter the number of processes : ");
int n = sn.nextInt();
for (i=0;i<n;i++)
{
System.out.println();
System.out.print("Enter the process ID of process
"+(i+1)+" : ");
id[i]=sn.nextInt();
System.out.print("Enter the execution time of process "+(i+1)+" : ");
etime[i]=sn.nextInt();
btime[i]=etime[i];
flag[i]=0;
}
while(true)
{
int min=9999,c=n;
if(total==n)
break;
for(i=0;i<n;i++)
{
if((id[i]<=st) && (flag[i]==0) && (etime[i]<min))
{
min=etime[i];
c=i;
}
}
if(c==n)
st++;
else
{
etime[c]--;
st++;
if(etime[c]==0)
{
ctime[c]=st;
flag[c]=1;
total++;
}
}
}
for(i=0;i<n;i++)
{
ta[i]=ctime[i]-id[i];
wtime[i]=ta[i]-btime[i];
avgwt+=wtime[i];
avgta+=ta[i];
}
System.out.println("\nArrival_time\tExecution_time\tcompletion_time\t Wait_time\tturn
around time");
for(i=0;i<n;i++)
{
System.out.println(id[i]+"\t\t"+btime[i]+"\t\t"+ctime[i]+"\t\t"+wtime[i]+"\t\
t"+ta[i]);
}
System.out.println("\nAverage wait time:
"+(float)(avgwt/n));
System.out.println("\nAverage turn around time:
"+(float)(avgta/n));
}

}
OUTPUT:

Enter the number of processes : 4

Enter the process ID of process 1 : 0


Enter the execution time of process 1 : 7

Enter the process ID of process 2 : 2


Enter the execution time of process 2 : 4

Enter the process ID of process 3 : 4


Enter the execution time of process 3 : 1

Enter the process ID of process 4 : 5


Enter the execution time of process 4 : 4

Arrival_time Exe._time completion_time Wait_time turn around time


0 7 16 9 16
2 4 7 1 5
4 1 5 0 1
5 4 11 2 6

Average wait time: 3.0

You might also like