- For individual users
- Instant access to solutions
- Ask your tech questions
- Start your 30-day Free Trial
Main Topics
Browse All TopicsI am trying to change this to preemptive shortest job first, but I honestly just do not understand what to do. I know that I need to remove the quantum and determine the scheduling based on the next burst length..I think. But I really do not understand it. Can anyone begin to point me in the right direction here. Below is the same code formulated in Preemptive Shortest Job First, except it prompts for a quantum instead of using a formula to figure it out.
#include <iostream>
#include <iomanip>
using namespace std;
// Struct to represent process
struct process {
int idno;
int ready; // start time
int run; // run time
int remaining; // time remaining
int active; // is process active?
int finish; // finish time
};
// Primitive class to represent system clock
class myclock
{
protected:
int clock99; // represent time on clock
// clock itself is a predefined function name
public:
myclock() { clock99 = 0; }
void setclock( int w ) { clock99 = w; }
void tick() { clock99++; }
int gettime() { return clock99; }
};
// This class represents the processor and the list of jobs waiting
// for the processor
class processor
{
protected:
int maxjobs; // maximum number of jobs
int jobsub; // location of next job to be added, also # of jobs
int current_id; // subscript of job currently running
int previous_id;
int quantum; // maximum length of time a job may run
int elapsed; // time current job has been running
process * list; // list of processes that want to use processor
public:
// constructor
processor( int q, int n)
{
maxjobs = n;
jobsub = 0;
elapsed = 0;
current_id = -1;
previous_id = -1;
quantum = q;
list = new process[n];
}
// place a single job into a queue
int insertjob( int id, int start, int run )
{
if( jobsub < maxjobs )
{
list[jobsub].idno = id;
list[jobsub].ready = start;
list[jobsub].run = run;
list[jobsub].remaining = run;
list[jobsub].active = 0;
list[jobsub].finish = -1;
jobsub++;
return 1;
}
else
return 0;
}
// display all jobs in the queue
void printlist()
{
if( jobsub > 0 )
for( int i = 0; i < jobsub; i++ )
{
cout << list[i].idno << setw(10) << list[i].ready << setw(10)
<< list[i].run << setw(10) << list[i].finish;
if( list[i].finish != -1 )
cout << setw(10) << list[i].finish - list[i].ready << endl;
else
cout << endl;
}
}
// determine if any jobs are left to run
int jobsleft()
{
int temp = 0;
for( int i=0; i < jobsub ; i++)
if( list[i].remaining != 0 )
temp = 1;
return temp;
}
// make all processes that become ready at the current time active
void fixready( int t)
{
for( int i=0; i < jobsub; i++)
if( list[i].ready == t)
list[i].active = 1;
}
// are there any active processes?
int anyactive()
{
int temp = 0;
for( int i = 0; i < jobsub; i++)
if( list[i].active )
temp = 1;
return temp;
}
// actual work of the simulation, checks quantum, performs context
// switch if necessary, and updates statistics
void check( int t )
{
if( current_id != -1 )
{
if( list[current_id].remaining
{
list[current_id].finish = t;
list[current_id].active = 0;
previous_id = -1;
current_id = -1;
elapsed = 0;
}
if( elapsed == quantum)
{
previous_id = current_id;
current_id = -1;
elapsed = 0;
}
}
if( current_id == -1 && anyactive() )
{
current_id = findmin();
}
if( current_id != -1 )
{
list[current_id].remaining
elapsed++;
}
}
// returns the id of the current process
int getcurrent() { return list[current_id].idno; }
// determines the position of the next process to get the processor
// this is the only function that needs modification if the
// scheduling discipline is changed from SRT
int findmin()
{
int found = 0, smallsub;
for( int i = 0; i < jobsub; i++)
if( list[i].remaining != 0 && list[i].active )
if( found == 0 )
{
smallsub = i;
found = 1;
}
else
if( list[i].active && (list[i].remaining <
list[smallsub].remaining ))
smallsub = i;
// This code makes sure that a job different from the current one is
// chosen in case of a tie.
if( smallsub == previous_id )
{
int countties = 0;
for( int i = 0; i < jobsub; i++)
if( list[i].active && list[i].remaining == list[smallsub].remaining )
countties++;
if( countties > 1 )
{
int j = 0;
int found = 0;
while( j < jobsub && !found )
if( list[j].active && list[j].remaining == list[smallsub].remaining
&& j != smallsub )
found = 1;
else
j++;
smallsub = j;
}
}
return smallsub;
}
// Sets finishing time of the last job to finish
// Not a very pleasing fix to allow the main program to run but it works.
void endsim( int t )
{
list[current_id].finish = t;
}
};
int main()
{
myclock timer; // clock
int quantum, t;
int newid,newstart,newruntime;
char ch;
int n;
cout << "How Many Jobs? ";
cin >> n; // Number of Jobs
cout << endl << "What Is The Quantum? ";
cin >> quantum; // Quantum
processor cpu( quantum, n); // Set up processor
cout << endl << "Enter ID Number, Start Time, and Run Time For Each Job ";
cout << endl << "-------------------------
cout << endl;
for( int i = 0; i < n; i++)
{
cin >> newid >> newstart >> newruntime;
if( cpu.insertjob( newid, newstart, newruntime ) )
cout << "Job Inserted Successfully " << endl;
else
cout << "Processor Queue Size Limit Exceeded " << endl;
cout << "-------------------------
<< endl;
}
cout << endl << "-------------------------
<< endl;
cout << "Summary of Data Entered " << endl;
cout << "-------------------------
cpu.printlist();
cout << "-------------------------
cout << endl << endl << "-------------------------
cout << "Gantt Chart" << endl;
cout << "-------------------------
while( cpu.jobsleft() ) // continue until time remaining = 0
{ // for all jobs
t = timer.gettime(); // read clock
cpu.fixready( t ); // make processes ready at time t active
cpu.check( t ); // do the work of the simulation
if( cpu.anyactive() ) // display the number of the active
cout << "|" << cpu.getcurrent(); // processes for the purpose of a trac
else
cout << "|---";
timer.tick(); // update the clock
}
t = timer.gettime(); // read clock
cout << "|";
cpu.endsim(t); // update last finish time, a crude fix
cout << endl << "-------------------------
cout << endl << endl;
cout << "-------------------------
cout << "Simulation Results " << endl;
cout << "-------------------------
cpu.printlist();
int ll;
cin >> ll; // display final results
cout << "-------------------------
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: babuno5Posted on 2004-12-15 at 19:14:03ID: 12836984
hello this is my algo for SJF Premptive
].arrivalt ime)
.arrivalti me)
tocomplete ) t"<<d[i].s tartedtime <<"\t\t"<< d[i].finis hedtime<<" \t\t"<<d[i ].waitingt ime<<"\t\t "<<d[i].ta t<<"\n";
ocessid; arrivaltim e; te-1;i++) tartedtime +count; ++) artedtime- pro[i].arr ivaltime; me-pro[i]. arrivaltim e; ocessid; arrivaltim e; te-1;i++) tartedtime +count; ].processi d) ++) ; artedtime- pro[i].arr ivaltime; me-pro[i]. arrivaltim e;
time+15,20 0,30*p1[n- 1].finishe dtime+15,3 00); ltime+15,3 05,buffer) ; +15,320,15 *pro[i].ar rivaltime+ 30,325); +15,320,15 *pro[i].ar rivaltime- 3,325); +15,320,15 *pro[i].ar rivaltime+ 15,350); [i+1].arri valtime) ro[i+2].ar rivaltime) ltime+15,3 60+7*j,buf fer); ltime+15,3 60,buffer) ;
+15,200,30 *p1[i].fin ishedtime+ 15,300); dtime+15,3 05,buffer) ; time+20,25 0,buffer);
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include<iostream.h>
#include<conio.h>
struct input
{
int processid;
int arrivaltime;
int tocomplete;
};
struct output
{
int processid;
int startedtime;
int finishedtime;
int waitingtime;
int tat;
};
input pro[19],pro1[19],pro2[19];
int n,a[67];
int equality(input a[35],int n,int start)
{
for(int i=start+1;i<=n-1;i++)
if(a[i-1].arrivaltime==a[i
continue;
else
return 1;
return 0;
}
void sort1(input b[50],int n)
{
int i ,j,sorted=0;
input u;
i=0;sorted=0;
while((i<=n-2)&&(!sorted))
{
sorted=1;
for(j=1;j<=n-1-i;j++)
if(b[j-1].arrivaltime>b[j]
{
u=b[j-1];
b[j-1]=b[j];
b[j]=u;
sorted=0;
}
i++;
}
}
void sort2(input c[50],int n)
{
int i ,j,sorted=0;
input t;
i=0;sorted=0;
while((i<=n-2)&&(!sorted))
{
sorted=1;
for(j=1;j<=n-1-i;j++)
if(c[j-1].tocomplete>c[j].
{
t=c[j-1];
c[j-1]=c[j];
c[j]=t;
sorted=0;
}
i++;
}
}
void display(output d[45],int n)
{
cout<<"PROCESSID\tSTART TIME\tFINISH TIME\tWAITING TIME\tTA TIME\n";
for(int i=0;i<=n-1;i++)
cout<<d[i].processid<<"\t\
}
void main()
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
output p1[19];
char buffer[80];
int flag=0;
int count,check=0;
clrscr();
cout<<"enter how many processes: ";
cin>>n;
for(int i=0;i<=n-1;i++)
{
cout<<"enter the process id:: ";
cin>>pro[i].processid;
cout<<"enter the arrival time of process id = "<<i+1<<":: ";
cin>>pro[i].arrivaltime;
cout<<"enter the time required to complete the process "<<i+1<<":: ";
cin>>pro[i].tocomplete;
}
cout<<"\n\n\n";
for(i=0 ;i <=n-1;i++)
pro1[i]=pro[i];
for(i=0 ;i <=n-1;i++)
pro2[i]=pro[i];
sort1(pro1,n);
sort2(pro2,n);
check=equality(pro1,n,0);
if(check==0)
{
count=pro2[0].arrivaltime;
p1[0].processid=pro2[0].pr
p1[0].startedtime=pro2[0].
for(i=0;i<pro2[0].tocomple
count++;
p1[0].finishedtime=p1[0].s
for(i=1;i<=n-1;i++)
{
p1[i].processid =pro2[i].processid;
p1[i].startedtime =count;
for(int j=0;j<pro2[i].tocomplete;j
count++;
p1[i].finishedtime=count;
}
for(i=0;i<=n-1;i++)
{
p1[i].waitingtime=p1[i].st
p1[i].tat=p1[i].finishedti
}
}
else
{
check=equality(pro1,n,1);
if(check==1)
{
count=pro1[0].arrivaltime;
p1[0].processid=pro1[0].pr
p1[0].startedtime=pro1[0].
for(i=0;i<pro1[0].tocomple
count++;
p1[0].finishedtime=p1[0].s
for(i=0;i<=n-1;i++)
{
if(pro2[i].processid==p1[0
continue;
else{
p1[i+1].processid =pro2[i].processid;
p1[i+1].startedtime =count;
for(int j=0;j<pro2[i].tocomplete;j
count++;
p1[i+1].finishedtime=count
}}
for(i=0;i<=n-1;i++)
{
p1[i].waitingtime=p1[i].st
p1[i].tat=p1[i].finishedti
}
}
}
display(p1,n);
cout<<"\n";
getch();
clrscr();
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "C:/tc/bgi");
/* draw a rectangle */
rectangle(15*p1[0].started
for( i=0;i <=n-1;i++)
{
//print the time
sprintf(buffer, "%d",pro[i].arrivaltime);
outtextxy(15*pro[i].arriva
//makes the arrow
line(15*pro[i].arrivaltime
line(15*pro[i].arrivaltime
//makes the horizontal line
line(15*pro[i].arrivaltime
if(pro[i].arrivaltime==pro
{ if(pro[i+1].arrivaltime==p
flag=1;
for(int j=0;j<=n-1;j++)
{
sprintf(buffer, "P%d",pro[j].processid);
outtextxy(15*pro[j].arriva
}
}
else
{
if(flag!=1)
{
//write the process
sprintf(buffer, "P%d",pro[i].processid);
outtextxy(15*pro[i].arriva
}
}
}
for( i=0;i <=n-1;i++)
{ //draw the inside line
line(30*p1[i].finishedtime
//print the time
sprintf(buffer, "%d",p1[i].finishedtime);
outtextxy(30*p1[i].finishe
//write the process
sprintf(buffer, "P%d",p1[i].processid);
outtextxy(30*p1[i].started
}
getch();
closegraph();
}