home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_09 / 1009062b < prev    next >
Text File  |  1992-06-30  |  2KB  |  164 lines

  1. /* 
  2.     Listing 2
  3. */
  4.  
  5. #include <stdio.h>
  6. #include "enet_cs.h"
  7.  
  8. long     critical_status=NORMAL;
  9.  
  10. int i, j, node, turn_num, flag_num;
  11.  
  12. /* Program by Matt Weisfeld */
  13.  
  14. main(argc,argv)
  15. int argc;
  16. char **argv;
  17. {
  18.  
  19.  
  20.     fprintf (stderr,"%sENET_CS%s(1.0)\n", bold,norm);
  21.  
  22.     if (argc != 2)  {
  23.         printf ("Error: bad number of args.\n");
  24.         exit(0);
  25.     }
  26.  
  27.     node = atoi(argv[1]);
  28.  
  29.     intro();    /* print out informational messages */
  30.  
  31.     i = node;
  32.  
  33.     /* perform critical function until exit condition exists */
  34.  
  35.     while (critical_status == NORMAL) {
  36.         perform();
  37.     }
  38.  
  39.        return;
  40.  
  41.  
  42. }
  43.  
  44. perform()
  45. {
  46.  
  47.     /*
  48.       A do-while (!expression) is the same as
  49.         do-until (expression)
  50.     */
  51.  
  52.     do {
  53.  
  54.         cs_write ( i, WANT_IN);
  55.  
  56.         turn_num = cs_read ( TURN_LOC);
  57.  
  58.         j = turn_num;
  59.  
  60.         while (j != i) {
  61.  
  62.             flag_num = cs_read ( j);
  63.  
  64.             if (flag_num != IDLE) {
  65.  
  66.                 turn_num = cs_read ( TURN_LOC);
  67.  
  68.                 j = turn_num;
  69.  
  70.             } else {
  71.             
  72.  
  73.                 j = (j+1) % PROCESSES;
  74.  
  75.  
  76.             }
  77.  
  78.         }
  79.         
  80.         cs_write ( i, IN_CS);
  81.     
  82.         
  83.         j = 0;
  84.  
  85.         flag_num = cs_read ( j);
  86.  
  87.         while ( (j<PROCESSES-1) && (j==i || flag_num!=IN_CS) ) {
  88.     
  89.             j++;
  90.  
  91.             flag_num = cs_read ( j);
  92.  
  93.         }
  94.  
  95.         turn_num = cs_read ( TURN_LOC);
  96.  
  97.         flag_num = cs_read ( turn_num);
  98.  
  99.     }while (!((j>=PROCESSES-1) && ((turn_num == i) || flag_num == IDLE)));
  100.  
  101.     turn_num = i;
  102.  
  103.     cs_write ( TURN_LOC, turn_num);
  104.  
  105.  
  106.     /* enter critical section */
  107.  
  108.     critical_status = critical_section();
  109.  
  110.     /* leave critical section */
  111.  
  112.     turn_num = cs_read ( TURN_LOC);
  113.  
  114.     j = (turn_num+1) % PROCESSES;
  115.  
  116.     flag_num = cs_read ( j);
  117.  
  118.     while (flag_num == IDLE) {
  119.  
  120.         j = (j+1) % PROCESSES;
  121.  
  122.         flag_num = cs_read ( j);
  123.  
  124.     }
  125.  
  126.     turn_num = j;
  127.  
  128.     cs_write ( TURN_LOC, turn_num);
  129.  
  130.     flag_num = IDLE;
  131.  
  132.     cs_write ( i, flag_num);
  133.  
  134. }
  135.  
  136.  
  137. intro()
  138. {
  139.  
  140.     switch (node) {
  141.  
  142.         case NODE0:
  143.             printf ("PROCESSOR NODE0\n");
  144.         break;
  145.  
  146.         case NODE1:
  147.             printf ("PROCESSOR NODE1\n");
  148.         break;
  149.  
  150.         case NODE2:
  151.             printf ("PROCESSOR NODE2\n");
  152.         break;
  153.  
  154.         default:
  155.             printf ("Error: invalid processor\n");
  156.             exit(0);
  157.         break;
  158.             
  159.  
  160.     }
  161.  
  162. }
  163.  
  164.