<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Maharajan Shunmuga Sundaram on Medium]]></title>
        <description><![CDATA[Stories by Maharajan Shunmuga Sundaram on Medium]]></description>
        <link>https://medium.com/@maharajan-ses?source=rss-9e82e027b363------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Maharajan Shunmuga Sundaram on Medium</title>
            <link>https://medium.com/@maharajan-ses?source=rss-9e82e027b363------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 10:40:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@maharajan-ses/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Your Body Is a 37-Trillion-Agent Distributed System]]></title>
            <link>https://maharajan-ses.medium.com/your-body-is-a-37-trillion-agent-distributed-system-aa4e7e6222ab?source=rss-9e82e027b363------2</link>
            <guid isPermaLink="false">https://medium.com/p/aa4e7e6222ab</guid>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[software-system-design]]></category>
            <dc:creator><![CDATA[Maharajan Shunmuga Sundaram]]></dc:creator>
            <pubDate>Sun, 21 Jun 2026 16:36:00 GMT</pubDate>
            <atom:updated>2026-06-21T16:36:00.740Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/360/0*sp0QK-dl1EKslq_9" /></figure><h3>Introduction</h3><p>Every software engineer has stared at a production dashboard and thought: <em>How is this thing still running?</em></p><p>Services crash. Queues back up. Alerts fire at 2 a.m. Dependencies fail in ways no architecture diagram predicted. We spend careers trying to build systems that are scalable, observable, resilient, secure, and self-healing.</p><p>Now consider this: you are already running one.</p><p>Not on AWS. Not in Kubernetes. Not across a fleet of servers.</p><p>Inside your own body.</p><p>The human body is, in many ways, the most sophisticated distributed system ever deployed: roughly 37 trillion autonomous agents coordinating in real time, exchanging messages, repairing damage, defending against attackers, adapting to load, and somehow keeping the whole platform alive.</p><h3>Organs Are Microservices</h3><p>In modern architecture, we split responsibilities across services. Each owns a domain. Each exposes behavior. Each depends on shared infrastructure.</p><p>The body works the same way.</p><ul><li>The <strong>heart</strong> is a transport service.</li><li>The <strong>lungs</strong> are an oxygen acquisition service.</li><li>The <strong>liver</strong> is a transformation engine.</li><li>The <strong>kidneys</strong> handle filtering and garbage collection.</li><li>The <strong>pancreas</strong> manages resource allocation.</li><li>The <strong>immune system</strong> is a security platform.</li><li>The <strong>brain</strong> acts like a global control plane.</li></ul><p>Each service has a clear responsibility. The kidneys do not need to understand how lungs extract oxygen. They simply consume oxygen delivered through the network.</p><p>That separation of concerns is not just elegant. It is survival.</p><h3>Blood Is the Message Bus</h3><p>Distributed systems need communication layers. We use REST, gRPC, Kafka, queues, event streams, and service meshes.</p><p>The body has two major communication systems.</p><p>The first is the <strong>bloodstream</strong>.</p><p>Blood carries oxygen, nutrients, hormones, immune cells, and waste products. Blood vessels are the network cables. Circulation is the routing layer. Every organ publishes and consumes through this shared transport.</p><p>But blood is stranger than Kafka. It does not just move information. It moves physical resources.</p><p>A message in software may say, “CPU usage is high.” A message in the bloodstream might say, “Here is glucose, oxygen, and an immune cell. Use immediately.”</p><p>That is not just messaging. That is logistics.</p><h3>The Nervous System Is Low-Latency RPC</h3><p>Some operations cannot wait for eventual consistency.</p><p>Touch a hot stove and your hand withdraws before you have consciously processed the pain. That is not a batch job. That is not an asynchronous event stream. That is a low-latency remote procedure call.</p><p>Brain.call(Hand.withdraw())</p><p>Neural communication is optimized for speed. Hormonal communication may take minutes. Nerve signals can travel in milliseconds.</p><p>The body supports both patterns:</p><ul><li><strong>Slow, broadcast-style coordination</strong> through hormones</li><li><strong>Fast, point-to-point control</strong> through nerves</li></ul><p>Good architecture rarely has one communication model. The body proves why.</p><h3>Hormones Are Events</h3><p>Hormones are a beautiful example of publish-subscribe architecture.</p><p>The thyroid does not personally instruct every cell to change metabolism. It publishes a signal into the bloodstream. Organs and tissues with the right receptors subscribe to it.</p><p>The signal is global. The reaction is local.</p><p>That is event-driven design at biological scale.</p><p>A hormone might effectively say:</p><p>publish(MetabolismChanged(1.25))</p><p>Every organ sees the message. Only the ones with matching receptors respond.</p><p>This is one of the body’s most powerful design patterns: broadcast widely, react selectively.</p><h3>Observability Comes Built In</h3><p>Engineers spend enormous effort adding observability to production systems. Metrics. Logs. Traces. Dashboards. Alerts. Health checks.</p><p>The body ships with observability enabled by default.</p><p>It continuously monitors:</p><ul><li>Blood pressure</li><li>Oxygen saturation</li><li>Carbon dioxide</li><li>Blood sugar</li><li>Electrolytes</li><li>Temperature</li><li>Hormone levels</li></ul><p>Pain is an alerting framework. Hunger means energy reserves are low. Thirst means water deficit detected. Fever means elevated threat level. Fatigue means resource consumption has exceeded sustainable limits.</p><p>Consciousness is the dashboard. Reflexes are health checks. Carbon dioxide sensors are watchdogs.</p><p>No one had to install this telemetry. Without it, the system would not survive.</p><h3>The Immune System Is Zero-Trust Security</h3><p>The immune system behaves like a security platform with multiple layers of defense.</p><p>It performs identity verification, threat detection, signature matching, anomaly detection, incident response, and long-term threat memory.</p><p>Vaccines are training datasets. They expose the system to a safe representation of a threat so future detection becomes faster and more accurate.</p><p>But, like any security system, it can misfire.</p><p>Allergies are false positives. Autoimmune disease is the security layer attacking trusted internal services. Cancer is a Byzantine service: a component that stops following protocol and begins consuming resources for itself.</p><p>Biology has bugs in production too.</p><h3>Sleep Is the Maintenance Window</h3><p>Every serious system needs maintenance. Indexes must be rebuilt. Logs must be compacted. Memory must be cleaned. Models must be refreshed.</p><p>The body schedules this work every night.</p><p>Sleep supports:</p><ul><li>Memory consolidation</li><li>Hormone regulation</li><li>Protein synthesis</li><li>Waste removal</li><li>Immune optimization</li></ul><p>The remarkable part is that maintenance happens while the system stays alive. The body does not fully shut down. It enters a reduced operating mode, repairs itself, and prepares for the next day’s traffic spike.</p><h3>DNA Is the Source Repository</h3><p>DNA looks a lot like a source repository.</p><p>It contains build instructions, configuration, deployment logic, feature flags, and environment-specific behavior.</p><p>Gene expression is not static. It changes based on context.</p><p>Starvation may enable fat storage mode. Pregnancy may activate development modules. Exercise may trigger muscle adaptation. Infection may activate defense pathways.</p><p>The codebase is shared, but runtime behavior depends on environment.</p><p>That is configuration management at cellular scale.</p><h3>Evolution Is Continuous Deployment</h3><p>Software teams deploy weekly, daily, or continuously.</p><p>Nature has been doing continuous deployment for hundreds of millions of years.</p><p>Poor versions fail. Successful versions survive. Useful mutations propagate. Broken changes disappear. Evolution is not sentimental. It is the oldest optimization algorithm we know.</p><p>There are no perfect releases. Only versions that survive long enough to reproduce.</p><h3>The Wildest Production System Ever Built</h3><p>The human body is self-healing, observable, fault tolerant, secure, energy efficient, adaptive, and continuously deployable.</p><p>It performs trillions of operations every second. It repairs damaged components. It defends itself against attackers. It routes resources, publishes events, handles incidents, and learns from prior failures.</p><p>And it does all this without an on-call rotation.</p><p>The next breakthrough in distributed systems may not come from another conference paper, another framework, or another cloud platform.</p><p>It may come from studying the architecture that has been walking around Earth all along.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aa4e7e6222ab" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Participle]]></title>
            <link>https://maharajan-ses.medium.com/participle-f8362bcd3815?source=rss-9e82e027b363------2</link>
            <guid isPermaLink="false">https://medium.com/p/f8362bcd3815</guid>
            <category><![CDATA[participles]]></category>
            <category><![CDATA[tamil]]></category>
            <category><![CDATA[english]]></category>
            <dc:creator><![CDATA[Maharajan Shunmuga Sundaram]]></dc:creator>
            <pubDate>Sun, 19 Jan 2025 17:14:24 GMT</pubDate>
            <atom:updated>2025-01-21T02:14:24.233Z</atom:updated>
            <content:encoded><![CDATA[<p>In a recent conversation, I asked a question of “What is past participle?” to bunch of kids studying in US. In US, english grammar is not taught the way in India. Since they know the language well from childhood, they don’t need to teach very fundamental. Here I am trying to add my thought on the subject.</p><h4>Three form of verb</h4><p>In English, if you know a <strong>verb</strong>, (eg: go), then you can form sentences in present tense (eg: I go), future tense (eg: I will go), but not in the past tense. You could use did (eg: I did go) which is valid, but the sentence is meant to emphasis something. You need to learn second form of go which is <strong>went</strong>. Then you can use past tense sentence (eg: I went). Continuous tense can be framed using (going = go + ing). Sentences using third person singular form can be made (eg: does + go = goes). But perfect tenses can’t be made. For that you need to know 3rd form of verb, <strong>gone</strong>. The sentence could be, “I have gone”.</p><p>So summary here is that in order to use a verb, you need to memorize all the three forms of a verb to use it in all tenses. There are additional use of participles. When a passive voice is needed, you again need a participle, (eg: I am gone). An another use case is adjective while describing a noun, (eg: A <strong>stolen</strong> watch is found). Again you need to memorize three different piece of words (sometime 1st form and 3rd form are same eg: come) to improve your bank of verbs.</p><h4>Tamil</h4><p>In Tamil, the case is very different. You need to know one verb, (eg: போ). How do we make a sentence in present tense? போகிறேன். This one word is a sentence which conveys the meaning, I go. How? போ + கிறு + ஏன். Here போ is verb, கிறு represents present tense. This is called நிகழ்கால இடைநிலை which represent present tense. See <a href="https://ta.wikipedia.org/wiki/%E0%AE%87%E0%AE%9F%E0%AF%88%E0%AE%A8%E0%AE%BF%E0%AE%B2%E0%AF%88">the page</a>. ஏன் represent first person singular (that is I) (தன்மை ஒருமை வினைமுற்று விகுதி). While representing third person, eg: போகிறான் (போ + கிறு + ஆன்), we can say gender of the subject which is male, it is not object (உயர்திணை vs அஃறிணை). To know more about this, see the <a href="https://www.tamilvu.org/ta/courses-degree-c021-c0212-html-c0212661-15873">page</a>.</p><p>One question you might ask, how come, போ + கிறு + ஏன், leads to போகிறேன். You need to know punarchi vidhigal. This is little advanced. As per இயல்பு புணர்ச்சி, போ + கிறு becomes போகிறு + ஏன். Based on “உயிர்வரின் முற்றியலுகரம் மெய் விட்டு கெடும்”, it becomes, போகிற் + ஏன். Based on “உடல்மேல் உயிர்வந்து ஒன்றுவது இயல்பே”, it becomes போகிறேன். See the <a href="https://www.valaitamil.com/punarchi_8895.html">page</a>.</p><p>In the past tense of the sentence, போனேன், போ + ன் + ஏன், ன் represents past tense. In future, it is போவேன், போ + வ் + ஏன், வ் represents future tense.</p><p>If you know any verb (one word), you can form tenses using punarchi (merging) laws, eg: போகிறேன், வருகிறேன், குதிக்கிறேன்.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f8362bcd3815" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Binary Linux Programs]]></title>
            <link>https://maharajan-ses.medium.com/understanding-binary-linux-programs-b52d348a12c7?source=rss-9e82e027b363------2</link>
            <guid isPermaLink="false">https://medium.com/p/b52d348a12c7</guid>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[debug]]></category>
            <category><![CDATA[elf]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Maharajan Shunmuga Sundaram]]></dc:creator>
            <pubDate>Sat, 14 Dec 2024 03:13:40 GMT</pubDate>
            <atom:updated>2024-12-14T03:13:40.540Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tMkZQnRAkLrdXanD" /></figure><h3>Understanding Binary Linux Programs</h3><h4>The Basics of Binary Programming</h4><p>Binary programming begins with an idea, which is then translated into source code written in a programming language such as C. This source code is saved in a file and then processed by a compiler, such as GCC. The compilation process occurs in two main stages:</p><ol><li><strong>Object Code Creation</strong>: The compiler translates the source code into object code.</li><li><strong>Binary File Generation</strong>: The linker combines the object code with any referenced libraries to produce a binary file.</li></ol><p>The resulting binary file contains machine code instructions that the CPU can execute directly when the program is run.</p><h4>The ELF File Format</h4><p>The binary files generated by the linker often follow a specific structure. One of the most widely used formats is the <strong>ELF</strong> (Executable and Linkable Format). This format is used for executable files, relocatable object files, shared libraries, and core dumps.</p><p>A Brief History of ELF</p><p>In 1999, the 86open project adopted ELF as the standard binary file format for Unix and Unix-like systems on x86 processors. This choice was made easier by prior documentation in both the System V Application Binary Interface and the Tool Interface Standard.</p><p>ELF’s design offers several advantages:</p><ul><li><strong>Flexibility</strong></li><li><strong>Extensibility</strong></li><li><strong>Cross-Platform Support</strong>: Accommodates different endian formats and address sizes.</li></ul><p>Its independence from specific processors, instruction sets, or hardware architectures makes ELF a universal standard. Today, it is used in various operating systems, including Linux and Solaris.</p><p>ELF File Extensions</p><p>ELF files may use a variety of filename extensions, such as .axf, .bin, .elf, .o, .prx, .puff, .ko, .so, and .mod, or sometimes no extension at all.</p><p>For a deeper dive into the ELF file format, see <a href="https://linuxhint.com/understanding_elf_file_format">this guide</a> or <a href="https://dtrugman.medium.com/elf-loaders-libraries-and-executables-on-linux-e5cfce318f94">this article</a>.</p><h4>Debug vs. Release Binaries</h4><p>A binary file in its simplest form contains headers, function names, and raw binary CPU instructions. However, <strong>debug binaries</strong> also include additional information to aid debugging, such as mapping compiled instructions to their original source code. For example, a debugger can determine:</p><ul><li>The source line associated with an instruction.</li><li>Variable names and types.</li></ul><h4>Example: Compiling in Release and Debug Modes</h4><p>Consider the following C++ program:</p><pre>#include &lt;iostream&gt;<br>int main(int argc, char* argv[]) {<br>    for (int i = 0; i &lt; argc; i++) {<br>        std::cout &lt;&lt; i &lt;&lt; std::endl;<br>    }<br>    return 0;<br>}</pre><p>You can compile this program in release or debug mode using the following commands:</p><ul><li><strong>Release Mode</strong>:</li></ul><pre>g++ main.cpp -o mainr.tsk</pre><ul><li><strong>Debug Mode</strong>:</li></ul><pre>g++ -g main.cpp -o maind.tsk</pre><p>The debug binary (“maind.tsk”) includes additional information, which increases its size compared to the release binary (“mainr.tsk”):</p><pre>-rwxrwxr-x 1 user group  8656 Sep 10 18:03 mainr.tsk<br>-rwxrwxr-x 1 user group 17952 Sep 10 18:03 maind.tsk</pre><h4>Inspecting Binaries</h4><p>You can use tools like objdump or nm to examine whether a binary is a debug or release version:</p><p><strong>Generate Logs with objdump</strong>:</p><pre>objdump --source mainr.tsk &gt; mainr.log objdump --source maind.tsk &gt; maind.log</pre><p><strong>Compare Logs</strong>:</p><pre>diff maind.log mainr.log</pre><p>Example output:</p><pre>2c2<br>&lt; maind.tsk:     file format elf64-x86-64<br>---<br>&gt; mainr.tsk:     file format elf64-x86-64<br>147,149d146<br>&lt; #include &lt;iostream&gt;<br>&lt;<br>&lt; int main(int argc, char* argv[]) {<br>155d151<br>&lt;     for (int i = 0; i &lt; argc; i++) {<br>158d153<br>&lt;         std::cout &lt;&lt; i &lt;&lt; std::endl;<br>166d160<br>&lt;     for (int i = 0; i &lt; argc; i++) {<br>171,172d164<br>&lt;     }<br>&lt;     return 0;<br>174d165<br>&lt; }<br>188,193d178<br>&lt;   extern wostream wclog; /// Linked to standard error (buffered)<br>&lt; #endif<br>&lt;   //@}<br>&lt;<br>&lt;   // For construction of filebuffers for cout, cin, cerr, clog et. al.<br>&lt;   static ios_base::Init __ioinit;</pre><p><strong>Display Symbol Table</strong>:</p><pre>objdump -t maind.tsk</pre><h4>Gaining Deeper Insights into Binary Behavior</h4><p>To understand what a Linux binary is doing, tools like strace, ltrace, and gdb are invaluable:</p><ul><li><strong>strace</strong>: Monitors system calls made by the program and the signals it receives. For example:</li></ul><pre>strace ./maind.tsk</pre><p>This provides a detailed list of system-level operations performed by the binary.</p><ul><li><strong>ltrace</strong>: Similar to strace, but focuses on library calls instead of system calls.</li><li><strong>gdb</strong>: A powerful debugger that lets you inspect the binary during runtime. For example:</li></ul><pre>gdb ./maind.tsk</pre><ul><li>Within gdb, you can set breakpoints, step through code, and inspect memory.</li></ul><p>For a detailed walkthrough of analyzing Linux binaries, see <a href="https://unix.stackexchange.com/questions/418354/understanding-what-a-linux-binary-is-doing">this discussion</a>.</p><p>These tools combined provide a comprehensive view of how a binary interacts with the operating system, libraries, and hardware.</p><h3>Conclusion</h3><p>Understanding the structure and behavior of Linux binary programs is essential for developers and system administrators alike. The ELF format’s flexibility and universality make it a cornerstone of modern Unix-like systems. Debugging and inspecting binaries, whether through tools like objdump, strace, or gdb, allow for greater control and insight into software functionality. By mastering these techniques, one can demystify the workings of binaries, ensuring efficient development, maintenance, and troubleshooting in a Linux environment.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b52d348a12c7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simple Pipeline in C++ with Folly]]></title>
            <link>https://maharajan-ses.medium.com/simple-pipeline-in-c-with-folly-23da89852e2e?source=rss-9e82e027b363------2</link>
            <guid isPermaLink="false">https://medium.com/p/23da89852e2e</guid>
            <category><![CDATA[cpp-programming]]></category>
            <category><![CDATA[programming-languages]]></category>
            <category><![CDATA[pipeline]]></category>
            <category><![CDATA[cpp-templates]]></category>
            <category><![CDATA[modern-cpp]]></category>
            <dc:creator><![CDATA[Maharajan Shunmuga Sundaram]]></dc:creator>
            <pubDate>Mon, 04 Nov 2024 02:29:38 GMT</pubDate>
            <atom:updated>2024-11-04T02:29:38.510Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*AQvVTOh9WWnCREwY" /></figure><h3>Creating a Pipelines in C++ with Folly</h3><p>In high-performance applications, a pipeline pattern is commonly used to process data through sequential stages, each performing a specific transformation or operation. This blog walks through building a modular data pipeline in C++ using Facebook’s <a href="https://github.com/facebook/folly">Folly</a> library. Folly’s flexible pipeline pattern makes it ideal for handling high-volume requests, allowing you to create a flow where each stage operates on and passes data to the next until a final output is produced.</p><p>I extracted the pipeline portion from folly library and presented here.</p><h3>Pipeline Design Overview</h3><p>In our example, each pipeline stage is a handler with a specific input and output type. The Handler class provides a base structure, and each custom handler class overrides the handle function. This function processes incoming data and uses a Context pointer to pass results forward with fireHandle. Below is the core example that demonstrates how a pipeline is constructed and run.</p><h3>Code Example</h3><pre>#include &lt;iostream&gt;<br>#include &lt;memory&gt;<br>#include &quot;pipeline.h&quot;<br><br>// First Handler: converts integer input to a string<br>struct Handler1 : public Handler&lt;int, std::string&gt; {<br>  void handle(Context* ctx, int msg) override {<br>    std::cout &lt;&lt; &quot;Handler1 : &quot; &lt;&lt; msg &lt;&lt; std::endl;<br>    ctx-&gt;fireHandle(std::to_string(msg));<br>  }<br>};<br>// Second Handler: appends a label to the string<br>struct Handler2 : public Handler&lt;std::string&gt; {<br>  void handle(Context* ctx, std::string msg) override {<br>    std::cout &lt;&lt; &quot;Handler2 : &quot; &lt;&lt; msg &lt;&lt; std::endl;<br>    ctx-&gt;fireHandle(msg + &quot; Handler2&quot;);<br>  }<br>};<br>// Third Handler: appends another label<br>struct Handler3 : public Handler&lt;std::string&gt; {<br>  void handle(Context* ctx, std::string msg) override {<br>    std::cout &lt;&lt; &quot;Handler3 : &quot; &lt;&lt; msg &lt;&lt; std::endl;<br>    ctx-&gt;fireHandle(msg + &quot; Handler3&quot;);<br>  }<br>};<br>int main() {<br>  auto p = Pipeline&lt;int, std::string&gt;::create();<br>  p-&gt;addBack(Handler1());<br>  p-&gt;addBack(Handler2());<br>  p-&gt;addBack(Handler3());<br>  p-&gt;finalize();<br>  p-&gt;handle(2);  // Start the pipeline with integer input<br>  return 0;<br>}</pre><p>In this example, Pipeline is constructed with Handler1, Handler2, and Handler3. Each handler processes the data, transforming and forwarding it until the final output is produced.</p><h3>Pipeline Class Implementation</h3><p>Now, let’s break down the inner workings of Folly’s Pipeline and its supporting classes.</p><ol><li><strong>PipelineBase</strong>: This base class manages a sequence of PipelineContext objects (one per handler). It has methods like addBack to add handlers and finalize to set up the pipeline structure.</li></ol><pre>class PipelineBase : public std::enable_shared_from_this&lt;PipelineBase&gt; {<br>public:<br>    virtual ~PipelineBase() = default;<br>    virtual void finalize() = 0;<br><br>    template &lt;class H&gt;<br>    PipelineBase&amp; addBack(H&amp;&amp; handler) {<br>      return addBack(std::make_shared&lt;H&gt;(std::forward&lt;H&gt;(handler)));<br>    }<br><br>    template &lt;class H&gt;<br>    PipelineBase&amp; addBack(std::shared_ptr&lt;H&gt; handler) {<br>      return addHelper(<br>        std::make_shared&lt;ContextImpl&lt;H&gt;&gt;(shared_from_this(), std::move(handler)), false);<br>    }<br><br>protected:<br>    std::vector&lt;std::shared_ptr&lt;PipelineContext&gt;&gt; ctxs_;<br>    std::vector&lt;PipelineContext*&gt; inCtxs_;<br><br>private:<br>    template &lt;class Context&gt;<br>    PipelineBase&amp; addHelper(std::shared_ptr&lt;Context&gt;&amp;&amp; ctx, bool front) {<br>      ctxs_.insert(front ? ctxs_.begin() : ctxs_.end(), ctx);<br>      inCtxs_.insert(front ? inCtxs_.begin() : inCtxs_.end(), ctx.get());<br>      return *this;<br>    }<br>};<br><br>template &lt;class R, class W&gt;<br>class Pipeline : public PipelineBase {<br> public:<br>  using Ptr = std::shared_ptr&lt;Pipeline&gt;;<br><br>  static Ptr create() {<br>    return std::shared_ptr&lt;Pipeline&gt;(new Pipeline());<br>  }<br><br>  ~Pipeline() override {}<br><br>  template &lt;class T = R&gt;<br>  void handle(R msg) {<br>    front_-&gt;handle(std::forward&lt;R&gt;(msg));<br>  }<br><br>  void finalize() override {<br>    front_ = nullptr;<br>    if (!inCtxs_.empty()) {<br>      front_ = dynamic_cast&lt;Link&lt;R&gt;*&gt;(inCtxs_.front());<br>      for (size_t i = 0; i &lt; inCtxs_.size() - 1; i++) {<br>        inCtxs_[i]-&gt;setNextIn(inCtxs_[i + 1]);<br>      }<br>      inCtxs_.back()-&gt;setNextIn(nullptr);<br>    }<br>  <br>    for (auto it = ctxs_.rbegin(); it != ctxs_.rend(); it++) {<br>      (*it)-&gt;attachPipeline();<br>    }<br>  }<br><br>protected:<br>  Pipeline() {}<br><br>private:<br>  Link&lt;R&gt;* front_{nullptr};<br>};</pre><p><strong>2. PipelineContext and Handler Classes</strong>: Each handler is wrapped in a PipelineContext, which manages its lifecycle and links it with the pipeline. Handler classes define custom processing logic in handle.</p><pre>template &lt;class Rin, class Rout = Rin&gt;<br>class Handler : public HandlerBase&lt;HandlerContext&lt;Rout&gt;&gt; {<br> public:<br>  typedef Rin rin;<br>  typedef Rout rout;<br>  typedef HandlerContext&lt;Rout&gt; Context;<br>  ~Handler() override = default;<br><br>  virtual void handle(Context* ctx, Rin msg) = 0;<br>};</pre><p><strong>3. Executing the Pipeline</strong>: Once finalize is called, the pipeline is ready. Calling handle on the pipeline triggers the first handler, and data flows through each stage until the end.</p><h3>Summary</h3><p>Using Folly’s pipeline, you can build efficient, modular processing flows in C++. This approach is well-suited for applications requiring streamlined, high-throughput processing of data or requests. For more details, including the full implementation, check out the <a href="https://github.com/maharajan1/pipeline">GitHub repository</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=23da89852e2e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementing a C++ Executor with Template Specialization]]></title>
            <link>https://maharajan-ses.medium.com/c-template-specialization-7cb946620159?source=rss-9e82e027b363------2</link>
            <guid isPermaLink="false">https://medium.com/p/7cb946620159</guid>
            <category><![CDATA[cpp20]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[template]]></category>
            <category><![CDATA[cplusplus]]></category>
            <category><![CDATA[async-programming]]></category>
            <dc:creator><![CDATA[Maharajan Shunmuga Sundaram]]></dc:creator>
            <pubDate>Sat, 12 Oct 2024 20:01:18 GMT</pubDate>
            <atom:updated>2024-10-23T13:08:11.025Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xU2aUR1zikuZeV2X" /></figure><p>In this post, we’ll implement an <strong>Executor</strong> that accepts a function of various types and executes it according to different execution policies. The function can be synchronous, asynchronous (returning a std::future), or even a coroutine. We’ll use template specialization to handle the differences in execution for each type.</p><p>Usage Example</p><pre>int fn() { return 10; }<br><br>std::future&lt;int&gt; fnAsync() { <br>  return std::async(std::launch::deferred, [](){ return 100; }); <br>}<br><br>// Synchronous execution<br>Executor&lt;int()&gt; a_executor(fn);<br>std::cout &lt;&lt; a_executor.execute() &lt;&lt; std::endl;<br><br>// Asynchronous execution<br>Executor&lt;std::future&lt;int&gt;()&gt; a_executorAsync(fnAsync);<br>std::cout &lt;&lt; a_executorAsync.execute() &lt;&lt; std::endl;</pre><h4>Template Specialization</h4><p>To implement the <strong>Executor</strong>, we need to use <strong>template specialization</strong>. This technique allows us to provide an explicit definition for a template based on certain arguments. It’s commonly used in C++ for type traits and conditional compilation.</p><p>We’ll determine whether the passed function is synchronous or asynchronous using <strong>type traits</strong>. The primary template will handle synchronous functions, while a specialized template will handle functions returning std::future.</p><pre>// Primary template<br>template &lt;typename Ret, typename... Args&gt;<br>struct FunctionInfo<br>{<br>    using ArgsPack      = std::tuple&lt;Args...&gt;;<br>    using ReturnType    = Ret;<br>    static constexpr bool IsFuture = false;<br>};<br><br>// Specialized template for async api<br>template &lt;typename Ret, typename... Args&gt;<br>struct FunctionInfo&lt;std::future&lt;Ret&gt;, Args...&gt;<br>{<br>    using ArgsPack      = std::tuple&lt;Args...&gt;;<br>    using ReturnType    = Ret;<br>    static constexpr bool IsFuture = true;<br>};</pre><p>Now, we can easily distinguish between synchronous and asynchronous functions:</p><pre>std::cout &lt;&lt; FunctionInfo&lt;int, void&gt;::IsFuture &lt;&lt; std::endl; // 0 (Sync)<br>std::cout &lt;&lt; FunctionInfo&lt;std::future&lt;int&gt;, void&gt;::IsFuture &lt;&lt; std::endl; // 1 (Async)</pre><h4>Executor Implementation</h4><p>The next step is to design the <strong>Executor</strong>’s interface. We use template specialization to handle different function types (synchronous and asynchronous). Here’s the basic outline of our executor:</p><pre>// Primary template declaration<br>template &lt;typename Signature&gt;<br>class Executor;<br><br>// Specialized template<br>template &lt;typename Ret, typename... Args&gt;<br>class Executor&lt;Ret(Args...)&gt;<br>{<br>    template &lt;typename... LclArgs&gt;<br>    TaskResult_t execute(LclArgs... lclArgs);<br>};</pre><h4>Task Result</h4><p>To avoid returning std::future&lt;Type&gt;, we define a TaskResult_t type that gives us the underlying return type of the function. This is where the previously defined FunctionInfo comes into play:</p><pre>template &lt;typename Ret, typename... Args&gt;<br>class Executor&lt;Ret(Args...)&gt; {<br>    using TaskResult_t  = typename FunctionInfo&lt;Ret, Args...&gt;::ReturnType;<br><br>    template &lt;typename... LclArgs&gt;<br>    TaskResult_t execute(LclArgs... lclArgs);<br>};</pre><h4>Handling Synchronous and Asynchronous Execution</h4><p>We now need to handle the different execution styles for synchronous and asynchronous functions. Again, template specialization helps:</p><pre>// Primary template declaration<br>template &lt;typename Executor, bool IsFuture, typename Signature&gt;<br>class Task;<br><br>// Specialized template for sync function<br>template &lt;typename Executor, typename FunctionReturn, typename... Args&gt;<br>class Task&lt;Executor, false, FunctionReturn(Args...)&gt;<br>{<br>  public:<br>    template &lt;typename F&gt;<br>    Task(F&amp;&amp; f)<br>    {<br>        d_task = std::forward&lt;F&gt;(f);<br>    }<br><br>    template &lt;typename... LclArgs&gt;<br>    typename Executor::TaskResult_t invoke(LclArgs&amp;&amp;... lclArgs) const<br>    {<br>        std::cout &lt;&lt; &quot;Sync&quot; &lt;&lt; std::endl;<br>        return d_task(std::forward&lt;LclArgs&gt;(lclArgs)...);<br>    }<br><br>  private:<br>    std::function&lt;FunctionReturn(Args...)&gt; d_task;<br>};<br><br>// Specialized template for async function<br>template &lt;typename Executor, typename FunctionReturn, typename... Args&gt;<br>class Task&lt;Executor, true, std::future&lt;FunctionReturn&gt;(Args...)&gt;<br>{<br>  public:<br>    template &lt;typename F&gt;<br>    Task(F&amp;&amp; f)<br>    {<br>        d_task = std::forward&lt;F&gt;(f);<br>    }<br><br>    template &lt;typename... LclArgs&gt;<br>    typename Executor::TaskResult_t invoke(LclArgs&amp;&amp;... lclArgs) const<br>    {<br>        std::cout &lt;&lt; &quot;ASync&quot; &lt;&lt; std::endl;<br>        return d_task(std::forward&lt;LclArgs&gt;(lclArgs)...).get();<br>    }<br><br>  private:<br>    std::function&lt;std::future&lt;FunctionReturn&gt;(Args...)&gt; d_task;<br>};</pre><p>Here, the template specialization differentiates the function type (synchronous or asynchronous) based on the IsFuture flag.</p><h3>Wrapping It All Together</h3><p>Finally, we define the full <strong>Executor</strong> class:</p><pre>template &lt;typename Signature&gt;<br>class Executor;<br><br>template &lt;typename Ret, typename... Args&gt;<br>class Executor&lt;Ret(Args...)&gt; {<br>public:<br>    using ArgsPack_t                = typename FunctionInfo&lt;Ret, Args...&gt;::ArgsPack;<br>    using TaskResult_t              = typename FunctionInfo&lt;Ret, Args...&gt;::ReturnType;    <br>    constexpr static bool IsFuture  = FunctionInfo&lt;Ret, Args...&gt;::IsFuture;<br>    using Task_t                    = Task&lt;Executor&lt;Ret(Args...)&gt;, FunctionInfo&lt;Ret, Args...&gt;::IsFuture, Ret(Args...)&gt;;<br>    <br>    template &lt;typename F&gt;<br>    Executor(F&amp;&amp; fn)<br>        : d_task(fn) {}<br><br>    template &lt;typename... LclArgs&gt;<br>    TaskResult_t execute(LclArgs... lclArgs) {<br>        return d_task.invoke(std::forward&lt;LclArgs&gt;(lclArgs)...);<br>    }<br>    <br>private:<br>    Task_t d_task;<br>};</pre><h4>Full Example</h4><pre>int add() { return 10; }<br><br>std::future&lt;int&gt; addAsync() { <br>    return std::async(std::launch::deferred, []() { return 100; }); <br>}<br><br>int main() {<br>    // Synchronous execution<br>    Executor&lt;int()&gt; a_executor(add);<br>    std::cout &lt;&lt; a_executor.execute() &lt;&lt; std::endl;<br>    <br>    // Asynchronous execution<br>    Executor&lt;std::future&lt;int&gt;()&gt; a_executorAsync(addAsync);<br>    std::cout &lt;&lt; a_executorAsync.execute() &lt;&lt; std::endl;<br>}</pre><h4>Conclusion</h4><p>By using <strong>template specialization</strong>, we can implement an efficient and flexible executor without incurring the runtime cost of virtual functions and vtable lookups. This approach also opens up the possibility of adding coroutine support by checking at compile-time if the function is awaitable.</p><p>If you’re interested in coroutine support or need further details, feel free to ask!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7cb946620159" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[C++ Coroutine ~ Deep dive]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://maharajan-ses.medium.com/c-coroutine-deep-dive-dd3fc0d57708?source=rss-9e82e027b363------2"><img src="https://cdn-images-1.medium.com/max/1135/0*NAPgIXlCQ5P9Wstq.png" width="1135"></a></p><p class="medium-feed-snippet">What is a Coroutine?</p><p class="medium-feed-link"><a href="https://maharajan-ses.medium.com/c-coroutine-deep-dive-dd3fc0d57708?source=rss-9e82e027b363------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://maharajan-ses.medium.com/c-coroutine-deep-dive-dd3fc0d57708?source=rss-9e82e027b363------2</link>
            <guid isPermaLink="false">https://medium.com/p/dd3fc0d57708</guid>
            <category><![CDATA[asynchronous-programming]]></category>
            <category><![CDATA[coroutine]]></category>
            <category><![CDATA[cplusplus]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[asynchronous]]></category>
            <dc:creator><![CDATA[Maharajan Shunmuga Sundaram]]></dc:creator>
            <pubDate>Mon, 23 Oct 2023 13:01:42 GMT</pubDate>
            <atom:updated>2023-10-23T13:01:42.785Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Boost ASIO thread pool]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://maharajan-ses.medium.com/boost-asio-thread-pool-687c4fb39c03?source=rss-9e82e027b363------2"><img src="https://cdn-images-1.medium.com/max/1024/0*BKg1irY2AyXiTwAz" width="1024"></a></p><p class="medium-feed-snippet">Abstract</p><p class="medium-feed-link"><a href="https://maharajan-ses.medium.com/boost-asio-thread-pool-687c4fb39c03?source=rss-9e82e027b363------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://maharajan-ses.medium.com/boost-asio-thread-pool-687c4fb39c03?source=rss-9e82e027b363------2</link>
            <guid isPermaLink="false">https://medium.com/p/687c4fb39c03</guid>
            <category><![CDATA[asio]]></category>
            <category><![CDATA[thread-pool]]></category>
            <category><![CDATA[job-scheduling]]></category>
            <category><![CDATA[boost]]></category>
            <category><![CDATA[cpp]]></category>
            <dc:creator><![CDATA[Maharajan Shunmuga Sundaram]]></dc:creator>
            <pubDate>Sun, 22 Nov 2020 04:24:22 GMT</pubDate>
            <atom:updated>2024-10-23T13:15:36.020Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Apache Pulsar — System Design]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://maharajan-ses.medium.com/apache-pulsar-system-design-e0461aab0133?source=rss-9e82e027b363------2"><img src="https://cdn-images-1.medium.com/max/717/0*wwjpVSUOpW51ypeH.jpeg" width="717"></a></p><p class="medium-feed-snippet">Introduction</p><p class="medium-feed-link"><a href="https://maharajan-ses.medium.com/apache-pulsar-system-design-e0461aab0133?source=rss-9e82e027b363------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://maharajan-ses.medium.com/apache-pulsar-system-design-e0461aab0133?source=rss-9e82e027b363------2</link>
            <guid isPermaLink="false">https://medium.com/p/e0461aab0133</guid>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[system-design-project]]></category>
            <category><![CDATA[apache-pulsar]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <dc:creator><![CDATA[Maharajan Shunmuga Sundaram]]></dc:creator>
            <pubDate>Mon, 16 Nov 2020 03:21:44 GMT</pubDate>
            <atom:updated>2020-11-22T00:07:48.119Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Abstract Factory]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-snippet">Problem</p><p class="medium-feed-link"><a href="https://maharajan-ses.medium.com/abstract-factory-50627e81b0bc?source=rss-9e82e027b363------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://maharajan-ses.medium.com/abstract-factory-50627e81b0bc?source=rss-9e82e027b363------2</link>
            <guid isPermaLink="false">https://medium.com/p/50627e81b0bc</guid>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[abstract-factory]]></category>
            <category><![CDATA[design-patterns]]></category>
            <dc:creator><![CDATA[Maharajan Shunmuga Sundaram]]></dc:creator>
            <pubDate>Sat, 31 Oct 2020 21:32:01 GMT</pubDate>
            <atom:updated>2020-11-21T19:14:05.864Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Producer Consumer Problem]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://maharajan-ses.medium.com/producer-consumer-problem-b325b7f49637?source=rss-9e82e027b363------2"><img src="https://cdn-images-1.medium.com/max/1500/0*38S_R1-9DekrLvXs.jpg" width="1500"></a></p><p class="medium-feed-snippet">Problem</p><p class="medium-feed-link"><a href="https://maharajan-ses.medium.com/producer-consumer-problem-b325b7f49637?source=rss-9e82e027b363------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://maharajan-ses.medium.com/producer-consumer-problem-b325b7f49637?source=rss-9e82e027b363------2</link>
            <guid isPermaLink="false">https://medium.com/p/b325b7f49637</guid>
            <category><![CDATA[cpp11]]></category>
            <category><![CDATA[threading]]></category>
            <category><![CDATA[cpp20]]></category>
            <category><![CDATA[cpp]]></category>
            <dc:creator><![CDATA[Maharajan Shunmuga Sundaram]]></dc:creator>
            <pubDate>Mon, 26 Oct 2020 00:35:09 GMT</pubDate>
            <atom:updated>2020-10-26T00:35:09.931Z</atom:updated>
        </item>
    </channel>
</rss>