Zum Inhalt springen
L

Das Video kommt von YouTube: erst beim Abspielen verbindet sich die Seite mit YouTube (Google).

Quick explanation: the Bounded-Buffer problem

Binary Wisdom7:33 45.770 Aufrufe veröffentlicht Auf YouTube

Das Wichtigste aus dem Video

Tipp auf eine Zeit – das Video springt genau dorthin.

Transkriptautomatisch erstellt · 42 Zeilen
Herunterladen
  1. hello in this video we will discuss the producer/consumer synchronization problem in some sources this problem is also called the bounded buffer problem
  2. so far we have only discussed the critical section problem but this problem is only one of many well-known synchronization problems when
  3. programming you can come across other synchronization scenarios other synchronization problems include the diamond philosophers problem the
  4. cigarette smokers problem the readers writers problem and sleeping barber problem all these problems are usually discussed in real-life metaphors but
  5. each one describes a parallel programming scenario that requires a special technique to synchronize processes or threads so don't be
  6. surprised when we discuss philosophers readers or barbers all of them adjust convenient real-life allegories for processes and threads that use shared
  7. resources and programmers use the solutions to these synchronization problems when creating programs in its simplest form the producer-consumer
  8. problem looks like this the producer produces items one by one and places them in a buffer that can hold a limited predetermined number of items the
  9. consumer takes items from this buffer one by one and consumes them in a more complex version of this problem there could be multiple producers and multiple
  10. consumers but we will stick with the initial simple version as we said before both the producer and consumer are in fact the processes or threads for
  11. example the producer thread generates some frames or webpages and the consumer thread shows them on screen or sends them via a network the buffer that they
  12. use to exchange these data items is in fact the shared memory that can hold a certain number of items we must arrange the producer in such a way that it waits
  13. for an empty space in the buffer if the buffer is full on the other hand if the buffer is empty there is nothing to consume the consumer
  14. should wait there are different ways to solve this problem one of the classic ways to do so is by using a couple of semaphores if you are not quite sure how
  15. semaphores work you can refresh this material in our semaphores video on the screen you can see the solution for the producer and consumer processes both
  16. processes can see first of all a shared buffer or fixed size the buffer on our illustration is big enough to store up to four items also both processes can
  17. see the semaphore full and the semaphore empty the semaphore full shows the number of items in the buffer and in the beginning it is initialized to zero
  18. because there are no items yet the semaphore empty shows the number of empty spaces in the buffer and in the beginning all four spaces are free so we
  19. initialize the semaphore to four let's take a look at the producer first it produces an item and then it wants to place it in the buffer it can place this
  20. new item in the buffer only if there is at least one empty space as we have just said the number of empty buffer spaces is indicated by the value of the empty
  21. semaphore so the producer waits for the semaphore empty if empty is greater than zero it means that there is a place for a new item it allows the producer to
  22. proceed and place the new item in the buffer by doing so the producer decreases the number of empty spaces in the buffer by one and that is already
  23. taken care of because the weight function automatically decreases the value of semaphore by one on the other hand if the producer waits for the
  24. semaphore empty and empty equals zero that means that there is no place in the buffer for the new item as you will remember if a process or thread waits
  25. for a semaphore that is zero or less they are placed in an inactive waiting state that is just what we here if empty equals zero the producer
  26. will just enter a waiting State and resume only when the consumer signals empty showing that an empty space has appeared in the buffer okay now what
  27. should the producer do after it places the new item in the buffer because placing the new item not only decreases the number of empty spaces in the buffer
  28. but also increases the number of items there the producer must increase the value of the semaphore full this can be done by signaling the semaphore full
  29. signaling fool does one more important thing if the buffer is empty and the consumer is waiting for an item to appear signaling fool will wake the
  30. consumer up when the new item appears in the buffer once again the producer makes a new item then it checks the availability of an
  31. empty space in the buffer if there is no empty space the producer waits for one when an empty space is available the producer decreases the number of empty
  32. spaces by one and proceeds to place the new item in the buffer once the item is in the buffer the producer increases the count of items in the buffer and wakes
  33. up the consumer in case the consumer is waiting for a new item to become available the consumer functions in a similar way but it waits and signals
  34. different semaphores see before taking an item from the buffer the consumer waits for the same for full it will put the consumer in the inactive waiting
  35. state if full is zero and there are no items to take but if some items are in the buffer the wait command decreases their number by one and this is right
  36. because the consumer will take one item from the buffer once the consumer takes one item from the buffer it must increase the number of empty spaces in
  37. the buffer and wake the producer up if it is waiting for an empty space to appear both of these things can be done with a single signal empty command
  38. that's it that's one way to solve the producer consumer problem so the producer-consumer problem says
  39. that a producer produces items one by one and places them in a buffer and the consumer takes items from the buffer one by one we must arrange both the producer
  40. and the consumer in such a way that if the buffer is full the producer doesn't try to place more items in it but just waits for an empty space to appear if
  41. the buffer is empty the consumer doesn't try to take an item from it but just waits for a new item to appear we showed how to solve the producer consumer
  42. problem by using two semaphores that's it thank you for being with us you

Zum Nachlesen