<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Richard Ramsden]]></title>
  <link href="http://rramsden.ca/atom.xml" rel="self"/>
  <link href="http://rramsden.ca/"/>
  <updated>2013-05-15T18:02:55+09:00</updated>
  <id>http://rramsden.ca/</id>
  <author>
    <name><![CDATA[Richard Ramsden]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Life in Japan]]></title>
    <link href="http://rramsden.ca/blog/2013/05/15/life-in-japan/"/>
    <updated>2013-05-15T17:59:00+09:00</updated>
    <id>http://rramsden.ca/blog/2013/05/15/life-in-japan</id>
    <content type="html"><![CDATA[<p>Testing stuff</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Appmon not working with rebar]]></title>
    <link href="http://rramsden.ca/blog/2012/07/10/appmon-not-working-with-rebar/"/>
    <updated>2012-07-10T21:12:00+09:00</updated>
    <id>http://rramsden.ca/blog/2012/07/10/appmon-not-working-with-rebar</id>
    <content type="html"><![CDATA[<p>I recently setup my development environment using rebar. However, my two favorite debugging
modules weren&#8217;t getting bundled into my development release <code>appmon:start()</code> and <code>tv:start()</code> were
returning undefined. If you want to enable tv and appmon you will need to bundle them into
your rebar release. You can do this by editing your reltool.config in rebar. Like so:</p>

<pre><code>{rel, "appname", "1",
[
  application1,
  application2,
  ....
  ....
  {tv, load}
  {appmon, load}
]}
</code></pre>

<p>Build your release and appmon and tv should be available :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[There and back again]]></title>
    <link href="http://rramsden.ca/blog/2012/06/29/there-and-back-again/"/>
    <updated>2012-06-29T22:40:00+09:00</updated>
    <id>http://rramsden.ca/blog/2012/06/29/there-and-back-again</id>
    <content type="html"><![CDATA[<p>When my boss asked me if I wanted to take a break from the Ruby on Rails world and
work with him on our Erlang backend I was pretty stoked. Here was a chance to try out a new
language, a functional one, something different than anything I&#8217;ve encountered before.
So that day I decided to put down my web developer hat for a bit and that week started cramming everything Erlang.</p>

<p>My first impression of Erlang wasn&#8217;t all that good. Looking at its syntax made me cringe a bit. Coming from
a background in Ruby I was use to reading code as if it was written in plain old english.
This wasn&#8217;t the case with Erlang. Staring at my first bit of Erlang code I had no clue what the heck was going on.
However, I pushed on. About a week in I started familiarizing myself with the syntax. And you know what?
Despite what everyone says I think the syntax is just fine. Try spending a bit of time learning Erlang
and you&#8217;ll realize it isn&#8217;t half-bad.</p>

<p>That being said, the syntax in Erlang wasn&#8217;t the thing bothering me. It was because working in a functional language
meant I would have to change the way I think.</p>

<h2>To understand recursion, you must understand recursion</h2>

<p>I remember a conversation I had with one of my Computer Science professors about recursion. She told me
that although recursion is totally awesome its usually frowned upon when you try and sneak it into production code.
She told me about her experience at IBM during her internship. In short, her co-workers weren&#8217;t too happy about her <em>creative</em> solutions.</p>

<p>Working in a functional language means you can&#8217;t avoid recursion. Its everywhere. Functional languages
impose a share-nothing architecture. This means everything is immutable. In order to modify something you would have
to create a new data structure. To prevent your code from looking like crap you would have to use recusive functions to build lists and
other data structures.</p>

<h2>Erlang vs Ruby</h2>

<p>The great thing about Ruby is it allows you to use Metaprogramming. The horrible thing
about Ruby is it allows you to use Metaprogramming. What I noticed about Erlang is
if I wanted to know how something worked I could open up the source code of some library
and take a peek. Within a few minutes I could grasp what the heck was going on. In Ruby, not quite.
You would have to pray the library was documented well enough to tell you what the heck was going on.
There are times when Metaprogramming helps readability however I often see it being misused by
library authors. Ruby looks innocent on the outside but its a complex beast once you start getting
into more advanced topics.</p>

<h2>Working with Ruby Again</h2>

<p>I started working on hobby projects again in Ruby sometime around April.
Coming back after almost a year of working with a functional programming language was a bit weird.</p>

<p>What I love about Erlang was pattern matching. Writing Ruby code I would always have
a sad face when I ran into cases where pattern matching would DRY up code. You&#8217;ve probably run into
situations where you have to bust out the old case statement to switch on arguments passed into a function.
It looks ugly and leads to your eyes having to jump around different logical paths.</p>

<p>I also noticed my way of thinking has changed. The first thing I think about is how
to write something recursively instead of the other way aroud. Would it be fewer lines of code
and more readable if I wrote this recursively? Most of the time the answer was no. When I did find
oppurtunities to use recusion it simplified hundreds of lines of code :) My X11 library for Ruby
constructs packets which contain sub-packets which was a good chance to use some <a href="https://github.com/rramsden/ruby-x11/blob/master/lib/X11/form.rb">recursion</a>.
Gotta love those moments when you can use recursion to simplify things.</p>

<p>Anyway, I&#8217;ve ranted enough. In retrospect becoming proficient in a functional programming language
has improved my imperative programming style immensly.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Gossiping in Distributed Systems - Part II]]></title>
    <link href="http://rramsden.ca/blog/2012/06/27/gossiping-in-distributed-systems-part-ii/"/>
    <updated>2012-06-27T21:22:00+09:00</updated>
    <id>http://rramsden.ca/blog/2012/06/27/gossiping-in-distributed-systems-part-ii</id>
    <content type="html"><![CDATA[<p>In my <a href="http://rramsden.ca/blog/2012/06/25/gossiping-in-distributed-systems/">last post</a> we talked
about how how gossiping in distributed systems allows us to quickly propogate messages out to nodes.
In this post will look at and implement a very simple Gossip-based algorithm for computing global aggregates
across nodes in a cluster (ie. sum, average, count).</p>

<h2>Σ&#8217;ing things up</h2>

<p>Suppose we want to compute a sum across every node in our cluster that gives us the total number of requests/second
our distributed system is handling. Now we could just send messages periodically from each node in the cluster to
some central point. However, our aim should always be for a fullly decentralized system where nodes don&#8217;t have to rely
on a single machine to calcuate the global estimate.</p>

<p>That being said, were going to use a Gossip-based algorithm called the <strong>Symmetric Push-Sum Protocol</strong> or <strong>SPSP</strong> for short.
Its a simple algorithm for computing aggregates across a cluster.</p>

<h2>Symmetric Push-Sum Protocol (SPSP)</h2>

<p><img src="http://rramsden.ca/images/blog/spsp.png" alt="" /></p>

<p>What does SPSP do for us? Well its a 2-in-1 algorithm since we can compute either a sum or an average with it.
Its also <em>damn simple</em> which is why its a good place to start when talking about Gossiping algorithms.</p>

<p>SPSP works by keeping track of a weight and a value at each node. In our case,
the value would be our request/second service rate at a node. And the weight acts like a percentage telling us
how much of the overall sum we have. Different weight values are defined in the SPSP <a href="http://www.thinkmind.org/download.php?articleid=ap2ps_2011_2_10_30063">whitepaper</a>.
For example, if you wanted to calculate a sum across a cluster you would initialize the weight to be 1 at one node and 0 at the rest. I&#8217;ll explain
why this is in a moment.</p>

<p>In the example below will pretend that nodes A and B are seeing 5 and 10 requests/second. Once our Gossip-based
algorithm is finished we should be able to query either A or B to figure out that the system is seeing in total 15 requests/second.</p>

<p><img src="http://rramsden.ca/images/blog/ex01.png" alt="" /></p>

<p>Once the system has initialzied nodes can start gossiping.</p>

<p><img src="http://rramsden.ca/images/blog/ex02.png" alt="" /></p>

<p>Above I&#8217;ve drawn a little sequence diagram showing SPSP in action:</p>

<ol>
<li>Node A will push half its weight and value to Node B</li>
<li>Node B receives a push message from Node A. Node B will take half its value and weight and send a symmetric push back to the caller Node A</li>
<li>Once Node A receives a symmetric push from Node B it simply adds the received value and weight to its own</li>
<li>Finally Node B will add Node A&#8217;s original push message to its own values</li>
</ol>


<p>Note: this is all completely asynchronous. Theres no need to block between a push and a symmetric push</p>

<h2>When is it finished?!</h2>

<p>You know when the algorithm is finished when the weight values at each node converge to <code>1 / N</code> where <code>N</code> is the number of nodes.
Once you reach convergence you&#8217;re ready to calculate your sum. This part is easy! Take your
value and divide by the weight. In the example above it would be 7.5 / 0.5 = 15</p>

<p>Next Page - <a href="http://rramsden.ca/blog/2012/06/28/gossiping-in-distributed-systems-part-iii/">Erlang Implementation</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[RE: Write Less Code]]></title>
    <link href="http://rramsden.ca/blog/2012/06/26/re-write-less-code/"/>
    <updated>2012-06-26T22:37:00+09:00</updated>
    <id>http://rramsden.ca/blog/2012/06/26/re-write-less-code</id>
    <content type="html"><![CDATA[<p>I was planning on continuing my blog post on Gossip Protocols tonight however an old friend decided to pay
an unexpected visit. Instead, I&#8217;m going to express my thoughts on a post I read titled
<a href="http://mikegrouchy.com/blog/2012/06/write-less-code.html">Write Less Code</a> by Mike Grouchy.</p>

<p>Reading his post made me remember my own evolution as a software developer. Some of the traps he describes I still suffer
from. Often I run into situations where I find myself hacking away at something I haven&#8217;t thought through. What happens?
I end up deleting the code and it never sees production. I&#8217;ve wasted so much energy in the past falling into this trap I still
suffer from it today but slowly learning to avoid it.</p>

<p>I know for sure my boss is a very experienced software developer. He spends an enormous amount of time on research.
This seems weird to me because I actually never see him programming, he&#8217;s constantly reading or whiteboarding things.
When I do see him program or work on something he&#8217;s able to crank out elegant solutions to problems because of
the amount of research and thinking he&#8217;s done beforehand.</p>

<p>I need to remember that <strong>code is a by-product</strong>. Thinking things out before
hacking away seems obvious but I still find myself slipping from time to time.
I&#8217;ll try keeping this in mind and remember that my real job is to think
and write less code.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Gossiping in Distributed Systems - Part I]]></title>
    <link href="http://rramsden.ca/blog/2012/06/25/gossiping-in-distributed-systems/"/>
    <updated>2012-06-25T17:36:00+09:00</updated>
    <id>http://rramsden.ca/blog/2012/06/25/gossiping-in-distributed-systems</id>
    <content type="html"><![CDATA[<p>This will be my first write up since I <a href="http://rramsden.ca/blog/2012/06/23/im-going-to-blog-every-single-day/">foolishly committed</a>
to blogging every single day for an entire year.  So if your just following along I usually pick a topic I don&#8217;t know anything about and
write and try really damn hard to figure it out.  Do I know anything about Gossiping in Distributed Systems? No. Do I ever know
what the hell I&#8217;m doing? No. But I&#8217;m here and I&#8217;ll give it my best shot ;) That being said if you haven&#8217;t closed your browser
yet I&#8217;m going to break this post up into a few blog entries since I probably can&#8217;t become an expert on gossip protocols over night.</p>

<h2>Where do we start?</h2>

<p>Lets start with explaining Gossip communication and what it means in distributed systems.
For example, perhaps a co-worker spreads a nasty rumour about you. Apparently you
haven&#8217;t been testing your code (shame on you). Your colleagues A and B chit chat over lunch and A
mentions your bad habit. Once A and B interact with more people the rumour spreads exponentially faster.
Pretty soon the whole office is glaring at you and you become the scape goat for every single problem
in the office. Eventually this rumour mutates and your boss hears your purposely unfactoring your code and using
pig latin for your method definitions. You end up being fired :(</p>

<p>Similarily, in a distributed system its easy to tell one node a message and have that message
quickly propogate out to a few servers, maybe thousands with amazing speed.
Wikipedia provides an excellent <a href="http://en.wikipedia.org/wiki/Gossip_protocol#Examples">example</a> of a data center
that houses 25,000 servers. A search term is passed to a central point where it is propogated out. Since
one of the machines has the item being searched for its able to receive and send a result back within 3 seconds in the
worst case scenario.</p>

<p>Next Page - <a href="http://rramsden.ca/blog/2012/06/27/gossiping-in-distributed-systems-part-ii/">The Push-Sum Protocol</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Teensy 2.0 USB Problems]]></title>
    <link href="http://rramsden.ca/blog/2011/10/23/teensy-little-problem/"/>
    <updated>2011-10-23T16:06:00+09:00</updated>
    <id>http://rramsden.ca/blog/2011/10/23/teensy-little-problem</id>
    <content type="html"><![CDATA[<p>A few problems I came across playing around with my new Teensy development board. Decided to
make a quick post on some solutions I found. Enjoy :)</p>

<h2>Problem #1</h2>

<p>Teensy USB device wasn&#8217;t being recognized. You can see the list of
registred devices on Ubuntu using <code>lsusb</code></p>

<h2>Solution </h2>

<p>Loaded program code may interfere with the USB registration process.
Use the RESET button then check the output of <code>lsusb</code> again make sure the device
is being recgonized. NOTE: you may have to hold it down and plug the USB device in then release it.</p>

<h2>Problem #2</h2>

<p>If that wasn&#8217;t enough my USB device was being registered under <code>/dev/hidraw0</code> which
isn&#8217;t recognized by the Teensy Loader Application. It only looks at /dev/ttyACM0</p>

<h2>Solution</h2>

<p>When the kernel registers the USB device it doesn’t give regular users write/read
access to the device. You will need to setup appropriate udev rules to do this.
udev rules are available from the Teensy website http://www.pjrc.com/teensy/49-teensy.rules
you will need to download this file and place it in /etc/udev/rules.d/</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[ReviewBoard with Nginx]]></title>
    <link href="http://rramsden.ca/blog/2011/09/26/nginx-reviewboard/"/>
    <updated>2011-09-26T08:50:00+09:00</updated>
    <id>http://rramsden.ca/blog/2011/09/26/nginx-reviewboard</id>
    <content type="html"><![CDATA[<p>This weekend I decided to checkout ReviewBoard a slick Web 2.0 open source Django app for doing code reviews. Unfortunately, it took me quite a bit of time to set it up from scratch with Nginx. Thus, I decided to do a quick writeup for Nginx users.
ReviewBoard&#8217;s setup docs is a good starting point for getting the dependencies you need to setup a ReviewBoard website.</p>

<p>Once you have ReviewBoard installed you can generate a website using <code>rb-site install</code>. I setup my website under /var/www/review.mysite.com but you can set it up anywhere if you prefer another location.
The next step is to launch the FastCGI daemon script bundled with ReviewBoard</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>rb-site manage /var/www/review runfcgi method=threaded port=3033 host=127.0.0.1 protocol=fcgi</span></code></pre></td></tr></table></div></figure>


<p>Once your FastCGI instance is up and running you just need to simply point Nginx at the FastCGI process</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>server {
</span><span class='line'>    listen 80;    
</span><span class='line'>    server_name review.mysite.com;
</span><span class='line'>    root /var/www/review.mysite.com/htdocs/;    
</span><span class='line'>    
</span><span class='line'>    location /media  {     
</span><span class='line'>      root /htdocs/media/;    
</span><span class='line'>    }
</span><span class='line'>   
</span><span class='line'>    location /errordoc {      
</span><span class='line'>      root /htdocs/errordocs/;    
</span><span class='line'>    }
</span><span class='line'>
</span><span class='line'>    location / {      
</span><span class='line'>      # host and port to fastcgi server      
</span><span class='line'>      fastcgi_pass 127.0.0.1:3033;      
</span><span class='line'>      fastcgi_param PATH_INFO $fastcgi_script_name;      
</span><span class='line'>      fastcgi_param REQUEST_METHOD $request_method;      
</span><span class='line'>      fastcgi_param QUERY_STRING $query_string;      
</span><span class='line'>      fastcgi_param CONTENT_TYPE $content_type;      
</span><span class='line'>      fastcgi_param CONTENT_LENGTH $content_length;      
</span><span class='line'>      fastcgi_pass_header Authorization;      
</span><span class='line'>      fastcgi_intercept_errors off;    
</span><span class='line'>    }  
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>


<p>Then restart your nginx server and reviewboard should be up and running. Did I miss anything?</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[CoffeeScript and Sprockets 2.0]]></title>
    <link href="http://rramsden.ca/blog/2011/08/27/coffeescript-and-sprockets/"/>
    <updated>2011-08-27T14:58:00+09:00</updated>
    <id>http://rramsden.ca/blog/2011/08/27/coffeescript-and-sprockets</id>
    <content type="html"><![CDATA[<p>Sprockets makes it really damn easy in a few lines of ruby to concatenate and serve your coffee script files.
It supports multiple template engines ERB, JavaScript, CSS, SCSS, etc you can
even add a dash of ERB &lt;%= %> to your coffeescript source files by naming your file source.coffee.erb and sprockets will automagically compile it
down with ERB then CoffeeScript and concatenate  it with other dependencies!</p>

<p>For the project I was working on I couldn&#8217;t depend on Node.js for dependency management since I was creating a front-end browser game. At first I decided to implement my own hacked up solution in Ruby to substitute and concatenate coffee script source files&#8230;
it wasn&#8217;t pretty :( After moving to sprockets I was able to refactor a few hundred lines into three :)
For example, suppose you have a file that contains sprockets <code>require</code> syntax called Main.coffee</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1">#= require &quot;src/one&quot;</span>
</span><span class='line'><span class="c1">#= require &quot;src/two&quot;</span>
</span><span class='line'><span class="c1">#= require &quot;src/three&quot;</span>
</span><span class='line'><span class="no">MyApp</span> <span class="sc">?=</span> <span class="p">{}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Then all you need to do is create a Sprockets::Environment instance and add the source path</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s1">&#39;sprockets&#39;</span>
</span><span class='line'><span class="n">env</span> <span class="o">=</span> <span class="no">Sprockets</span><span class="o">::</span><span class="no">Environment</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'><span class="n">env</span><span class="o">.</span><span class="n">paths</span> <span class="o">&lt;&lt;</span> <span class="s2">&quot;src/coffee&quot;</span>
</span><span class='line'><span class="nb">open</span><span class="p">(</span><span class="s2">&quot;example.js&quot;</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">write</span><span class="p">(</span> <span class="n">env</span><span class="o">[</span><span class="s2">&quot;Main.coffee&quot;</span><span class="o">]</span> <span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Tada! It just works :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Producer-consumer problem in C]]></title>
    <link href="http://rramsden.ca/blog/2011/08/27/producer-consumer-problem-in-c/"/>
    <updated>2011-08-27T14:10:00+09:00</updated>
    <id>http://rramsden.ca/blog/2011/08/27/producer-consumer-problem-in-c</id>
    <content type="html"><![CDATA[<p>For my Operating Systems class CSCI360 we had to write the classic Producer-consumer problem in C using semaphores.
This is a classic problem/solution to preventing race conditions over a shared resource.</p>

<p>For those of you who don&#8217;t know what a semaphore is it&#8217;s simply a variable that acts as a counter ( with two operations up/down )
which puts a process to sleep if it tries to down/decrement a 0 valued semaphore.
The process will receive a wakeup signal once the 0 valued semaphore is up/incremented by another process.</p>

<p>In the producer-consumer example below we have a critical region (some shared buffer) that needs to be
controlled using semaphores to prevent race conditions.
We create two semaphores one called EMPTY which represents the number of empty slots available
for the producer and another semaphore called FULL to represent the number of slots occupied with items.</p>

<p>If there are no items in our buffer a consumer will goto sleep since it will try to down the FULL counter which will
be 0 because there are no items occupying any slots for it to consume.
Once the producer creates an item it will down EMPTY and up FULL; vice-versa.
In the case of the consumer when it consumes items it will up EMPTY and down FULL.
This might seem a little confusing at first so I created a diagram to
illustrate the process (note: producer/consumer are NOT running in parallel in this diagram).</p>

<p><img src="http://img.skitch.com/20110827-q7hd4p6n938dfnix18arm2sjwa.jpg"></p>

<p>It is important to note that EMPTY and FULL alone do not prevent race conditions.<br/>
This doesn&#8217;t cover the case when the buffer is neither full nor empty.
This means we need a third semaphore to lock the shared buffer to prevent consumers and producers from accessing
the shared buffer at the same time.</p>

<p>We call a semaphore with a single counter (initialized to 1)
a binary semaphore or more commonly referred to as a mutex or lock. Using a mutex initialized to
1 when a consumer wishes to consume an item it simply needs to down the mutex before consuming
the item which will prevent other producers/consumers from manipulating the shared buffer.
There are two main semaphore implementations in UNIX-based systems: System V (available in older-systems) and POSIX.
For my example program I used System V implementation.</p>

<h2>Producer.c</h2>

<div><script src='https://gist.github.com/1175897.js?file='></script>
<noscript><pre><code>#include &quot;shared.h&quot;

void insert_item(int item, int semid, int *shared_buffer) {
  int index = get_buffer_size(shared_buffer);
  shared_buffer[index] = item; 
}

int produce_item() {
  return 0xFF; // nothing dynamic just write a static integer a slot
}

int main(int argc, const char *argv[])
{
  int *shared_buffer = create_shared_mem_buffer();
  int semid = create_semaphore_set();

  clear_buffer(shared_buffer); // prepare buffer for jobs

  int item = 0;

  while(1) {
    item = produce_item();
    semop(semid, &amp;downEmpty, 1);
    semop(semid, &amp;downMutex, 1);
    insert_item(item, semid, shared_buffer);
    debug_buffer(shared_buffer);
    semop(semid, &amp;upMutex, 1);
    semop(semid, &amp;upFull, 1);
  }
 
  return EXIT_SUCCESS;
}</code></pre></noscript></div>


<h2>Consumer.c</h2>

<div><script src='https://gist.github.com/1175900.js?file='></script>
<noscript><pre><code>#include &quot;shared.h&quot;

void consume_item(int item) {
  // do something with item
}

int remove_item(int semid, int *shared_buffer) {
  int index = get_buffer_size(shared_buffer) - 1;
  int item = shared_buffer[index];
  shared_buffer[index] = 0x00;
  return item;
}

int main(int argc, const char *argv[])
{
  int *shared_buffer = create_shared_mem_buffer();
  int semid = create_semaphore_set();

  int item = 0;

  while(1) {
    semop(semid, &amp;downFull, 1);
    semop(semid, &amp;downMutex, 1);
    item = remove_item(semid, shared_buffer);
    debug_buffer(shared_buffer);
    semop(semid, &amp;upMutex, 1);
    semop(semid, &amp;upEmpty, 1);
    consume_item(item);
  } 

  return EXIT_SUCCESS;
}</code></pre></noscript></div>


<h2>Shared.h</h2>

<div><script src='https://gist.github.com/1175904.js?file='></script>
<noscript><pre><code>#include &lt;sys/types.h&gt;
#include &lt;sys/ipc.h&gt;
#include &lt;sys/sem.h&gt;
#include &lt;sys/shm.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;errno.h&gt;

#define BUFFER_SIZE 5
#define EMPTY_ID 0
#define FULL_ID 1
#define MUTEX_ID 2
#define NSEM_SIZE 3

#define SHM_KEY 9
#define SEM_KEY &quot;.&quot;

static struct sembuf downEmpty = { EMPTY_ID, -1, 0 };
static struct sembuf upEmpty = { EMPTY_ID, 1, 0 };
static struct sembuf upFull = { FULL_ID, 1, 0 };
static struct sembuf downFull = { FULL_ID, -1, 0 };
static struct sembuf downMutex = { MUTEX_ID, -1, 0 };
static struct sembuf upMutex = { MUTEX_ID, 1, 0 };

int *create_shared_mem_buffer();
int create_semaphore_set();
int get_buffer_size(int *sbuff);
void clear_buffer(int *sbuf);</code></pre></noscript></div>


<h2>Shared.c</h2>

<div><script src='https://gist.github.com/1175903.js?file='></script>
<noscript><pre><code>#include &quot;shared.h&quot;

/**
 * returns current size of shared buffer
 */
int get_buffer_size(int *sbuff) {
  int i = 0;
  int counter = 0;
  for (i = 0; i &lt; BUFFER_SIZE; ++i) {
    if (sbuff[i] == 0xFF) {
      counter++;
    } 
  }
  return counter;
}

void debug_buffer(int *sbuff) {
  int i = 0;
  for (i = 0; i &lt; BUFFER_SIZE; ++i) {
    if (sbuff[i] == 0xFF) printf(&quot;1&quot;);
  }
  printf(&quot;\n&quot;);
}

/**
 * returns a pointer to a shared memory buffer that the
 * producer can write to.
 */
int *create_shared_mem_buffer() {
  int *shmaddr = 0; /* buffer address */
  key_t key = SHM_KEY; /* use key to access a shared memory segment */
  
  int shmid = shmget(key, BUFFER_SIZE, IPC_CREAT | SHM_R | SHM_W); /* give create, read and write access */
  if (errno &gt; 0) {
    perror(&quot;failed to create shared memory segment&quot;);
    exit (EXIT_FAILURE);
  }

  shmaddr = (int*)shmat(shmid, NULL, 0);
  if (errno &gt; 0) {
    perror (&quot;failed to attach to shared memory segment&quot;);
    exit (EXIT_FAILURE);
  }

  // clean out garbage memory in shared memory
  return shmaddr;
}

/**
 * only used in the producer to clean out garbage memory when
 * constructing initial buffer.
 */
void clear_buffer(int *sbuff) {
  int i = 0;
  for (i = 0; i &lt; BUFFER_SIZE; ++i) sbuff[i] = 0x00;
}

/**
 * create FULL and EMPTY semaphores
 */
int create_semaphore_set() {
  key_t key = ftok(SEM_KEY, 'E');
  
  int semid = semget(key, NSEM_SIZE, 0600 | IPC_CREAT);
  if (errno &gt; 0) {
    perror(&quot;failed to create semaphore array&quot;);
    exit (EXIT_FAILURE);
  } 

  semctl(semid, FULL_ID, SETVAL, 0);
  if (errno &gt; 0) {
    perror(&quot;failed to set FULL semaphore&quot;);
    exit (EXIT_FAILURE);
  }

  semctl(semid, EMPTY_ID, SETVAL, BUFFER_SIZE);
  if (errno &gt; 0) {
    perror(&quot;failed to set EMPTY sempahore&quot;);
    exit (EXIT_FAILURE);
  }

  semctl(semid, MUTEX_ID, SETVAL, 1);
  if (errno &gt; 0) {
    perror(&quot;failed to create mutex&quot;);
  }

  return semid;
}</code></pre></noscript></div>



]]></content>
  </entry>
  
</feed>
