diff --git a/Concurrent Systems/notes/4 - Semaphores.md b/Concurrent Systems/notes/4 - Semaphores.md index be9bc6e..d5c6c07 100644 --- a/Concurrent Systems/notes/4 - Semaphores.md +++ b/Concurrent Systems/notes/4 - Semaphores.md @@ -93,4 +93,23 @@ B.consume() := > Reading from / writing into the buffer can be very expensive! #### (Multiple) Producers/Consumers -**Accessing BUF in MUTEX slows down the implementation** \ No newline at end of file +**Accessing BUF in MUTEX slows down the implementation**, we would like to have the possibility to read and write from different cells **in parallel**. +- we use two arrays FULL and EMPTY of atomic boolean registers, initialized at ff (all zeros) and tt (all ones), respectively +- we have two extra semaphores SP and SC, both initialized at 1 + +``` +B.produce(v) := + FREE.down() + SP.down() + while not EMPTY[in] do + IN <- (IN+1) mod k + i <- IN + EMPTY[IN] <- ff + SP.up() + BUF[i] <- v + FULL[i] <- tt + BUSY.up() + return + + +``` \ No newline at end of file