<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dheeraj R Reddy</title>
    <description>The latest articles on DEV Community by Dheeraj R Reddy (@squadrick).</description>
    <link>https://dev.to/squadrick</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F390992%2F82488d2b-4baf-4a52-b4f9-25884abeedc5.png</url>
      <title>DEV Community: Dheeraj R Reddy</title>
      <link>https://dev.to/squadrick</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/squadrick"/>
    <language>en</language>
    <item>
      <title>Shadesmar: Fast C++ IPC</title>
      <dc:creator>Dheeraj R Reddy</dc:creator>
      <pubDate>Thu, 21 May 2020 23:21:27 +0000</pubDate>
      <link>https://dev.to/squadrick/shadesmar-fast-c-ipc-13no</link>
      <guid>https://dev.to/squadrick/shadesmar-fast-c-ipc-13no</guid>
      <description>&lt;h2&gt;
  
  
  My Final Project
&lt;/h2&gt;

&lt;p&gt;I built a C++ inter-process communication library that uses shared memory for sending messages between processes. It currently supports publish-subscribe and RPC paradigms. Since it uses shared memory, the library is blazing fast and allows for zero-copy transfers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Demo Link
&lt;/h2&gt;

&lt;p&gt;You can check out the examples &lt;a href="https://github.com/Squadrick/shadesmar/tree/master/examples"&gt;here&lt;/a&gt;. For a demo of it in action, you can see the GitHub Actions CI of the project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Link to Code
&lt;/h2&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vWogaON8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/github-logo-28d89282e0daa1e2496205e2f218a44c755b0dd6536bbadf5ed5a44a7ca54716.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/Squadrick"&gt;
        Squadrick
      &lt;/a&gt; / &lt;a href="https://github.com/Squadrick/shadesmar"&gt;
        shadesmar
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Fast C++ IPC using shared memory (with msgpack)
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;h3&gt;
Shadesmar&lt;/h3&gt;
&lt;p&gt;&lt;a href="https://stormlightarchive.fandom.com/wiki/Soulcasting" rel="nofollow"&gt;Soulcast&lt;/a&gt; &lt;a href="https://stormlightarchive.fandom.com/wiki/Hoid" rel="nofollow"&gt;hoid&lt;/a&gt; pointers.&lt;/p&gt;
&lt;p&gt;An IPC library that uses the system's shared memory to pass messages
The communication paradigm is either publish-subscibe or RPC similar to ROS and ROS2
The library was built to be used within &lt;a href="http://projectmanas.in/" rel="nofollow"&gt;Project MANAS&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Required packages: Boost, Msgpack&lt;/p&gt;
&lt;h4&gt;
Features&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Multiple subscribers and publishers.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Multithreaded RPC support.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Uses a circular buffer to pass messages between processes.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Faster than using the network stack like in the case with ROS.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Read and write directly from GPU memory to shared memory.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Decentralized, without &lt;a href="https://squadrick.github.io/journal/ipc-locks.html" rel="nofollow"&gt;resource starvation&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Allows for both serialized message passing (using &lt;code&gt;msgpack&lt;/code&gt;) and to
pass raw bytes.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;No need to define external IDL files for messages. Use C++ classes as
message definition.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
Publish-Subscribe (serialized messages)&lt;/h4&gt;
&lt;p&gt;Message Definition (&lt;code&gt;custom_message.h&lt;/code&gt;):&lt;/p&gt;
&lt;div class="highlight highlight-source-c++"&gt;
&lt;pre&gt;#&lt;span class="pl-k"&gt;include&lt;/span&gt; &lt;span class="pl-s"&gt;&lt;span class="pl-pds"&gt;&amp;lt;&lt;/span&gt;shadesmar/message.h&lt;span class="pl-pds"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class="pl-k"&gt;class&lt;/span&gt; &lt;span class="pl-en"&gt;InnerMessage&lt;/span&gt; : &lt;span class="pl-k"&gt;public&lt;/span&gt; &lt;span class="pl-en"&gt;shm&lt;/span&gt;::BaseMsg {
  &lt;span class="pl-k"&gt;public:&lt;/span&gt;
    &lt;span class="pl-k"&gt;int&lt;/span&gt; inner_val{}
    std::string inner_str{};
    &lt;span class="pl-en"&gt;SHM_PACK&lt;/span&gt;(inner_val, inner_str);

    &lt;span class="pl-en"&gt;InnerMessage&lt;/span&gt;() = &lt;span class="pl-k"&gt;default&lt;/span&gt;;&lt;/pre&gt;…&lt;/div&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/Squadrick/shadesmar"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  How I built it (what's the stack? did I run into issues or discover something new along the way?)
&lt;/h2&gt;

&lt;p&gt;The library is built entirely in C++17. I use &lt;a href="https://msgpack.org/index.html"&gt;msgpack&lt;/a&gt; for serializing C++ objects to transfer them "over-the-wire", but this is not a necessity. For higher performance, the library allows transferring memory directly (pointers). &lt;/p&gt;

&lt;p&gt;At the core of the library is a &lt;code&gt;SharedQueue&lt;/code&gt; which is a circular MPMC queue that runs entirely in shared memory space. It is built with the intention to minimize contention as much as possible, with all writes to the queue being lock-free. There is currently work underway to make the queue fully lock-free.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;SharedQueue&lt;/code&gt; indexes into the underlying shared memory buffer that uses Boost's &lt;code&gt;managed_shared_memory&lt;/code&gt; for maintaining allocations, using a red-black tree.&lt;/p&gt;

&lt;p&gt;The library also supports inter-device transfers (CPU-&amp;gt;GPU, etc.) using a new construct called &lt;code&gt;Copier&lt;/code&gt;. This also allows for exotic copy functions apart from &lt;code&gt;std::memcpy&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I faced several challenges while building Shadesmar, chief among them being the need for robust interprocess locks. I found little to no pre-existing information so I decided to write a few blog posts on my website:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://squadrick.dev/journal/ipc-locks.html"&gt;Interprocess Locks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://squadrick.dev/journal/better-ipc-locks.html"&gt;(Better) Interprocess Locks&lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Writing the tests for the library was particularly tricky since all tests needed to &lt;code&gt;fork()&lt;/code&gt; for inter-process communication, and all the testing frameworks I tried don't seem to work when using &lt;code&gt;fork()&lt;/code&gt;. I'm currently manually doing tests with &lt;code&gt;assert&lt;/code&gt;s. &lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Thoughts / Feelings / Stories
&lt;/h2&gt;

&lt;p&gt;The name of the library is from one of my favourite high fantasy novels: The Stormlight Archive by Brandon Sanderson. In the books, &lt;a href="https://stormlightarchive.fandom.com/wiki/Shadesmar"&gt;Shadesmar&lt;/a&gt; is an alternative plane of existence that binds the other two planes of existence together. I used the name since shared memory (shm) is the bridge between processes. &lt;/p&gt;

&lt;p&gt;Funnily enough, I recently posted about my library on Reddit and was &lt;a href="https://www.reddit.com/r/cpp/comments/gfwhwv/shadesmar_fast_c_ipc_using_shared_memory/fq8tznm?utm_source=share&amp;amp;utm_medium=web2x"&gt;acknowledged by the author&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>octograd2020</category>
    </item>
  </channel>
</rss>
