Homework Help!! Priority Queue ??

Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
Reply

Join Date: Feb 2002
Posts: 12,035
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 128
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Homework Help!! Priority Queue ??

 
0
  #1
Aug 26th, 2003
Please help me! Some homework help appreciated here, thanks!

I'm trying to simulate a server (e.g. a cpu) which accepts requests (e.g. processes). All requests (theoretically) come in at the same time. Each request consists of the amount of cpu time it needs in addition to a priority on a scale of 1-5 or 1-10 or something to the like. I need a function which calculates a Priority Rating (PR) as well as a "penalty" which manipulates these two variables. My server's queue is then ordered by the PR to decrease the amount of cpu time while still handling priority. In other words, some form of a penalty must be in place the longer high priority processes have to wait in the queue.

Does anyone have any ideas of some general algorithm I can consider? Or somewhere I could go for some help on this. My entire program is written except for this one function:

// THIS IS A DUMMY FUNCTION
// AN APPROPRIATE ALGORITHM WILL BE DETERMINED IN THE FUTURE
int calc_pr(int cpu_time, int request_priority)
{
return (cpu_time * request_priority);
}

Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,035
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 128
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Homework Help!! Priority Queue ??

 
0
  #2
Aug 26th, 2003
If anyone's curious, here's what my lil fake server does as of now My goal is to have a more robust way of queueing processes.
Attached Files
File Type: zip Unix File Server.zip (41.9 KB, 48 views)
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 1
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: Re: Homework Help!! Priority Queue ??

 
0
  #3
Aug 26th, 2003
Originally Posted by cscgal
If anyone's curious, here's what my lil fake server does as of now My goal is to have a more robust way of queueing processes.

Are you allowed to use the Standard Template Library or do you need to implement your own heap with heap sort? Do you know how heap sort works?
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,035
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 128
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Homework Help!! Priority Queue ??

 
0
  #4
Aug 26th, 2003
Because processes are added to the queue in random places, based on their PR, they're stored via a binary tree. One of my original implementations of this is available at http://www.daniweb.com/forums/thread895.html although it's gone through a few lives since then. (BTW thanks for responding to that thread, too, subtronic).

In any case, my binary queue is sorted based on PR. I've already implemented an inorder traversal function which touches each process from lower PR to higher PR. In addition, I'm currently working on something like a "reheapify" function which can be called at any time, and it "fixes" any inconsistencies in the binary tree should the PR of a process be changed.

Basically what I'm looking for is a nice algorithm to calculate PR given cpu_time, request_priority, and wait_time (how long the server must wait in the queue). In other words, should cpu_time be weighted higher than the priority or vice versa?
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 1
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: Re: Homework Help!! Priority Queue ??

 
0
  #5
Aug 27th, 2003
Originally Posted by cscgal
Basically what I'm looking for is a nice algorithm to calculate PR given cpu_time, request_priority, and wait_time (how long the server must wait in the queue). In other words, should cpu_time be weighted higher than the priority or vice versa?
So let's assume PR isn't important right now and let's assume that cpu_time is how things are sorted in the heap, is this a MIN or MAX heap is my first question? Are the jobs that take least time executed first or last? Either way, assuming this is our ownly qualifier, there could be a potential for starvation of a job (this depends, in your case, in which order you add new jobs and complete existing jobs (which a processor would consider an interupt)). This question of which should go first is a really nice question because it really depends on what you're going to do (most processors multiplex using time and will process each job a little at a time). Since I'm assuming you're having to complete a job and then move on to the next, space multiplexing is the only viable option you have. This being the case a nice thing to have is PR. In this case you can specifically set a higher priority which can minimize the likelyhood of starvation or optimally guarantee that a job runs. What I might do to determine priority so that all jobs regardless of cpu_time will fit into the queue and be less likely starved is determine a line of best fit (just off the top of my head).. this will give you the minimal error (which can be used as your priority).. Remember your linear algebra ? I'm assuming your data will look near linear as you won't have negative times or priorities.
Last edited by subtronic; Aug 27th, 2003 at 2:51 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,035
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 128
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Homework Help!! Priority Queue ??

 
0
  #6
Aug 30th, 2003
Subtronic, I really do appreciate all your help. I'm going to read your response and get back to you as soon as I can. Just give me a day or two. Unfortunately I came down with a really, really horrible cold and can't even think about programming right now.
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Oct 2003
Posts: 16
Reputation: Wiecek5 is an unknown quantity at this point 
Solved Threads: 0
Wiecek5 Wiecek5 is offline Offline
Newbie Poster

Re: Homework Help!! Priority Queue ??

 
0
  #7
Oct 27th, 2003
Just out of curiousity what class is this ?
As well as do you have to error check? Like for invalid input such as .3 or A.
You want your scheme to go like
Time <-shorter longer-> 1-10
PR <-BEST WORST-> 1-10

A quick easy way is to add the priority with the time length. You will have values ranging from 2 till 20.
2 would be priority of 1 and time of 1. Which i would assume you would want to be first processed. the last item to be processed would be one that has a pr of 10 and a time of 10. You could also make the value of the priority worth more if you want. I can help with that too. Also are you going to constantly be having values come in? if so i think i have a way to deal with that and make sure that
"Beware of computer programmers that carry screwdrivers."
Leonard Brandwein.
Reply With Quote Quick reply to this message  
Join Date: Oct 2003
Posts: 16
Reputation: Wiecek5 is an unknown quantity at this point 
Solved Threads: 0
Wiecek5 Wiecek5 is offline Offline
Newbie Poster

Re: Homework Help!! Priority Queue ??

 
0
  #8
Oct 27th, 2003
your processes will not starve. sorry bout the second post.
"Beware of computer programmers that carry screwdrivers."
Leonard Brandwein.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Computer Science Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC