# Simulating CPU Scheduling

I am attempting to write a program that simulates the First Come First Serve, Shortest Job Next, and Shortest Remaining Time scheduling algorithms.  For each algorithm I want to print, in the order of completion, the finishing time, wait time, and turnaround time for each process.  I am also going to display the average waiting and turnaround times for each algorithm.

The number of processes, arrival time, and CPU time is gathered from the user.  The arrival time and CPU time is stored into a two dimensional array.

I have the FCFS algorithm coded and working, but am having problems figuring out an algorithm for the SRT and SJN.  Any help would be appreciated.
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Hi

Shortest Job Next :
------------------------

This a non preemptive algorithm. You have to maintain a queue of jobs as they arrive in time. When a job arrives it should be placed in the queue or array whatever you use in a sorted order. The jobs will be stored in ascending order of CPU time required for the job.

Then as a job is over the first job in the array is picked and executed. However just before this check for an incoming job at the same time first and put it in array in sorted order first. Then only take the first job from the queue.

Dhyanesh
0
Commented:
Shortest Remaining Time :
---------------------------------

This is a pre-emptive algorithm. It is similar to shortest job next algorithm. In this again you maintain a queue or array in sorted order. However the only difference is that when a job arrives say having CPU time 't1' and the job currently executing has CPU time 't2' REMAINING i.e. it requires 't2' time to complete.

Now if t1<t2 then the job currently executing is removed from CPU and newly arrived job starts executing. The removed job is placed in sorted order in the queue.

However if t1>=t2 then the newly arrived job is simply put in sorted order in the queue as in Shortest Job Next.

Dhyanesh
0
Senior .Net DeveloperCommented:
pseudo code:

SJN();
while ( no_jobs() == false)
{
sort_job_list_by_time();

do
{
job();
} while (job_not_finished() == true);

}

SRT:

while (no_jobs()==false)
{
sort_job_list_by_time();
job(); //ONLY 1 TICK
}

When I was doing this I used a vector, and used jobs[0] as the one to be executed by the CPU.    The difference between SRT and SJN is that SRT will re-sort everytime, while SJN will wait until the job is completed, get the next one, and then continue.  Hope this helps.

0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is: