*You have given a structure which has two member, One which stores the
time and other stores the function pointer Your function has to call the *
*function stored in the fuction poitner after the time given in the
structure elapses.
Design that function? *

Approach: To design this function I would use a min Heap data structure.
Each node of a heap has
                two parameters one is the running time and other one is the
function pointer.

// Initialise a function pointer
typedef void (*functionToBeCalled)(int arg1, int arg2);

// Timer structure
typedef struct timer
{
   float runingTime;   // in terms of seconds
   functionToBeCalled funcToBeCall; // function pointer
}TIMER;

void initTimer()
{
   Initialise few nodes with running time and its corresponding function
   Initialise a MIN heap data structure
}

void addTimer(uint32 runingTime, functionToBeCalled func)
{
     TIMER *temp;
      temp = (TIMER *)malloc(sizeof(TIMER));
      temp->runingTime = runningTime
      temp->funcToBeCall = func;
      HeapAdd(temp);
      Heapify();
}

void scheduler()
{
            uint32 currentTime = ObtainCurrentTime();
            // Obtain the runing time of top most element of the min Heap
            uint32 runingTime = PeakHeap();
            // if the runningTime is equal to current time then extract the
top most
            // element of the heap and execute the function associate with
it
            // Heapify the MIN heap data structure
            // Obtain the runing time of top most element of the min heap
            // scheduler sleep for that much amount of time.
            if(runingTime == currentTime)
            {
                 TIMER * node = ExtractMinHeap();
                  CreateThread(node->func, Thread);
                  Heapify();
                  runingTime = PeakHeap();
                 sleep(runningTime);
            }
            else
            {
               // scheduler updates its sleep time
              // if runing time is not equal to current time
              sleep(runningTime);
            }

 }

Let me know your comments

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to