Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Simulating CPU Scheduling

Posted on 2003-12-04
4
Medium Priority
?
2,172 Views
Last Modified: 2012-05-04
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.
0
Comment
Question by:sscrowell
  • 2
4 Comments
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9879950
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
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9879984
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
 
LVL 41

Accepted Solution

by:
Kyle Abrahams earned 2000 total points
ID: 9885258
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
 
LVL 5

Expert Comment

by:migoEX
ID: 10443432
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:

Accept dhyanesh's comment as answer.

Please leave any comments here within the next four days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

migoEX
EE Cleanup Volunteer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question