daily pastebin goal
34%
SHARE
TWEET

Untitled

a guest May 16th, 2018 108 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Reader
  3.  */
  4. int cur_id;
  5. int cur_num;
  6.  
  7. sem wait_for_writer;
  8. sem wait_for_reader;
  9.  
  10. void reader(const int id)
  11. {
  12.     while (1)
  13.     {
  14.         P(wait_for_reader);
  15.        
  16.         // Wait for writer to write and release again (we don't actually need it i guess)
  17.         P(wait_for_writer);
  18.         V(wait_for_writer);
  19.        
  20.         // Do stuff
  21.         if (id == cur_id)
  22.         {
  23.             printf( "%d", cur_num );
  24.         }
  25.        
  26.         // Release reader lock so the writer knows we're done
  27.         V(wait_for_reader);
  28.     }
  29. }
  30.  
  31.  
  32. /*
  33.  * Writer
  34.  */
  35.  
  36. void writer( )
  37. {
  38.     while (1)
  39.     {
  40.         P(wait_for_writer);
  41.         cur_id = get_rand_id( );
  42.         cur_num = get_rand_num( );
  43.        
  44.         V(wait_for_writer);
  45.         P(wait_for_reader);
  46.         V(wait_for_reader);
  47.     }
  48. }
RAW Paste Data
Top