Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.39 KB | None | 0 0
  1. /*
  2.  * catsem.c
  3.  *
  4.  * Please use SEMAPHORES to solve the cat syncronization problem in
  5.  * this file.
  6.  */
  7.  
  8.  
  9. /*
  10.  *
  11.  * Includes
  12.  *
  13.  */
  14.  
  15. #include <types.h>
  16. #include <lib.h>
  17. #include <test.h>
  18. #include <thread.h>
  19. #include "catmouse.h"
  20.  
  21. // added this
  22. #include <synch.h>
  23.  
  24. // access controls
  25. struct semaphore *bowl_check;
  26. struct semaphore *bowl_access;
  27.  
  28. // is the bowl being eaten from (1 means yes, 0 means no)
  29. //int bowl1 = 1;
  30. //int bowl2 = 1;
  31.  
  32. // counters to keep track of cat and mouse
  33.  
  34. //int catCount = 0;
  35. //int mouseCount = 0;
  36.  
  37. struct type{
  38.         int cat;
  39.         int mouse;
  40.         int empty;
  41.        
  42. };  
  43.  
  44. struct type bowl1, bowl2;
  45. /*
  46.  *
  47.  * Function Definitions
  48.  *
  49.  */
  50.  
  51. /*
  52.  * catsem()
  53.  *
  54.  * Arguments:
  55.  *      void * unusedpointer: currently unused.
  56.  *      unsigned long catnumber: holds the cat identifier from 0 to NCATS - 1.
  57.  *
  58.  * Returns:
  59.  *      nothing.
  60.  *
  61.  * Notes:
  62.  *      Write and comment this function using semaphores.
  63.  *
  64.  */
  65.  
  66. static
  67. void
  68. catsem(void * unusedpointer,
  69.        unsigned long catnumber)
  70. {
  71.         /*
  72.          * Avoid unused variable warnings.
  73.          */
  74.  
  75. int bowl;
  76.         /*
  77.          * a thread was created/forked from catmousesem
  78.          * now let this thread try and access the food/semaphore
  79.          * if it can't (2 threads already eating), sleep
  80.          * else if a mouse thread is eating, sleep
  81.         */
  82.  
  83.         int meal = 0;
  84.         // while the thread has not eaten all its meals yet
  85.         while ( meal < NMEALS) {
  86.                 // If there are 2 things eating, it won't be able to acquire the other semaphore
  87.                 P(bowl_access);
  88.                 P(bowl_check);
  89.                 // if the mouse is eatiing the cat cant eat              
  90.                 if ((bowl1.mouse == 1)||(bowl2.mouse == 1)){
  91.                         meal--;
  92.                         V(bowl_check);
  93.                         V(bowl_access);
  94.                         continue;
  95.                 }
  96.                 //catCount++;
  97.                 assert (bowl1.mouse == 0 && bowl2.mouse == 0)
  98.                // if (catCount == 1){
  99.                       //  P(cat_or_mouse);
  100.                         // critical region
  101.                         if (bowl1.cat == 1) {
  102.                                 bowl2.cat = 1;
  103.                                 catmouse_eat("cat", catnumber, 2, meal);
  104.                                // bowl2 = 1;
  105.                                bowl = 2;
  106.                         }
  107.                         else {
  108.                                 bowl1.cat = 1;
  109.                                 catmouse_eat("cat", catnumber, 1, meal);
  110.                                 //bowl1 = 1; // bowl1 now occupied
  111.                                  bowl = 1;
  112.                         }
  113.                         meal++;
  114.                         V(bowl_check);
  115.                // }
  116.                
  117.                 P(bowl_check);
  118.                // catCount--;
  119.                // if(catCount == 0)// all cats finished eating
  120.                  //       V(cat_or_mouse);
  121.                 if(bowl == 1){
  122.                         bowl1.empty =1;
  123.                         bowl1.cat = 0;
  124.                 }        
  125.                 else {
  126.                         bowl2.empty = 1;
  127.                         bowl2.cat = 0;
  128.                 }
  129.                 V(bowl_check);
  130.                 V(bowl_access);
  131.         }
  132.  
  133.  
  134.         (void) unusedpointer;
  135.         (void) catnumber;
  136. }
  137.        
  138.  
  139. /*
  140.  * mousesem()
  141.  *
  142.  * Arguments:
  143.  *      void * unusedpointer: currently unused.
  144.  *      unsigned long mousenumber: holds the mouse identifier from 0 to
  145.  *              NMICE - 1.
  146.  *
  147.  * Returns:
  148.  *      nothing.
  149.  *
  150.  * Notes:
  151.  *      Write and comment this function using semaphores.
  152.  *
  153.  */
  154.  
  155. static
  156. void
  157. mousesem(void * unusedpointer,
  158.          unsigned long mousenumber)
  159. {
  160.         /*
  161.          * Avoid unused variable warnings.
  162.          */
  163. int bowl = 0;
  164.         // if no one is there, go ahead
  165.         // or if there is one mouse eating, go ahead
  166.         // if there is one cat eating, wait
  167.         // or if there are two things eating (no matter what they are), wait
  168.  
  169.         int meal = 0;
  170.  
  171.         // while the thread has not eaten all its meals yet
  172.         while (meal < NMEALS) {
  173.                  // If there are 2 things eating, it won't be able to acquire the other semaphore
  174.                 P(bowl_access);
  175.                 P(bowl_check);
  176.  
  177.                 if ((bowl1.cat == 1) || (bowl2.cat == 1)){
  178.                         meal--;
  179.                         V(bowl_check);
  180.                         V(bowl_access);
  181.                         continue;
  182.                 }        
  183.                 // If a cat is eating, it won't be able to acquire the semaphore
  184.               //  mouseCount++;
  185.                  assert (bowl1.cat == 0 && bowl2.cat == 0)
  186.                // if (mouseCount == 1){
  187.                        
  188.                  //       P(cat_or_mouse);
  189.                        
  190.                         // critical region
  191.                        
  192.                         if (bowl1.mouse == 1) {
  193.                                 bowl2.mouse = 1;
  194.                                 catmouse_eat("mouse", mousenumber, 2, meal);
  195.                                 bowl = 2;
  196.                         }
  197.                         else  {
  198.                                 bowl1.mouse =1;
  199.                                 catmouse_eat("mouse", mousenumber, 1, meal);
  200.                                 bowl = 1; // bowl1 now occupied
  201.                         }
  202.                         meal++;
  203.                         V(bowl_check);
  204.                 //}
  205.                
  206.                 P(bowl_check);
  207.                
  208.               //  mouseCount--;
  209.                
  210.               //  if(mouseCount == 0)// all cats finished eating
  211.                 //        V(cat_or_mouse);
  212.                
  213.                 if(bowl == 1){
  214.                         bowl1.empty = 1;
  215.                         bowl1.mouse = 0;
  216.                 }
  217.                 else{
  218.                         bowl2.empty = 1;
  219.                         bowl2.mouse = 0;
  220.                 }
  221.                
  222.                 V(bowl_check);
  223.                 V(bowl_access);
  224.         }
  225.  
  226.         (void) unusedpointer;
  227.         (void) mousenumber;
  228. }
  229.  
  230.  
  231. /*
  232.  * catmousesem()
  233.  *
  234.  * Arguments:
  235.  *      int nargs: unused.
  236.  *      char ** args: unused.
  237.  *
  238.  * Returns:
  239.  *      0 on success.
  240.  *
  241.  * Notes:
  242.  *      Driver code to start up catsem() and mousesem() threads.  Change this
  243.  *      code as necessary for your solution.
  244.  */
  245.  
  246. int
  247. catmousesem(int nargs,
  248.             char ** args)
  249. {
  250.         int index, error;
  251.         bowl_check = sem_create("bowl_check", 2);
  252.         bowl_access = sem_create("bowl_access", 1);
  253.  
  254.  
  255.         /*
  256.          * Start NCATS catsem() threads.
  257.          */
  258.  
  259.         for (index = 0; index < NCATS; index++) {
  260.            
  261.                 error = thread_fork("catsem Thread",
  262.                                     NULL,
  263.                                     index,
  264.                                     catsem,
  265.                                     NULL
  266.                                     );
  267.                
  268.                 /*
  269.                  * panic() on error.
  270.                  */
  271.  
  272.                 if (error) {
  273.                  
  274.                         panic("catsem: thread_fork failed: %s\n",
  275.                               strerror(error)
  276.                               );
  277.                 }
  278.         }
  279.        
  280.         /*
  281.          * Start NMICE mousesem() threads.
  282.          */
  283.  
  284.         for (index = 0; index < NMICE; index++) {
  285.    
  286.                 error = thread_fork("mousesem Thread",
  287.                                     NULL,
  288.                                     index,
  289.                                     mousesem,
  290.                                     NULL
  291.                                     );
  292.                
  293.                 /*
  294.                  * panic() on error.
  295.                  */
  296.  
  297.                 if (error) {
  298.          
  299.                         panic("mousesem: thread_fork failed: %s\n",
  300.                               strerror(error)
  301.                               );
  302.                 }
  303.         }
  304.  
  305.         /*
  306.          * wait until all other threads finish
  307.          */
  308.  
  309.         while (thread_count() > 1)
  310.                 thread_yield();
  311.  
  312.         (void)nargs;
  313.         (void)args;
  314.         kprintf("catsem test done\n");
  315.  
  316.         return 0;
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement