Classical%20Synchronization%20Problems - PowerPoint PPT Presentation

About This Presentation
Title:

Classical%20Synchronization%20Problems

Description:

Classical Synchronization Problems – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 9
Provided by: Kristofer6
Category:

less

Transcript and Presenter's Notes

Title: Classical%20Synchronization%20Problems


1
Classical Synchronization Problems
  • CS 537 - Introduction to Operating Systems

2
Classical Problem 1
  • Producer-Consumer

3
  • include ltstdio.hgt
  • include ltsemaphore.hgt
  • define SIZE 50
  • define NUM_THREADS 5
  • struct object buffer
  • sem_t mutex, empty, full
  • void nap()
  • int sleepTime (int)(5 random())
  • sleep(sleepTime)
  • void enter(struct object item)
  • empty.down()
  • mutex.down()
  • count
  • bufferin item

4
  • void producer()
  • struct object newObj
  • for()
  • nap()
  • newObj getObject()
  • enter(newObj)
  • void consumer()
  • struct object item
  • for()
  • nap()
  • item remove()
  • useItem(item)
  • int main(int argc, char argv)

5
Classical Problem 2
  • Readers-Writers

6
Readers-Writers
  • Several threads accessing a database
  • Some threads read the database (readers)
  • Some threads write the database (writers)
  • Multiple readers can be accessing database at a
    time if no writers accessing it
  • Only one thread can be accessing database if it
    is a writer
  • 2 major variations
  • no reader waits if no writer in database
  • writers can starve
  • all readers wait if a writer is waiting
  • writer has to wait until current reader (or
    writer) is finished with database
  • readers may starve
  • Many more variations to deal with starvation
    issues
  • Following example is for the first variant
  • starvation is disregarded

7
  • include ltstdio.hgt
  • include ltpthreads.hgt
  • define NUM_THREADS 5
  • sem_t mutex, database
  • void reader()
  • while(true)
  • nap()
  • startRead()
  • nap()
  • endRead()
  • void writer()
  • while(true)
  • nap()

8
  • void startRead()
  • mutex.down()
  • readerCount
  • if(readerCount 1) database.down()
  • mutex.up()
  • void endRead()
  • mutex.down()
  • readerCount--
  • if(readerCount 0) database.up()
  • mutex.up()
  • void startWrite() database.down()
  • void endWrite() database.up()
  • int main()
  • pthread_t threadsNUM_THREADS
Write a Comment
User Comments (0)
About PowerShow.com