Quick explanation: the Bounded-Buffer problem Binary Wisdom https://www.youtube.com/watch?v=LRiN3DJdskA Transkript (automatisch erstellt) 0:00 hello in this video we will discuss the producer/consumer synchronization problem in some sources this problem is also called the bounded buffer problem 0:10 so far we have only discussed the critical section problem but this problem is only one of many well-known synchronization problems when 0:20 programming you can come across other synchronization scenarios other synchronization problems include the diamond philosophers problem the 0:28 cigarette smokers problem the readers writers problem and sleeping barber problem all these problems are usually discussed in real-life metaphors but 0:39 each one describes a parallel programming scenario that requires a special technique to synchronize processes or threads so don't be 0:48 surprised when we discuss philosophers readers or barbers all of them adjust convenient real-life allegories for processes and threads that use shared 0:59 resources and programmers use the solutions to these synchronization problems when creating programs in its simplest form the producer-consumer 1:10 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 1:21 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 1:31 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 1:43 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 1:55 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 2:06 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 2:15 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 2:27 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 2:38 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 2:49 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 3:00 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 3:12 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 3:25 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 3:36 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 3:48 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 3:58 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 4:07 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 4:18 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 4:30 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 4:41 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 4:51 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 5:01 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 5:11 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 5:22 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 5:31 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 5:42 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 5:52 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 6:03 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 6:14 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 6:24 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 6:34 that's it that's one way to solve the producer consumer problem so the producer-consumer problem says 6:43 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 6:54 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 7:06 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 7:16 problem by using two semaphores that's it thank you for being with us you