forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortest Job first Algorithm
More file actions
402 lines (359 loc) · 12.3 KB
/
Shortest Job first Algorithm
File metadata and controls
402 lines (359 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("nEnter Burst Time:n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
//sorting of burst times
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n;
total=0;
printf("nProcesst Burst Time tWaiting TimetTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("np%dtt %dtt %dttt%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n;
printf("nnAverage Waiting Time=%f",avg_wt);
printf("nAverage Turnaround Time=%fn",avg_tat);
}
Output:
Shortest Job First Scheduling
In the above program, we calculate the average waiting and average turn around times of the jobs. We first ask the user to enter the number of processes and store it in n. We then accept the burst times from the user. It is stored in the bt array.
After this, the burst times are sorted in the next section so the shortest one can be executed first. Here selection sort is used to sort the array of burst time bt.
Waiting time of the first element is zero, the remaining waiting time is calculated by using two for loop that runs from 1 to in that controls the outer loop and the inner loop is controlled by another for loop that runs from j=0 to j<i. Inside the loop, the waiting time is calculated by adding the burst time to the waiting time.
1
2
3
4
5
6
7
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
Total is the addition of all the waiting time together. The average waiting time is calculated:
avg_wt=(float)total/n;
and it is printed.
Next, the turnaround time is calculated by adding the burst time and the waiting time
1
2
3
4
5
6
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("np%dtt %dtt %dttt%d",p[i],bt[i],wt[i],tat[i]);
}
Again, here the for loop is used. And the total variable here holds the total turnaround time. After this the average turnaround time is calculated. This is how Non-Preemptive scheduling takes place
Code for Pre-emptive SJF Scheduling
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
int main()
{
int arrival_time[10], burst_time[10], temp[10];
int i, smallest, count = 0, time, limit;
double wait_time = 0, turnaround_time = 0, end;
float average_waiting_time, average_turnaround_time;
printf("nEnter the Total Number of Processes:t");
scanf("%d", &limit);
printf("nEnter Details of %d Processesn", limit);
for(i = 0; i < limit; i++)
{
printf("nEnter Arrival Time:t");
scanf("%d", &arrival_time[i]);
printf("Enter Burst Time:t");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
}
burst_time[9] = 9999;
for(time = 0; count != limit; time++)
{
smallest = 9;
for(i = 0; i < limit; i++)
{
if(arrival_time[i] <= time && burst_time[i] < burst_time[smallest] && burst_time[i] > 0)
{
smallest = i;
}
}
burst_time[smallest]--;
if(burst_time[smallest] == 0)
{
count++;
end = time + 1;
wait_time = wait_time + end - arrival_time[smallest] - temp[smallest];
turnaround_time = turnaround_time + end - arrival_time[smallest];
}
}
average_waiting_time = wait_time / limit;
average_turnaround_time = turnaround_time / limit;
printf("nnAverage Waiting Time:t%lfn", average_waiting_time);
printf("Average Turnaround Time:t%lfn", average_turnaround_time);
return 0;
}
Output:
Non pre-emptive Scheduling
The only difference in preemptive and non-preemptive is that when two burst times are same the algorithm evaluates them on first come first serve basis. Hence there is an arrival time variable.
With this, we come to an end of this Shortest Job Scheduling in C article. I hope you got an idea of how this scheduling works.
Now that you have understood the basics of SJF Scheduling in C, check out the training provided by Edureka on many technologies like Java, Spring and many more, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe
Got a question for us? Mention it in the comments section of this “Shortest Job First Scheduling in C Programming” blog and we will get back to you as soon as possible.
Recommended videos for you
how-to-crack-cfa-level-1-exam.jpg
How To Crack CFA Level 1 Exam
Microsoft-Azure-Certification-Microsoft-Azure-Tutorial-Azure-Training-Edureka.jpeg
Microsoft Azure Certifications – All You Need To Know
nandan-nilekani-on-entrepreneurship.jpg
Nandan Nilekani on Entrepreneurship
Recommended blogs for you
Service-now0-300x175.png
How To Get A ServiceNow Developer Instance?
Inheritance-in-C-300x175.jpg
All You Need to Know About Inheritance in C++
A-One-Stop-Guide-to-Learning-from-Home-300x175.jpg
A One-Stop Guide to Learning from Home
RCM-300x175.png
Here are the Ridiculously Committed Mentors of 2018
no-image-1.png
Introduction to C Programming-Algorithms
Radix-Sort-Program-In-C-300x175.png
How To Best Implement Radix Sort Program In C?
Asset-3@2x-300x208.png
Edureka: The Ridiculous Learning Revolutioner
mapsn-in-C-300x175.jpg
What are Maps in C++ and how to implement it?
Arrays-in-C-300x175.jpg
All you Need to Know about Arrays In C Programming
IndiaITRepublic-Top-10-Facts-about-IBM-India-Edureka-Blog-Edureka-0-1-300x175.png
#IndiaITRepublic – Top 10 Facts about Accenture – India
Feature-Image-of-RPA-Automation-Anywhere-RPA-Automation-Anywhere-Edureka-1-300x175.jpg
RPA Automation Anywhere – A Beginner’s Guide To Automation Anywhere
Vol.-XIV-Edureka-Career-Watch-25th-May-2019-Edureka-Blog-Edureka-300x175.png
Vol. XIV – Edureka Career Watch – 25th May 2019
Feature-Image-19-300x158.jpg
Why Strategic Transformation of Business is Important?
Top-10-Programming-Languages-1-300x169.jpg
Top 10 Programming Languages to Learn In 2021
risk-analysis-in-software-testing-300x175.jpg
What is Risk Analysis in Software Testing and how to perform it?
Instructor-Led-Training-2-2-300x175.jpg
Instructor-Led Training: RIGHT Way To Learn Online
no-image-1.png
How to Install Flutter on Windows? – Step by Step guide
IndiaITRepublic-Top-10-Facts-about-IBM-India-Edureka-Blog-Edureka-0-300x175.png
#IndiaITRepublic – Top 10 Facts about IBM – India
C-interview-questyion-1-300x175.png
Top 50 C# Interview Questions You Need To Know In 2023
What-is-Interface-Testing-300x175.jpg
What is Interface Testing and why do we need it?
Comments
0 Comments
Join the discussion
Trending Courses
Full Stack Web Development Internship Program
Full Stack Web Development Internship Program
29k Enrolled Learners
Weekend/Weekday
Live Class
Reviews
5 (11250)
Data Science and Machine Learning Internship Program
Data Science and Machine Learning Internship ...
22k Enrolled Learners
Weekend/Weekday
Live Class
Reviews
5 (8450)
Cyber Security and Ethical Hacking Internship Program
Cyber Security and Ethical Hacking Internship ...
15k Enrolled Learners
Weekend/Weekday
Live Class
Reviews
5 (5650)
Microsoft Power BI Certification Training Course
Microsoft Power BI Certification Training Cou ...
66k Enrolled Learners
Weekend/Weekday
Live Class
Reviews
5 (26350)
DevOps Certification Training Course
DevOps Certification Training Course
157k Enrolled Learners
Weekend/Weekday
Live Class
Reviews
5 (62700)
AWS Solutions Architect Certification Training Course
AWS Solutions Architect Certification Trainin ...
156k Enrolled Learners
Weekend/Weekday
Live Class
Reviews
5 (62250)
PMP Certification Training Course
PMP Certification Training Course
65k Enrolled Learners
Weekend/Weekday
Live Class
Reviews
5 (25800)
Python Certification Training Course
Python Certification Training Course
52k Enrolled Learners
Weekend
Live Class
Reviews
5 (20700)
Cyber Security Course
Cyber Security Course
60k Enrolled Learners
Weekend
Live Class
Reviews
5 (23650)
ChatGPT-4 Complete Course: Beginners to Advanced
ChatGPT-4 Complete Course: Beginners to Advan ...
9k Enrolled Learners
Weekend
Live Class
Reviews
5 (3600)
Browse Categories
Artificial IntelligenceBI and VisualizationBig DataBlockchainBusiness ManagementCloud ComputingCyber SecurityData ScienceData Warehousing and ETLDatabasesDevOpsDigital MarketingEnterpriseFront End Web DevelopmentHuman Resource ManagementMobile DevelopmentOperating SystemsOperations ManagementProduct ManagementProgramming & FrameworksProject Management and MethodologiesRobotic Process AutomationSoftware TestingSupply Chain ManagementSystems & Architecture
Subscribe to our Newsletter, and get personalized recommendations.
Already have an account? Sign in.×
TRENDING CERTIFICATION COURSES
DevOps Certification Training
AWS Architect Certification Training
Big Data Hadoop Certification Training
Tableau Training & Certification
Python Certification Training for Data Science
Selenium Certification Training
PMP® Certification Exam Training
Robotic Process Automation Training using UiPath
Apache Spark and Scala Certification Training
Microsoft Power BI Training
Online Java Course and Training
Python Certification Course
TRENDING MASTERS COURSES
Data Scientist Masters Program
DevOps Engineer Masters Program
Cloud Architect Masters Program
Big Data Architect Masters Program
Machine Learning Engineer Masters Program
Full Stack Web Developer Masters Program
Business Intelligence Masters Program
Data Analyst Masters Program
Test Automation Engineer Masters Program
Post-Graduate Program in Artificial Intelligence & Machine Learning
Post-Graduate Program in Big Data Engineering
COMPANY
About us
News & Media
Reviews
Contact us
Blog
Community
Sitemap
Blog Sitemap
Community Sitemap
Webinars
WORK WITH US
Careers
Become an Instructor
Become an Affiliate
Become a Partner
Hire from Edureka
DOWNLOAD APP
apple_store google_playstore
CATEGORIES
Cloud Computing DevOps Big Data Data Science BI and Visualization Programming & Frameworks Software Testing Project Management and Methodologies Robotic Process Automation Frontend Development Data Warehousing and ETL Artificial Intelligence Blockchain Databases Cyber Security Mobile Development Operating Systems Architecture & Design Patterns Digital Marketing
TRENDING BLOG ARTICLES
Selenium tutorial Selenium interview questions Java tutorial What is HTML Java interview questions PHP tutorial JavaScript interview questions Spring tutorial PHP interview questions Inheritance in Java Polymorphism in Java Spring interview questions Pointers in C Linux commands Android tutorial JavaScript tutorial jQuery tutorial SQL interview questions MySQL tutorial Machine learning tutorial Python tutorial What is machine learning Ethical hacking tutorial SQL injection AWS certification career opportunities AWS tutorial What Is cloud computing What is blockchain Hadoop tutorial What is artificial intelligence Node Tutorial Collections in Java Exception handling in java Python Programming Language Python interview questions Multithreading in Java ReactJS Tutorial Data Science vs Big Data vs Data Analytics Software Testing Interview Questions R Tutorial Java Programs JavaScript Reserved Words and Keywords Implement thread.yield() in Java: Examples Implement Optical Character Recognition in Python All you Need to Know About Implements In Java
© 2023 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved. Terms & Conditions Legal & Privacy
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.