home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_12 / 9n12027a < prev    next >
Text File  |  1991-10-15  |  2KB  |  91 lines

  1.  
  2. /* --------------------------------------------------------------
  3.  
  4. FUNCTION ADD_NODE: The steps to add a new node to the list in
  5.     ascending alphabetic order are:
  6.  
  7. Note that process_name_table is a lookup table that allows a
  8. user-entered process name to be translated to the corresponding
  9. processing function.
  10.  
  11. A. Get the process name and priority from the user.
  12.  
  13. B. Get a free node from free node list. If no more, complain and
  14.    get out.
  15.  
  16. C. Find place to insert new node. The pointer returned from
  17.    locate_node is the address of the node after which the new 
  18.    node should be inserted.
  19.  
  20. D. Insert new node and initialize its members. Since the list is 
  21.    maintained in decreasing order of priority, we need to update
  22.    the root pointer when a new higher priority process node
  23.    arrives. If the new node's priority equals to the current 
  24.    highest priority, the new node is inserted ahead of that
  25.    duplicate.
  26.  
  27. -------------------------------------------------------------- */
  28.  
  29. void add_node(void)
  30. {
  31.     int i;
  32.     Node *pnew_node;    /* ptr to new node */
  33.     Node *ploc_node;    /* ptr to located node */
  34.     char process_name[21];    /* selected process name */
  35.     unsigned priority;    /* function priority */
  36.     static const struct {
  37.         char *ppname;
  38.         void (*pprocess)(unsigned);
  39.     } process_name_table[] = {
  40.         {"pro1", pro1},
  41.         {"pro2", pro2},
  42.         {"pro3", pro3}
  43.     };
  44.     static unsigned highest_priority = 0;
  45.         /*A*/    while (1) {
  46.         printf("\n  Enter process name: ");
  47.         scanf("%20s", process_name);
  48.     
  49.         for (i = 0; i < NUMELEM(process_name_table); ++i) {
  50.         if (strcmp(process_name_table[i].ppname,
  51.               process_name) == 0)
  52.                 goto found;
  53.         }
  54.     
  55.         printf("invalid process name\n");
  56.     }
  57.     
  58. found:    while (1) {
  59.         printf("\n  Enter process priority: ");
  60.         scanf("%3u", &priority);
  61.         if (priority > 0 && priority <= 100) {
  62.             break;
  63.         }
  64.  
  65.         printf("1<=priority<=100\n");
  66.     }
  67.  
  68. /*B*/    pnew_node = get_free_node();
  69.     if (pnew_node == NULL) {
  70.         printf("\n  No nodes available\n");
  71.         return;
  72.     }
  73.  
  74. /*C*/    ploc_node = locate_node(priority, INEXACT);
  75.  
  76. /*D*/    pnew_node->pfwd = ploc_node->pfwd;
  77.     pnew_node->pbwd = ploc_node;
  78.     ploc_node->pfwd = pnew_node;
  79.     pnew_node->pfwd->pbwd = pnew_node;
  80.     pnew_node->process = process_name_table[i].pprocess;
  81.     pnew_node->priority = priority;
  82.     if (priority >= highest_priority) {
  83.         proot_node = pnew_node;
  84.         highest_priority = priority;
  85.     }
  86.  
  87.     printf("  Node added\n");
  88. }
  89.  
  90.  
  91.