Description: Following C++ code runs a simulation to show how process scheduling in operating system is done using the job scheduling algorithm Shortest Job First and also taking into account the concept of pre-emption. Pre-emptive SJF is one of the many scheduling algorithms in operating system.
#include<iostream>
#include<windows.h>
using namespace
std;
struct
Node{
int
p_id;
int
arr_time;
int
exe_time;
int
sum;
int
rem_time;
};
int
main(){
int
i, j, min = 100000, no_of_processes;
cout<<"\n\t*** --> Pre-emptive Shortest Job First
Scheduling <-- ***\n\n";
cout<<"Enter no. of
Processes:";
cin>>no_of_processes;
Node *Queue = new
Node[no_of_processes];
for
(i = 0; i<no_of_processes; i++){
cout << "\n\nGive
Process#" << i + 1 << "
id :";
cin>>Queue[i].p_id;
cout<<"\nGive
Arrival Time (0~23):";
cin>>Queue[i].arr_time;
if
(Queue[i].arr_time<min)
min = i;
cout<<"\nGive
Execution Time (in secs.):";
cin>>Queue[i].exe_time;
Queue[i].sum = 0;
Queue[i].rem_time = Queue[i].exe_time;
}
int
time = 0, count = 0;
i = min;
while
(count<no_of_processes){
cout << "Process#"
<< Queue[i].p_id<<" executing\t(Total
Time Elapsed : "<<time<<"secs.)\n";
cout << "-->
" << ++Queue[i].sum << endl;
Sleep(1000);
Queue[i].rem_time--;
if
(Queue[i].rem_time == 0)
count++;
time++;
for
(j = 0; j<no_of_processes; j++)
if
(Queue[j].rem_time != 0 && time >= Queue[j].arr_time){
min = j;
break;
}
for
(j = 0; j<no_of_processes; j++)
if
(Queue[j].rem_time != 0 && Queue[j].rem_time <=
Queue[min].rem_time&&time >= Queue[j].arr_time)
min = j;
i = min;
}
system("pause");
return
0;
}
No comments
Post a Comment