473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to implement round robin in an application?

pradeepjain
563 512MB
I have a project where a patient comes to a hospital for an appointment and he must be allocated a doctor from a set of doctors based on round robin logic. How would i do this? and what is the over all concept of round robin?
Mar 3 '10 #1

✓ answered by Atli

how do i build the doctor queue ?
A simple array containing the info on each doctor would do.
Like:
Expand|Select|Wrap|Line Numbers
  1. $doctors = array(
  2.     array(
  3.         'name' => 'Greg',
  4.         'patients' => array()
  5.     ),
  6.     array(
  7.         'name' => 'Meredith',
  8.         'patients' => array()
  9.     )
  10.     // etc...
  11. );
loop through each doctors slots ?
The "slots" array in the above array would be the list of filled slots. You would loop through that to find out which patients the doctor is mean to see.

say at a give time there are 20 patients and 5 doctors , how do i make sure that each doctor has 4 patients . bcos it must be divided equally . Its bit confusing for me .
Code based on the pseudo-code I posted earlier would do that.

You could use a foreach loop to loop through the patients. Inside that loop you would add the patient to the next doctor in the queue. To figure out which doctor is next, just create a variable that is incremented on every loop and reset it when it exceeds the number of doctors.
Again, in pseudo-code:
Expand|Select|Wrap|Line Numbers
  1. doctorIndex = 0
  2. For each patient:
  3.     // Add the client to the slots for the
  4.     // doctor at the current index
  5.     doctors[doctorIndex]["slots"][] = patient
  6.  
  7.     // Increment the doctor index, so the next
  8.     // doctor will be used on the next loop.
  9.     doctorIndex = doctorIndex + 1
  10.  
  11.     // Make sure the index doesn't go higher than
  12.     // the total amount of doctors.
  13.     If doctorIndex > count(doctors):
  14.         doctorIndex = 0

5 7579
Atli
5,058 Expert 4TB
Hey.

As I understand it, Round Robin is used for CPU scheduling, where each process is allocated CPU time in turn, with no process taking priority.

For a doctor application, I would use a FIFO (first in, first out) approach. It's the most "natural" approach. - That is; while there are more patients then there are doctors, each patient would be assigned a doctor as soon as one became available. The first patient to come in would - naturally - be the first to be assigned a doctor. - But while there are more doctors then there are patients, the first doctor to become free would be assigned the next patient to come in. Once a doctor becomes available, he is put at the end of the queue and could take a break while the doctors in front of him/her in the line are assigned the incoming patients, until it is his/her turn.

... Come to think about it, that is probably how the the "Round-Robin" CPU scheduling is implemented as well.
Mar 3 '10 #2
pradeepjain
563 512MB
but each doctor has multiple slots where in he sees a patient like every 15mins is a slot for a doctor. like this he will have around say 150 slots in a day,this condition has also to be taken care of.
Mar 3 '10 #3
Atli
5,058 Expert 4TB
Ok, so each doctor only has a limited amount of time per patient? - That makes this even easier to schedule. You just go through the doctors in a loop, assigning the next patient to the next available slot for the next doctor in the loop.

I mean, it could be written like this, in pseudo-code:
Expand|Select|Wrap|Line Numbers
  1. patient = a stream of incomming patients
  2. doctor = a queue of doctors, each with a set number of slots
  3.  
  4. When a new patient arrives:
  5.     If next doctor in the queue has an available slot:
  6.         assign the patient to next available slot
  7.     Else
  8.         put patient on tomorrows waiting list
Note that there is no need to check the other doctors for open slots if the current doctor doesn't have one. Given that all the doctors have the same amount of slots, that situation would never present itself.

P.S.
150 slots per day at 15 minutes each = 37,5 hours... Hard-working doctors you have over there xD
Mar 3 '10 #4
pradeepjain
563 512MB
how do i build the doctor queue ?

loop through each doctors slots ?

say at a give time there are 20 patients and 5 doctors , how do i make sure that each doctor has 4 patients . bcos it must be divided equally . Its bit confusing for me .
Mar 3 '10 #5
Atli
5,058 Expert 4TB
how do i build the doctor queue ?
A simple array containing the info on each doctor would do.
Like:
Expand|Select|Wrap|Line Numbers
  1. $doctors = array(
  2.     array(
  3.         'name' => 'Greg',
  4.         'patients' => array()
  5.     ),
  6.     array(
  7.         'name' => 'Meredith',
  8.         'patients' => array()
  9.     )
  10.     // etc...
  11. );
loop through each doctors slots ?
The "slots" array in the above array would be the list of filled slots. You would loop through that to find out which patients the doctor is mean to see.

say at a give time there are 20 patients and 5 doctors , how do i make sure that each doctor has 4 patients . bcos it must be divided equally . Its bit confusing for me .
Code based on the pseudo-code I posted earlier would do that.

You could use a foreach loop to loop through the patients. Inside that loop you would add the patient to the next doctor in the queue. To figure out which doctor is next, just create a variable that is incremented on every loop and reset it when it exceeds the number of doctors.
Again, in pseudo-code:
Expand|Select|Wrap|Line Numbers
  1. doctorIndex = 0
  2. For each patient:
  3.     // Add the client to the slots for the
  4.     // doctor at the current index
  5.     doctors[doctorIndex]["slots"][] = patient
  6.  
  7.     // Increment the doctor index, so the next
  8.     // doctor will be used on the next loop.
  9.     doctorIndex = doctorIndex + 1
  10.  
  11.     // Make sure the index doesn't go higher than
  12.     // the total amount of doctors.
  13.     If doctorIndex > count(doctors):
  14.         doctorIndex = 0
Mar 3 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jerry Khoo | last post by:
i am currently working c++ project about executing a round robin algorrithm, how do i control when the algorithm should stop, what is the condition for the loop to stop, what is the condition to...
7
by: William Stacey [MVP] | last post by:
I can think of a couple ways to do this, but was wonder what the fastest way you can think of. Want to take any arraylist of > 1 element and make the first element the last, and all the rest of...
1
by: Ioannis Vranos | last post by:
I was checking .NET multithreading lately, and my book mentions that the thread scheduler provides quantoms of a time to each thread in "round robin" fashion. Is there any on line reference...
1
by: Scott | last post by:
Hi All. I have a web site that is currently using DNS round robin as a poor man's load balancer. Some users are receiving an 'Unable to Validate Data' error when posting back to the server. ...
5
by: Marc | last post by:
Hi, I cannot get the round function to work on vb.net. I get the message that round is not declared? Has round function changed or something? MsgBox(round(3, 3))
6
by: rhitx | last post by:
Can somebody please give like an overview how to do a round robin scheduler.. I know that we're suppose to use a quantum/quanta? Is this going to be like an int, then assign with with a value? ...
3
kirara
by: kirara | last post by:
Hi all, I am trying to develop a mechanism to know which machine can handle a user job (by providing his Requirements) for now I wrote this method but it is a Round robin, I am trying to modify it...
2
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
how do I code generic functions to return the next item in an enumeration a) sorted by name, b) sorted by value c) sorted by declaration in a round-robin style ? for example the enum is Enum...
6
by: Zeng | last post by:
Math.Round has good behavior as following: Math.Round(3.45, 1); //Returns 3.4. The last '5' is thrown away because 4 is even Math.Round(3.75, 1); //Returns 3.8. The last '5' is used because '7'...
5
by: 08butoryr | last post by:
Hi everyone! this should be pretty simple for programmers on this forum... I need to design a java program which interacts with a MS Access DB in order to simulate the progression of a soccer round...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.