Friday, December 3, 2010

Producer Consumer Problem (unbounded) - Semaphores

Problem:
 producer () {
    while(true) {
        produce()
        append()
     }
}
Consumer() {
     while(true) {
         take()
         consume()
    }
}
In multithreading this may cause problem:
Solution:

semaphore mutex=1
semaphore full=0
producer() {
       while(true) {
          produce()
          mutex.wait()
          append()
          mutex.signal()
          full.signal() //increment full
      }
}
consumer () {
     while(true) {
         full.wait()  // while full is not filled by producer, decrement full
         mutex.wait()  // mutex to give signal
         take()
         mutex.signal()
         consume()
     }
}






 
 



1 comment:

  1. Thanks for offering us such a useful details. Keep up the great work. You can also check out this awarded Vitamin supplement store for nutritional supplements from leading brands.
    Nutritional Supplements

    ReplyDelete