How to implement round robin in an application?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pradeepjain
    Contributor
    • Jul 2007
    • 563

    How to implement round robin in an application?

    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?
    Last edited by Niheel; May 7 '10, 06:52 PM. Reason: spelling, grammar, punctuation
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    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.

    Comment

    • pradeepjain
      Contributor
      • Jul 2007
      • 563

      #3
      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.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        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:
        [code=text]patient = a stream of incomming patients
        doctor = a queue of doctors, each with a set number of slots

        When a new patient arrives:
        If next doctor in the queue has an available slot:
        assign the patient to next available slot
        Else
        put patient on tomorrows waiting list[/code]
        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

        Comment

        • pradeepjain
          Contributor
          • Jul 2007
          • 563

          #5
          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 .

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            how do i build the doctor queue ?
            A simple array containing the info on each doctor would do.
            Like:
            [code=php]$doctors = array(
            array(
            'name' => 'Greg',
            'patients' => array()
            ),
            array(
            'name' => 'Meredith',
            'patients' => array()
            )
            // etc...
            );[/code]

            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:
            [code=pseudo]doctorIndex = 0
            For each patient:
            // Add the client to the slots for the
            // doctor at the current index
            doctors[doctorIndex]["slots"][] = patient

            // Increment the doctor index, so the next
            // doctor will be used on the next loop.
            doctorIndex = doctorIndex + 1

            // Make sure the index doesn't go higher than
            // the total amount of doctors.
            If doctorIndex > count(doctors):
            doctorIndex = 0[/code]

            Comment

            Working...