EE-332 Operating System
Department of Electrical Engineering | The University of Faisalabad
Zeeshan Iqbal BEE-FA18-022
(Computer)
Experiment # 8
Title:
Introduction to shortest job first scheduling
Objective:
To write a program to implement CPU scheduling algorithm for shortest job first
scheduling.
First-Come First-Serve Scheduling (FCFS)
• First come first serve. With FCFS, the process that requests the CPU first is allocated the
CPU first. The implementation of FCFS policy is easily managed with a FIFO queue such
as customers waiting in line at the bank or the post office.
• The average waiting time under the FCFS policy, however, is often quite long. Consider
the following set of processes that arrive at time 0, with the length of the CPU-burst
time given in milliseconds:
Process Burst Time
P1 24
P2 3
P3 3
• In the first Gantt chart below, processes arrive in the order P1, P2, P3. The waiting time
is 0ms for P1, 24 ms for P2, and 27 ms for P3. Thus, the average waiting time for the
three processes is ( 0 + 24 + 27 ) / 3 = 17.0 ms.
• In the second Gantt chart below, processes arrive in the order P2, P3, P1. The same
three processes have an average wait time of ( 0 + 3 + 6 ) / 3 = 3.0 ms. The average
waiting time under a FCFS policy is generally not minimal, and may vary substantially if
the process CPU-burst times vary greatly.
EE-332 Operating System
Department of Electrical Engineering | The University of Faisalabad
Zeeshan Iqbal BEE-FA18-022
(Computer)
ALGORITHM:
• Start the program. Get the number of processes and their burst time.
• Initialize the waiting time for process 1 as 0.
• The processes are stored according to their burst time.
• The waiting time for the processes are calculated a follows:
for(i=2;i<=n;i++).wt.p[i]=p[i=1]+bt.p[i-1].
• The waiting time of all the processes summed and then the average time is calculate
• The waiting time of each processes and average time are displayed.
• Stop the program
PROGRAM (SHORTEST JOB FIRST SCHEDULING):
#include<iostream>
#include<stdio.h>
struct process
{
int pid;
int bt;
int wt,tt;
}
p[10];
int main()
{
int i,n,totwt,tottt,avg1,avg2;
printf("enter the no of process: "); scanf("%d",&n);
for(i=1;i<=n;i++)
{
p[i].pid=i;
printf("enter the burst time(i): "); scanf("%d",&p[i].bt);
}
p[1].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;
while(i<=n)
{
p[i].wt=p[i-1].bt+p[i-1].wt; p[i].tt=p[i].bt+p[i].wt;
EE-332 Operating System
Department of Electrical Engineering | The University of Faisalabad
Zeeshan Iqbal BEE-FA18-022
(Computer)
i ++;
}
i=1;
totwt=tottt=0;
printf("n processid t btt wtt ttn");
while(i<=n){
printf("nt%d t%d t%d t%d",p[i].pid,p[i].bt,p[i].wt,p[i].tt);
totwt=p[i].wt+totwt;
tottt=p[i].tt+tottt;
i++;
}
avg1=totwt/n; avg2=tottt/n;
printf("navg1=%d t avg2=%dt",avg1,avg2);
getc;
return 0;
}
Output:

Introduction to shortest job first scheduling

  • 1.
    EE-332 Operating System Departmentof Electrical Engineering | The University of Faisalabad Zeeshan Iqbal BEE-FA18-022 (Computer) Experiment # 8 Title: Introduction to shortest job first scheduling Objective: To write a program to implement CPU scheduling algorithm for shortest job first scheduling. First-Come First-Serve Scheduling (FCFS) • First come first serve. With FCFS, the process that requests the CPU first is allocated the CPU first. The implementation of FCFS policy is easily managed with a FIFO queue such as customers waiting in line at the bank or the post office. • The average waiting time under the FCFS policy, however, is often quite long. Consider the following set of processes that arrive at time 0, with the length of the CPU-burst time given in milliseconds: Process Burst Time P1 24 P2 3 P3 3 • In the first Gantt chart below, processes arrive in the order P1, P2, P3. The waiting time is 0ms for P1, 24 ms for P2, and 27 ms for P3. Thus, the average waiting time for the three processes is ( 0 + 24 + 27 ) / 3 = 17.0 ms. • In the second Gantt chart below, processes arrive in the order P2, P3, P1. The same three processes have an average wait time of ( 0 + 3 + 6 ) / 3 = 3.0 ms. The average waiting time under a FCFS policy is generally not minimal, and may vary substantially if the process CPU-burst times vary greatly.
  • 2.
    EE-332 Operating System Departmentof Electrical Engineering | The University of Faisalabad Zeeshan Iqbal BEE-FA18-022 (Computer) ALGORITHM: • Start the program. Get the number of processes and their burst time. • Initialize the waiting time for process 1 as 0. • The processes are stored according to their burst time. • The waiting time for the processes are calculated a follows: for(i=2;i<=n;i++).wt.p[i]=p[i=1]+bt.p[i-1]. • The waiting time of all the processes summed and then the average time is calculate • The waiting time of each processes and average time are displayed. • Stop the program PROGRAM (SHORTEST JOB FIRST SCHEDULING): #include<iostream> #include<stdio.h> struct process { int pid; int bt; int wt,tt; } p[10]; int main() { int i,n,totwt,tottt,avg1,avg2; printf("enter the no of process: "); scanf("%d",&n); for(i=1;i<=n;i++) { p[i].pid=i; printf("enter the burst time(i): "); scanf("%d",&p[i].bt); } p[1].wt=0; p[1].tt=p[1].bt+p[1].wt; i=2; while(i<=n) { p[i].wt=p[i-1].bt+p[i-1].wt; p[i].tt=p[i].bt+p[i].wt;
  • 3.
    EE-332 Operating System Departmentof Electrical Engineering | The University of Faisalabad Zeeshan Iqbal BEE-FA18-022 (Computer) i ++; } i=1; totwt=tottt=0; printf("n processid t btt wtt ttn"); while(i<=n){ printf("nt%d t%d t%d t%d",p[i].pid,p[i].bt,p[i].wt,p[i].tt); totwt=p[i].wt+totwt; tottt=p[i].tt+tottt; i++; } avg1=totwt/n; avg2=tottt/n; printf("navg1=%d t avg2=%dt",avg1,avg2); getc; return 0; } Output: