Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- main()
- {
- printf("Hello World");
- //spinlock names
- //wmutex
- //readtry
- //resource
- //rmutex
- //rentry
- //
- //Favoring Writers
- //Writer
- writerSolution2{
- //<Entry Section>
- spinlock_lock(&wmutex); //set mutex lock to avoid race conditions on incrementing writer
- writers++; //increment writer
- if(writers == 1)
- spinlock_lock(&readtry); //lock readtry if this is the first writer
- spinlock_unlock(&wmutex); //unlock wmutex
- spinlock_lock(&resource); //lock resource and goto critical
- //CRIT SECTION
- //<Exit Section>
- spinlock_unlock(&resource); //unlock resource on critical section
- spinlock_lock(&wmutex); //lock wmutex to decrement writers
- writers--;
- if (writers == 0) //if that was the last writer
- spinlock_unlock(&readtry); //unlock readtry, make accessible to readers
- spinlock_unlock(&wmutex); //unlock wmutex
- }
- //Reader
- readerSolution2{
- //entry section
- spinlock_lock(&rentry); //lock rentry
- spinlock_lock(&readtry);//lock readtry
- spinlock_lock(&rmutex);//lock wmutex all 3 locked
- readers++; //increment readers
- if(readers == 1)
- spinlock_lock(resource); //lock rescource if that was the first reader
- spinlock_unlock(&rmutex); //unlock mutex
- spinlock_unlock(&readtry); //unlock readtry
- spinlock_unlock(&rentry); //unlock rentry after all 3 unlocked go to crit section
- //critical section
- //exit section
- spinlock_lock(&rmutex); //lock mutex for decrementing readers
- readers--; //decrement readers
- if (readers == 0) //if that was the last reader
- spinlock_unlock(&resource); //unlock resource for the writers
- spinlock_unlock(&rmutex); //unlock rmutex
- }
- //mutex
- //mutex1
- //read
- //write
- //
- readerSolution3{
- //entry section
- spinlock_lock(&mutex); // lock mutex
- if ((writers > 0) &&(readers == 0)){ //check if there are writers and if there are 0 readers
- spinlock_unlock(&mutex); //if so we unlock mutex
- spinlock_lock(&resource); //and lock resource as well as mutex again
- spinlock_lock(&mutex);
- }
- readers++; //increment readers
- spinlock_unlock(mutex); //unlock mutex (this was to avoid race conditions on readers when incrementing)
- //critical section
- //exit section
- spinlock_lock(&mutex); //lock mutex
- readers--; //decrement readers
- if (readers == 0) //if there are no remaining readers
- spinlock_unlock(&resource); //unlock the resource for other writers
- spinlock_unlock(&mutex); //unlock mutex (this was for race conditions when decrementing readers
- }
- writerSolution3{
- //entry section
- spinlock_lock(&mutex); //lock mutex for race conditions on mutex
- writers++; //increment writers
- spinlock_unlock(&mutex); //unlock mutex
- spinlock_lock(&resource); //lock resource
- //critical section
- //exit section
- spinlock_lock(&mutex); //lock mutex
- writers--; //decrement writers
- spinlock_unlock(&mutex); //unlock mutex
- spinlock_unlock(&resource); //unlock resource
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement