Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <pthread.h>
  4.  
  5. #define N 5
  6.  
  7. int readcount, writecount;
  8. int reader_num[N] = {0,1,2,3,4};
  9. pthread_mutex_t the_mutex, db, read, readcnt, writecnt;
  10.  
  11. void read_db(int i)
  12. {
  13.     //pthread_mutex_lock(
  14.     printf("reader %d reading DB\n", i);
  15.     usleep(10000);
  16. }
  17.  
  18. void write_data()
  19. {
  20.     printf("writer writing to the DB\n");
  21.     usleep(10000);
  22. }
  23.  
  24. void * reader(void * num)
  25. {
  26.     int i = (int)num;
  27.     while (1)
  28.     {
  29.         pthread_mutex_lock(&the_mutex);
  30.         pthread_mutex_lock(&read);
  31.         pthread_mutex_lock(&readcnt);
  32.        
  33.         readcount++;
  34.        
  35.         if ( readcount == 1 )
  36.             pthread_mutex_lock(&db);
  37.        
  38.         pthread_mutex_unlock(&readcnt);
  39.         pthread_mutex_unlock(&read);
  40.         pthread_mutex_unlock(&the_mutex);
  41.        
  42.         read_db(i);
  43.        
  44.         pthread_mutex_lock(&the_mutex);
  45.         pthread_mutex_lock(&readcnt);
  46.        
  47.         readcount--;
  48.         if ( readcount == 0 )
  49.             pthread_mutex_unlock(&db);
  50.        
  51.         pthread_mutex_lock(&readcnt);
  52.         pthread_mutex_unlock(&the_mutex);  
  53.        
  54.     }// end while
  55. }// end reader
  56.  
  57. void * writer()
  58. {
  59.     while (1)
  60.     {
  61.         pthread_mutex_lock(&db);
  62.         pthread_mutex_lock(&writecnt);
  63.        
  64.         writecount++;
  65.         if ( writecount == 1 )
  66.             pthread_mutex_lock(&read);
  67.        
  68.         pthread_mutex_unlock(&writecnt);
  69.            
  70.        
  71.         write_data();
  72.        
  73.        
  74.         pthread_mutex_lock(&writecnt);
  75.        
  76.         writecount--;
  77.         if ( writecount == 0 )
  78.             pthread_mutex_unlock(&read);
  79.        
  80.         pthread_mutex_unlock(&writecnt);
  81.         pthread_mutex_unlock(&db);
  82.        
  83.     }//end while
  84. }//end writer
  85.  
  86. int main()
  87. {  
  88.     int i;
  89.     readcount = 0; writecount = 0;
  90.     pthread_mutex_init(&the_mutex, 0);
  91.     pthread_mutex_init(&db, 0);
  92.     pthread_mutex_init(&read, 0);
  93.     pthread_mutex_init(&readcnt, 0);
  94.     pthread_mutex_init(&writecnt, 0);
  95.    
  96.     pthread_t writer_id, reader_id[N];
  97.    
  98.     pthread_create(&writer_id, 0, writer, 0);
  99.    
  100.     for ( i = 0; i < N; i++ )
  101.         pthread_create(&reader_id[i], 0, reader, (void *)reader_num[i]);
  102.        
  103.     pthread_join(writer_id, NULL);
  104.     for ( i = 0; i < N; i++ )
  105.         pthread_join(reader_id[i], NULL);
  106.    
  107.    
  108.        
  109.     pthread_mutex_destroy(&the_mutex);
  110.     pthread_mutex_destroy(&db);
  111.     pthread_mutex_destroy(&read);
  112.     pthread_mutex_destroy(&readcnt);
  113.     pthread_mutex_destroy(&writecnt);
  114.    
  115.     return 0;          
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement