Skip to content

Commit 7b0fb5b

Browse files
committed
preemptive sjf added
1 parent b55ac87 commit 7b0fb5b

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/sstf/target/
22
/fcfs/target/
33
/NonprempriveSJF/target/
4+
/PreemptiveSJF/target/

PreemptiveSJF/pom.xml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.mycompany</groupId>
5+
<artifactId>PreemptiveSJF</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>16</maven.compiler.source>
11+
<maven.compiler.target>16</maven.compiler.target>
12+
<exec.mainClass>com.mycompany.preemptivesjf.PreemptiveSJF</exec.mainClass>
13+
</properties>
14+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
3+
*/
4+
5+
package com.mycompany.preemptivesjf;
6+
import java.util.*;
7+
/**
8+
*
9+
* @author lenovo
10+
*/
11+
public class PreemptiveSJF {
12+
13+
public static void main(String[] args) {
14+
System.out.println("Enter the number of processes");
15+
Scanner sc= new Scanner(System.in);
16+
int pno=sc.nextInt();
17+
18+
int[] at=new int[pno];
19+
int[] bt=new int[pno];
20+
int[] rt=new int[pno]; //remaining time
21+
int[] wt=new int[pno];
22+
int[] bt1=new int[pno];
23+
int[] ct=new int[pno];
24+
int[] flag=new int[pno];
25+
int[] tat=new int[pno];
26+
int[] pid=new int[pno];
27+
28+
System.out.println("Enter the arrival time");
29+
for(int i=0;i<pno;i++)
30+
{
31+
at[i]=sc.nextInt();
32+
}
33+
34+
System.out.println("Enter the burst time");
35+
for(int i=0;i<pno;i++)
36+
{
37+
bt[i]=sc.nextInt();
38+
}
39+
System.out.println("Please wait!");
40+
//initialising flag
41+
for(int i=0;i<pno;i++)
42+
{
43+
flag[i]=0;
44+
}
45+
46+
//copying burst time in remaining time
47+
48+
for(int i=0;i<pno;i++)
49+
{
50+
rt[i]=bt[i];
51+
bt1[i]=bt[i]; //for priniting purpose only
52+
53+
}
54+
int st=0;
55+
int count=0;
56+
int smallest=0;
57+
//max number
58+
int end;//end time
59+
for(st=0;count!=pno;st++)
60+
{
61+
int min=99999;
62+
63+
for(int i=0; i<pno; i++)
64+
{
65+
if(at[i]<=st && bt[i]<min && bt[i]>0 ) //chosing the smallest burst time
66+
smallest=i; //choosing the smallest burst time of all
67+
min=bt[i];
68+
}
69+
bt[smallest]--; //dec burst time by 1...we decrement step by step i.e 1 by 1 to check if there is any other process at that particular time having burst time less
70+
71+
72+
if(bt[smallest]==0) // iterate through loop untill bt=0 i.e process execution is completed
73+
{
74+
pid[smallest]++;
75+
count++;
76+
end=st+1;
77+
tat[smallest] = end - at[smallest];
78+
ct[smallest] = end;
79+
wt[smallest] =tat[smallest] - bt1[smallest];
80+
81+
}
82+
83+
}
84+
System.out.println("\tpid\t\tat\t\tbt\t\ttat\t\twt\t\tct\t\t");
85+
for(int i=0;i<pno;i++)
86+
{
87+
System.out.println("\t\t"+pid[i]+"\t\t"+at[i]+"\t\t"+bt1[i]+"\t\t"+tat[i]+"\t\t"+wt[i]+"\t\t"+ct[i]+"\t\t");
88+
}
89+
90+
91+
92+
93+
}
94+
}

0 commit comments

Comments
 (0)