home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / KRNLDEMO.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  150 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. ** public domain demo of cooperative multitasking using function pointers
  5. ** written 29 Nov, 1992
  6. ** by David Gersic
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <conio.h>
  13.  
  14. #define MAX_TASKS 32                /* maximum number of runnable tasks */
  15.  
  16. /* global variables */
  17.  
  18. void (*tasks[MAX_TASKS])(void);     /* pointer to list of tasks to run  */
  19.  
  20. /* prototypes */
  21.  
  22. int  schedule(void (*fp)(void));    /* add function to the task list    */
  23. int  mark(void (*fp)(void));        /* mark task in list                */
  24. int  kill(void (*fp)(void));        /* remove function from task list   */
  25. void dispatcher(void);              /* dispatch a task                  */
  26. int  set_up(void);                  /* initialization code              */
  27. int  terminate(void);               /* shutdown code                    */
  28.  
  29. int main(void)
  30. {
  31.       void walk_the_dog(void);
  32.       void floss_the_cat(void);
  33.       void make_the_donuts(void);
  34.  
  35.       if(set_up())
  36.       {
  37.             fputs("Initialization failed. Exiting.\n",stderr);
  38.             exit(1);
  39.       }
  40.  
  41.       if(schedule(walk_the_dog))
  42.             fputs("Could not add new function to task list.\n",stderr);
  43.       if(schedule(floss_the_cat))
  44.             fputs("Could not add new function to task list.\n",stderr);
  45.       if(schedule(make_the_donuts))
  46.             fputs("Could not add new function to task list.\n",stderr);
  47.  
  48.       while(!kbhit())  /* run 'till key hit */
  49.             dispatcher();
  50.  
  51.       if(terminate())
  52.       {
  53.             fputs("Error while shutting down.\n",stderr);
  54.             exit(1);
  55.       }
  56.       return(0);
  57. }
  58.  
  59. /*
  60. ** initialize task buffer and any other 'internal' setup code
  61. */
  62.  
  63. int set_up(void)
  64. {
  65.       memset(tasks, 0x00, sizeof(tasks));
  66.       return(0);
  67. }
  68.  
  69. /*
  70. ** kill any tasks still in the table and shut down
  71. */
  72.  
  73. int terminate(void)
  74. {
  75.       int i;
  76.  
  77.       for(i = 0; i < MAX_TASKS; i++)
  78.       {
  79.             if (tasks[i])
  80.                   kill(tasks[i]);
  81.       }
  82.       return(0);
  83. }
  84.  
  85. /*
  86. ** dispatch the next runnable task
  87. */
  88.  
  89. void dispatcher(void)
  90. {
  91.       static int i = 0;
  92.  
  93.       if(tasks[i])
  94.             tasks[i]();
  95.       i = (i + 1) % MAX_TASKS;
  96. }
  97.  
  98. /*
  99. ** add task to the list of runnable tasks
  100. */
  101.  
  102. int schedule(void (*fp)(void))
  103. {
  104.       int i;
  105.  
  106.       for (i = 0; i < MAX_TASKS && tasks[i]; i++)
  107.             ;                       /* find a open slot in the table    */
  108.       if (i < MAX_TASKS)            /* if not end of table              */
  109.             tasks[i] = fp;          /* add task to list                 */
  110.       else  return(1);
  111.       return(0);
  112. }
  113.  
  114. /*
  115. ** remove a task from the list of runnable tasks
  116. */
  117.  
  118. int kill(void (*fp)(void))
  119. {
  120.       int i;
  121.  
  122.       for (i = 0; i < MAX_TASKS; i++)
  123.       {
  124.             if (tasks[i] == fp)
  125.             {
  126.                   tasks[i] = NULL;
  127.                   return(0);
  128.             }
  129.       }
  130.       return(1);
  131. }
  132.  
  133. void walk_the_dog(void)
  134. {
  135.       puts("dog");
  136.       return;
  137. }
  138.  
  139. void floss_the_cat(void)
  140. {
  141.       puts("cat");
  142.       return;
  143. }
  144.  
  145. void make_the_donuts(void)
  146. {
  147.       puts("donuts");
  148.       return;
  149. }
  150.