Advertisement
Guest User

shit

a guest
Dec 9th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.95 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. main()
  4. {
  5.     printf("Hello World");
  6.  
  7.    
  8. //spinlock names
  9.  
  10. //wmutex
  11. //readtry
  12. //resource 
  13. //rmutex
  14. //rentry
  15. //
  16.    
  17.    
  18. //Favoring Writers
  19.    
  20.    
  21.    
  22.    
  23. //Writer   
  24.  
  25. writerSolution2{
  26.     //<Entry Section>
  27.    
  28.     spinlock_lock(&wmutex); //set mutex lock to avoid race conditions on incrementing writer
  29.     writers++; //increment writer
  30.     if(writers == 1)
  31.         spinlock_lock(&readtry); //lock readtry if this is the first writer
  32.     spinlock_unlock(&wmutex); //unlock wmutex
  33.     spinlock_lock(&resource); //lock resource and goto critical
  34.    
  35.    
  36.     //CRIT SECTION
  37.    
  38.    
  39.     //<Exit Section>
  40.     spinlock_unlock(&resource); //unlock resource on critical section
  41.     spinlock_lock(&wmutex); //lock wmutex to decrement writers
  42.     writers--;
  43.     if (writers == 0) //if that was the last writer
  44.         spinlock_unlock(&readtry); //unlock readtry, make accessible to readers
  45.     spinlock_unlock(&wmutex); //unlock wmutex
  46.    
  47. }
  48. //Reader
  49. readerSolution2{
  50.    
  51.     //entry section
  52.     spinlock_lock(&rentry); //lock rentry
  53.     spinlock_lock(&readtry);//lock readtry
  54.     spinlock_lock(&rmutex);//lock wmutex all 3 locked
  55.     readers++; //increment readers
  56.     if(readers == 1)
  57.         spinlock_lock(resource); //lock rescource if that was the first reader
  58.     spinlock_unlock(&rmutex); //unlock mutex
  59.     spinlock_unlock(&readtry); //unlock readtry
  60.     spinlock_unlock(&rentry); //unlock rentry after all 3 unlocked go to crit section
  61.    
  62.     //critical section
  63.    
  64.    
  65.     //exit section
  66.     spinlock_lock(&rmutex); //lock mutex for decrementing readers
  67.     readers--; //decrement readers
  68.     if (readers == 0) //if that was the last reader
  69.         spinlock_unlock(&resource); //unlock resource for the writers
  70.     spinlock_unlock(&rmutex); //unlock rmutex
  71.    
  72. }
  73.    
  74. //mutex
  75. //mutex1
  76. //read
  77. //write
  78. //
  79.    
  80. readerSolution3{
  81.    
  82.     //entry section
  83.    
  84.     spinlock_lock(&mutex); // lock mutex   
  85.     if ((writers > 0) &&(readers == 0)){ //check if there are writers and if there are 0 readers
  86.         spinlock_unlock(&mutex); //if so we unlock mutex
  87.         spinlock_lock(&resource); //and lock resource as well as mutex again
  88.         spinlock_lock(&mutex);
  89.     }
  90.     readers++; //increment readers
  91.     spinlock_unlock(mutex); //unlock mutex (this was to avoid race conditions on readers when incrementing)
  92.    
  93.     //critical section
  94.    
  95.    
  96.     //exit section
  97.     spinlock_lock(&mutex); //lock mutex
  98.     readers--; //decrement readers
  99.     if (readers == 0) //if there are no remaining readers
  100.         spinlock_unlock(&resource); //unlock the resource for other writers
  101.     spinlock_unlock(&mutex); //unlock mutex (this was for race conditions when decrementing readers
  102.    
  103. }
  104.  
  105. writerSolution3{
  106.    
  107.     //entry section
  108.     spinlock_lock(&mutex); //lock mutex for race conditions on mutex
  109.     writers++; //increment writers
  110.     spinlock_unlock(&mutex); //unlock mutex
  111.     spinlock_lock(&resource); //lock resource
  112.    
  113.     //critical section
  114.    
  115.     //exit section
  116.     spinlock_lock(&mutex); //lock mutex
  117.     writers--; //decrement writers
  118.     spinlock_unlock(&mutex); //unlock mutex
  119.     spinlock_unlock(&resource); //unlock resource
  120.    
  121.    
  122.    
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement