From 2249f8166e859b675c636dfc213d9d5d73eff501 Mon Sep 17 00:00:00 2001 From: Jacob Date: Wed, 23 Oct 2019 14:00:39 -0400 Subject: [PATCH] Starting new semaphores post --- .gitmodules | 2 +- .../post/multiprocessing-and-semaphores.md | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 content/post/multiprocessing-and-semaphores.md diff --git a/.gitmodules b/.gitmodules index 2992b12..06bea7c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "themes/blackburn"] path = themes/blackburn - url = https://github.com/jaketothepast/blackburn + url = https://github.com/yoshiharuyamashita/blackburn diff --git a/content/post/multiprocessing-and-semaphores.md b/content/post/multiprocessing-and-semaphores.md new file mode 100644 index 0000000..928938c --- /dev/null +++ b/content/post/multiprocessing-and-semaphores.md @@ -0,0 +1,26 @@ +--- +title: "Multiprocessing and Semaphores" +date: 2019-10-23T13:23:11-04:00 +draft: true +--- + +Sometimes in my line of work, I have to work on C code. In particular, one of my company's key applications is an embedded system that gathers transaction data from stores for transmission to our cloud data pipeline. One of the mechanisms we use for reporting errors within our heartbeat protocol, is to write error messages to a Unix named-pipe. That pipe is read by a separate process which then adds those error messages to a message that is packaged and sent to our cloud to be analyzed. + +One of the many issues we've come across with this approach, is if the pipe is continuously written to by one process, it is unavailable for writing by another process that may be having legitimate issues. Our solution to throttle writes to the pipe was to use semaphores. + +## What's a semaphore? + +A semaphore is essentially a counter. When the counter is above 0, everything is fine and dandy. When the counter is equal to 0, we have to wait before we are able to do anything. Semaphores traditionally have two operations as part of thei API: `signal` and `wait`. + +### wait + +When we call wait on a semaphore, a few things happen. First, we check the count of the semaphore. If the count is 0, we are blocked right there and have to stop doing any work, waiting for the count to rise above 0. If the count is not 0, we will decrement the count and be on our merry way. + +### signal + +When we call signal on a semaphore, we increment the count of the semaphore. What happens when we increment the semaphore is the next process in line that got stuck waiting for the count to rise above 0 will get to start up, and decrement the count - likely back to 0 - causing more processes to wait until it's finished. + +## Named Semaphores + +## The approach +