<?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 Chelbiahmed on Medium]]></title>
        <description><![CDATA[Stories by Chelbiahmed on Medium]]></description>
        <link>https://medium.com/@chelbiahmed501?source=rss-a2c04884838b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Chelbiahmed on Medium</title>
            <link>https://medium.com/@chelbiahmed501?source=rss-a2c04884838b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 13:48:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chelbiahmed501/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[My Journey into Linux Kernel Development: From First Steps to First Contribution! ]]></title>
            <link>https://medium.com/@chelbiahmed501/my-journey-into-linux-kernel-development-from-first-steps-to-first-contribution-1431e990be3e?source=rss-a2c04884838b------2</link>
            <guid isPermaLink="false">https://medium.com/p/1431e990be3e</guid>
            <category><![CDATA[linux-kernel]]></category>
            <category><![CDATA[systems-programming]]></category>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[kernel-development]]></category>
            <dc:creator><![CDATA[Chelbiahmed]]></dc:creator>
            <pubDate>Tue, 03 Feb 2026 01:55:38 GMT</pubDate>
            <atom:updated>2026-02-03T02:16:27.922Z</atom:updated>
            <content:encoded><![CDATA[<h4>“How I went from feeling intimidated to making my first kernel contribution and what I learned along the way…”</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/441/1*pLIwT8EALkdA46ezx9Otyw.png" /></figure><p>A few months ago, the words “Linux kernel development” would have made me freeze. The kernel felt like this massive, untouchable codebase , something only the most elite developers could work on. Fast forward to today, and while it’s still challenging, it’s become <strong>exciting</strong> rather than intimidating.</p><p>I’m thrilled to share that I’ve completed <strong>LFD103: A Beginner’s Guide to Linux Kernel Development</strong> from the Linux Foundation and made my first contribution to the Linux kernel. Yes, it was “just” fixing spelling errors in the documentation, but let me tell you that small step taught me more than I ever expected.</p><h4>Why the Linux Kernel?<strong>Why Now?</strong></h4><p>Here’s the thing: <strong>I’m on a mission to master C/C++ and low-level systems programming.</strong> I wanted to work on something real, something that would push me to understand how computers actually work under the hood not just abstract concepts, but the actual machinery.</p><h4><strong>The Overwhelming First Steps (And How I Got Past Them)</strong></h4><p>Let me be honest: <strong>the first time I cloned the kernel repository, I felt lost.</strong>The codebase is <em>massive</em>. Millions of lines of code. Thousands of contributors. Decades of history. Where do you even begin?</p><p>I relied on two main resources:</p><ul><li><a href="https://docs.kernel.org/"><strong>The official Linux Kernel documentation</strong></a></li><li><a href="https://training.linuxfoundation.org/training/a-beginners-guide-to-linux-kernel-development-lfd103/"><strong>LFD103 course</strong></a><strong> </strong>which broke down the intimidation into manageable chunks</li></ul><p>1. <strong>I learned the workflow, </strong>how to submit patches, how to communicate with maintainers, how the Linux Kernel Mailing List (LKML) works</p><p>2. <strong>I built confidence, </strong>every small contribution proved I could do this</p><h4><strong>My First Contribution: Small But Significant</strong></h4><p>My first real contribution was simple:</p><p>- Find spelling errors and inconsistencies using `git grep` and manual checks</p><p>- Submit patches through LKML following the strict contribution guidelines</p><p>Every contribution to the kernel matters because millions of developers rely on that documentation. A clearer guide helps the next person who’s standing where I was a few months ago.</p><p><strong>Building and Booting My First Kernel: The Real Test</strong></p><p>Theory is one thing. Actually building and booting a custom kernel? That’s where things get real.</p><p>I took on the <strong>LFX Mentorship Task</strong> to build and boot Linus Torvalds’ latest kernel tree. This wasn’t just a checkbox exercise, it was a deep dive into: <strong>The Technical Journey</strong></p><p><strong>1. Setting Up the Environment</strong></p><ul><li>Cloned the kernel from Google’s mirror (faster than “kernel.org” for me)</li></ul><pre>$ git clone &#39;https://kernel.googlesource.com/pub/scm/linux/kernel/git/torvalds/linux.git&#39;</pre><ul><li>Installed all build dependencies on Ubuntu 22.04</li></ul><pre>sudo apt install libncurses-dev gawk flex bison openssl libssl-dev dkms libelf-dev libudev-dev libpci-dev libiberty-dev autoconf llvm</pre><p><strong>2. Configuration Challenges</strong></p><p>The `<em>make menuconfig</em>` step taught me patience. Ubuntu’s kernel has Debian-specific configurations that don’t exist in mainline. I had to:</p><p>- Remove references to `debian/canonical-certs.pem`</p><p>- Clean up certificate configurations</p><p><strong>3. The Build Process</strong></p><pre>$ make -j$(nproc) 2&gt;&amp;1 | tee build.log</pre><p>Watching the kernel compile was mesmerizing. Thousands of files, millions of lines, all coming together.</p><p><strong>4. The Critical Detail: Module Stripping</strong></p><p>Here’s something I learned the hard way: if you don’t strip modules during installation, your `initrd.image` explodes in size and the system won’t boot. The solution:</p><pre>$ sudo make INSTALL_MOD_STRIP=1 modules_install</pre><p>Finally, install the kernel with “<em>sudo make install”</em></p><p><strong>5. Boot the kernel</strong></p><p>Using `grub-reboot` to select my custom kernel, rebooting, and seeing:</p><pre>6.19.0-rc7 #1 SMP PREEMPT_DYNAMIC Mon Jun 29 12:38:59 CST 2026 x86_64 x86_64 x86_64 GNU/Linux</pre><p><strong>What’s Next?</strong></p><p>This is just the beginning. I’m continuing to:</p><p>- Explore different kernel subsystems</p><p>- Take on more substantial contributions</p><p>- Engage more with the kernel community</p><p><strong>A Message to Recruiters and Fellow Developers</strong></p><ul><li><strong>If you’re a recruiter or team lead</strong> looking for someone who’s curious, motivated, and serious about low-level programming, someone who doesn’t just talk the talk but walks the walk, I’d love to connect.</li><li><strong>If you’re a developer with open-source experience</strong>, especially in kernel development, I’d genuinely appreciate your advice. What should I focus on next? What mistakes did you make that could be avoided?</li></ul><p><strong>Resources That Helped Me</strong></p><p>- <a href="https://training.linuxfoundation.org/training/a-beginners-guide-to-linux-kernel-development-lfd103/">LFD103: A Beginner’s Guide to Linux Kernel Development (Linux Foundation)</a></p><p>- <a href="https://docs.kernel.org/">Official Linux Kernel Documentation (kernel.org)</a></p><p>- <a href="https://wiki.ubuntu.com/Kernel/BuildYourOwnKernel">Ubuntu BuildYourOwnKernel Wiki</a></p><p>- <a href="https://lkml.org/">The Linux Kernel Mailing List (LKML)</a></p><p><strong>Still learning. Still contributing. Still enjoying the process.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1431e990be3e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Device Tree vs ACPI: Why Embedded Linux Needs It but x86 PCs Don’t]]></title>
            <link>https://medium.com/@chelbiahmed501/device-tree-vs-acpi-why-embedded-linux-needs-it-but-x86-pcs-dont-bc63fc20a84f?source=rss-a2c04884838b------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc63fc20a84f</guid>
            <category><![CDATA[acpi]]></category>
            <category><![CDATA[linux-boot-process]]></category>
            <category><![CDATA[x86]]></category>
            <dc:creator><![CDATA[Chelbiahmed]]></dc:creator>
            <pubDate>Fri, 23 Jan 2026 08:03:04 GMT</pubDate>
            <atom:updated>2026-01-26T11:08:55.723Z</atom:updated>
            <content:encoded><![CDATA[<h3>Stage 1: Why the Kernel Needs Hardware Knowledge</h3><p>When a Linux kernel boots, it must know what hardware exists and how to talk to it. But here’s the tricky part: the kernel itself doesn’t magically know where the UART controller is, or how many I²C buses are wired.</p><p>Think about it: without a description, the kernel would be blind. That’s why embedded systems rely on the <strong>Device Tree (DT)</strong> : a hardware description file that tells the kernel exactly what components are present and how they’re connected.</p><p>On x86 PCs, however, decades of standardization mean the kernel can rely on <strong>firmware interfaces</strong> like BIOS, UEFI, ACPI tables, and PCI bus enumeration. These mechanisms already describe the hardware in a consistent way, so no DT is needed.</p><h3>Stage 2: Booting with Device Tree (Embedded)</h3><p>In an embedded boot process, once the bootloader (often U‑Boot) has brought up RAM and basic hardware, it loads:</p><ul><li>The Linux kernel binary</li><li>The Device Tree Blob (DTB)</li></ul><p>The DTB is passed to the kernel at startup. The kernel then parses this tree to:</p><ul><li>Identify CPU cores, memory regions, and interrupts</li><li>Locate buses like I²C, SPI, and CAN</li><li>Initialize peripherals such as UART, Ethernet, and GPIO</li></ul><p>Without the DT, the kernel would have no way of knowing how to interact with the hardware on the board.</p><h3>Stage 3: Booting with ACPI/PCI (x86 PCs)</h3><p>On a PC, the story is different. The firmware provides <strong>ACPI tables</strong>, and the kernel can probe the <strong>PCI bus</strong> to discover devices dynamically:</p><ul><li><strong>ACPI (Advanced Configuration and Power Interface)</strong>: Describes system devices, CPU topology, power states, and configuration. It standardizes how the OS interacts with hardware.</li><li><strong>PCI (Peripheral Component Interconnect)</strong>: A bus standard that allows the kernel to automatically discover connected devices and their resources.</li></ul><p>Together, ACPI and PCI give the kernel a complete picture of the hardware without requiring a Device Tree.</p><h3>Stage 4: Trade-offs in Practice</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*dwCj37LkJt0AErfjxiv9Ew.png" /></figure><h3>Conclusion</h3><p>The difference isn’t arbitrary, it reflects decades of architectural evolution:</p><ul><li>Embedded platforms need DT because hardware is diverse and non-standard.</li><li>x86 PCs rely on ACPI and PCI, which provide a standardized discovery mechanism.</li></ul><h4>Official References</h4><p>If you’d like to dive deeper into how Linux discovers and describes hardware, here are some authoritative resources:</p><p><strong>Linux Device Tree — Official Kernel Documentation</strong><br> <a href="https://docs.kernel.org/devicetree/usage-model.html">https://docs.kernel.org/devicetree/usage-model.html</a></p><p><strong>ACPI Support in the Linux Kernel</strong><br> <a href="https://docs.kernel.org/firmware-guide/acpi/index.html">https://docs.kernel.org/firmware-guide/acpi/index.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc63fc20a84f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Embedded Linux Boot Process]]></title>
            <link>https://medium.com/@chelbiahmed501/embedded-linux-boot-process-5061b78623b3?source=rss-a2c04884838b------2</link>
            <guid isPermaLink="false">https://medium.com/p/5061b78623b3</guid>
            <category><![CDATA[u-boot]]></category>
            <category><![CDATA[userspace]]></category>
            <category><![CDATA[linux-boot-process]]></category>
            <category><![CDATA[boot-time-optimization]]></category>
            <category><![CDATA[embedded-linux]]></category>
            <dc:creator><![CDATA[Chelbiahmed]]></dc:creator>
            <pubDate>Thu, 25 Dec 2025 09:26:28 GMT</pubDate>
            <atom:updated>2025-12-25T09:26:28.231Z</atom:updated>
            <content:encoded><![CDATA[<h3>Stage 1: RAM initialization</h3><p>When working with an embedded target, the CPU normally fetches code from <strong>RAM</strong> and executes it. But when we <strong>first power on the board</strong>, how does the CPU even reach that point if <strong>RAM is not yet initialized</strong>?</p><p>Think about it: to use RAM, the CPU must <strong>configure it properly</strong> according to its datasheet (setting timings, voltages, refresh cycles..) That means <strong>some code must run before RAM is ready</strong> to prepare it for normal operation.</p><p>Here’s the tricky part:</p><ul><li>If RAM is not yet initialized, <strong>where does the CPU fetch this first code from?</strong></li><li>And <strong>who writes it</strong>?</li></ul><p>That’s why modern <strong>SoCs</strong> include a small, built-in memory directly attached to the CPU, often called <strong>internal SRAM</strong>. This memory allows a very small program to run immediately at startup.</p><p>The role of this first program is simple but <strong>crucial</strong>: it initializes the <strong>external RAM</strong>, ensuring the system has working memory for the next stages of boot.</p><p>This program is usually called the <strong>SPL (Secondary Program Loader)</strong>. It knows exactly how to configure the RAM controller for the target board. But to execute the SPL, it first needs to be loaded into the internal SRAM.</p><p>Here’s where the <strong>ROM Code</strong> comes into play. The ROM Code is a tiny piece of software <strong>burned into the SoC at manufacturing time by the chip vendor </strong>it cannot be modified. Its responsibilities include:</p><ul><li>Locating the SPL in predefined storage devices (SD card, eMMC, SPI flash, USB, etc.)</li><li>Loading the SPL into the internal SRAM</li><li>Jumping to the SPL to start the next stage of boot</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*WXLrvWIW0vfrL99FpVxbbQ.png" /><figcaption><strong>RAM Initialization</strong></figcaption></figure><h3>Stage 2 : Loading the Bootloader</h3><p>Once the SPL has initialized the RAM, it can load a bigger program into it: the main bootloader, often called U-Boot in the embedded Linux world.<br>Why do we need this second stage? Because the SPL is intentionally tiny. It only has one mission: bring up RAM (and some essential HW).</p><p>But once we have working RAM, we can run a much larger program with many more features. U-Boot provides:</p><ul><li>Kernel loading and verification: Ensures the Linux kernel, device tree, and initramfs are correctly loaded and validated before execution.</li><li>Filesystem support: Understands FAT, EXT, and other formats so it can locate kernel images on various storage devices.</li><li>Network booting: Supports TFTP, NFS, and other protocols to fetch the kernel remotely, useful for development or diskless systems.</li><li>Environment variables: Stores flexible configuration values (e.g., which kernel to boot, where to find the device tree), making the boot process adaptable without recompiling.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*WcPD-V5o8u1zJY61K_5aiQ.png" /><figcaption>Loading the Bootloader</figcaption></figure><h3>Stage 3 : Booting the Linux Kernel</h3><p>By this point, <strong>U‑Boot has completed its mission</strong>. It has loaded the Linux kernel binary and the Device Tree Blob (DTB) into RAM, and then passed execution over to the kernel. From here on, the <strong>Linux kernel takes full control</strong> of the system.</p><h4>Why the Device Tree Matters ?</h4><p>Unlike a traditional PC, an embedded board doesn’t have built‑in mechanisms to automatically inform the operating system about its hardware. That’s where the <strong>Device Tree</strong> comes in.</p><p>The Device Tree is essentially a <strong>hardware description file</strong> that tells the kernel exactly what it needs to know, including:</p><ul><li><strong>Available peripherals</strong>: I2C, SPI, UART, GPIO, CAN, and more.</li><li><strong>Memory‑mapped addresses</strong>: The specific locations in memory where these peripherals can be accessed.</li></ul><p>Without this information, the kernel would have no way of knowing how to interact with the hardware on the board.</p><h3>Stage 3: What the Kernel Does After Taking Control</h3><p>Once U‑Boot hands over execution, the <strong>Linux kernel takes charge</strong> of the system. Its responsibilities at this stage include:</p><p><strong>CPU Scheduling &amp; Memory Management</strong> The kernel sets up how tasks are scheduled on the CPU and manages system memory.</p><p><strong>Device Driver Initialization</strong> Using the <strong>Device Tree</strong>, the kernel loads the correct drivers for the hardware.</p><ul><li>Brings up buses like I²C, SPI, and CAN.</li><li>Initializes communication peripherals such as UART and Ethernet.</li></ul><p><strong>Mounting the Root Filesystem</strong> The kernel locates the root filesystem image (from NAND flash, SD card, or even NFS over Ethernet).</p><ul><li>It mounts this filesystem as /.</li><li>This step gives Linux access to user‑space binaries, libraries, and configuration files.</li></ul><h3>Stage 4: Starting the User Space</h3><p>At this point, the kernel is fully awake the CPU, memory, and hardware drivers are initialized. But here’s the catch: <strong>the kernel itself doesn’t provide a user‑friendly environment</strong>. That’s where user space begins.</p><p>Here’s what happens next:</p><ol><li><strong>Launching the First Process</strong> The kernel starts the very first process, usually called init.</li><li><strong>Starting Essential Services</strong> init brings up system services needed for the OS to function.</li><li><strong>Shell or Graphical Interface</strong> A command‑line shell or graphical desktop environment is launched, giving humans a way to interact.</li><li><strong>Running User Applications</strong> Finally, user programs and applications can execute, completing the boot journey.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/379/1*BlvfP7seF3Xc8bo0ek0OhQ.png" /><figcaption>Fundamental Architecture of Linux</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5061b78623b3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding FIFOs (Named Pipes) in Unix Processes with C]]></title>
            <link>https://medium.com/@chelbiahmed501/understanding-fifos-named-pipes-in-unix-processes-with-c-9e215c08bcb2?source=rss-a2c04884838b------2</link>
            <guid isPermaLink="false">https://medium.com/p/9e215c08bcb2</guid>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[fifo]]></category>
            <category><![CDATA[unix]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[ipc]]></category>
            <dc:creator><![CDATA[Chelbiahmed]]></dc:creator>
            <pubDate>Wed, 10 Dec 2025 11:33:34 GMT</pubDate>
            <atom:updated>2025-12-10T11:35:58.643Z</atom:updated>
            <content:encoded><![CDATA[<p>In Linux, unnamed pipes are useful for communication between related processes (parent-child), but if we need communication between unrelated processes that’s where FIFOs (First In,First Out) or named pipes are used.</p><p>In this article, we’ll explore <strong>what FIFOs are, why they are useful, how they work</strong>, and how to use them in <strong>C with a concrete example</strong>.</p><h3>Why Use FIFOs?</h3><p>Linux provides <strong>unnamed pipes</strong> for communication between related processes, typically a <strong>parent and its child</strong>. These pipes are created in memory and disappear once the processes terminate.</p><p>But what if:</p><ul><li>The processes are <strong>unrelated</strong>?</li><li>They are started <strong>independently</strong>?</li><li>They need to communicate through a <strong>known and persistent channel</strong>?</li></ul><p>This is exactly the problem that <strong>FIFOs (First In, First Out)</strong>, also known as <strong>named pipes</strong> are designed to solve.</p><h3>What Is a FIFO?</h3><p>A <strong>FIFO</strong> is a special type of file that exists in the filesystem and acts as a communication channel between processes.</p><p>Key characteristics:</p><ul><li>It appears as a <strong>file</strong>, but it does <strong>not store data on disk</strong></li><li>Any process with proper permissions can access it</li><li>Data is read in the same order it is written (<strong>First In, First Out</strong>)</li></ul><p>A FIFO has two ends:</p><ul><li><strong>Write end</strong> → for sending data</li><li><strong>Read end</strong> → for receiving data</li></ul><p>This makes FIFOs a <strong>unidirectional IPC mechanism</strong>.</p><h3>Blocking Behavior (Very Important)</h3><p>By default, FIFOs are <strong>blocking</strong>, which means:</p><ul><li>Opening the FIFO for <strong>reading</strong> blocks until another process opens it for <strong>writing</strong></li><li>Writing to a FIFO blocks until there is a <strong>reader</strong></li></ul><p>This behavior is useful for synchronization, but it also introduces a risk.</p><h3>⚠️ Deadlocks</h3><p>If two processes wait indefinitely for each other (for example, both waiting to read), the program will hang. This situation is known as a <strong>deadlock</strong>.</p><p>✅ To avoid deadlocks, processes must be carefully coordinated, or non-blocking modes should be used when appropriate.</p><h3>How FIFOs work</h3><p>To work with FIFO mechanism, we have to create a fifo file. This file can be created using the mkfifo command or the mkfifo() system call in C</p><h4>1. From the Shell</h4><pre>mkfifo my_fifo</pre><h4>2. From C (using a system call)</h4><pre>int mkfifo(const char *pathname, mode_t mode);<br>#example: int mkfifo(&quot;my_fifo&quot;, 0666);</pre><blockquote>pathname: the name of the file to be created (or its path).<br>mode: specifies the permissions (modified by mode &amp; ~umask).</blockquote><p>mkfifo returns 0 (success of file creation) or -1 (file creation failed)</p><p>Once created, the FIFO behaves like a regular file and can be opened using open().</p><pre>int open(const char *pathname, int flags);<br>int open(const char *pathname, int flags, mode_t mode);</pre><blockquote>pathname : The path of the FIFO file.<br>flags : Specifies how the file should be opened,<br>O_RDONLY : Open the FIFO for read-only access.<br>O_WRONLY : Open the FIFO for write-only access.<br>O_RDWR : Open the FIFO for both read and write.<br>mode : Used when O_CREAT is specified, to define the permissions of the new file.</blockquote><h4>⚠️ Be Careful:</h4><p>Opening the read or write end of a FIFO blocks the communication until the other end is also opened (by another process or thread) : If we open a FIFO for writing, the open call hangs until another process comes and opens the same FIFO for reading, then the process that it’s opened for writing will continue. ==&gt; The two ends of the FIFO have to be opened.</p><pre>#include &lt;stdio.h&gt;<br>#include &lt;errno.h&gt;<br><br>int main()<br>{<br>    // Try to open (or create) a file &quot;file.txt&quot; for writing<br>    FILE *f = fopen(&quot;file.txt&quot;, &quot;w&quot;);<br>    <br>    if (f == NULL) {<br>        perror(&quot;Could not create file&quot;);<br>        return 1;<br>    }<br><br>    printf(&quot;Opened\n&quot;);<br><br>    if (fwrite(&amp;c, sizeof(c), 1, f) != 1) {<br>        perror(&quot;Write failed&quot;);<br>        fclose(f);<br>        return 2;<br>    }<br><br>    printf(&quot;Written\n&quot;);<br><br>    fclose(f);<br>    printf(&quot;Closed\n&quot;);<br><br>    return 0;<br>}</pre><pre>printf(&quot;Opened\n&quot;); // WRONLY :) The code will hang here if there is no reading processor</pre><p>Expected output:</p><pre>Opened<br>Written<br>Closed</pre><p>And a file.txt containing the single character ‘Z’ will be created.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9e215c08bcb2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Pipes in Unix Processes with C]]></title>
            <link>https://medium.com/@chelbiahmed501/understanding-pipes-in-unix-processes-with-c-cbfb21844492?source=rss-a2c04884838b------2</link>
            <guid isPermaLink="false">https://medium.com/p/cbfb21844492</guid>
            <category><![CDATA[embedded-linux]]></category>
            <category><![CDATA[learningpipes]]></category>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[ipc]]></category>
            <category><![CDATA[unix]]></category>
            <dc:creator><![CDATA[Chelbiahmed]]></dc:creator>
            <pubDate>Wed, 03 Dec 2025 08:47:42 GMT</pubDate>
            <atom:updated>2025-12-03T08:58:33.930Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>In Unix systems, a <em>pipe</em> is a powerful mechanism that enables communication between processes. It allows the output of one process to be used directly as the input of another, creating a simple but effective data-flow channel. Pipes support <strong>unidirectional</strong> data transfer, meaning data flows in only one direction at a time.</p><p>Unix provides two main types of pipes:</p><h3>1. Unnamed Pipes</h3><p>These pipes are used for communication between <em>related</em> processes typically a parent process and its child. They are created using the pipe() system call.</p><h3>2. Named Pipes (FIFOs)</h3><p>Unlike unnamed pipes, FIFOs allow communication between <em>unrelated</em> processes. They are created using the mkfifo() system call.<br> <em>I will explore Named Pipes in a dedicated, upcoming article.</em></p><h3>How Pipes Work</h3><p>When you create an unnamed pipe using pipe(), the kernel allocates a memory buffer that temporarily stores data transmitted between processes. This buffer has a system-defined fixed size.</p><p>The pipe() function fills an array with <strong>two file descriptors</strong>:</p><ul><li>pipefd[0] — the <strong>read end of the pipe</strong></li><li>pipefd[1] — the <strong>write end of the pipe</strong></li></ul><p>A writing process sends data using the write end, while a reading process retrieves data through the read end.</p><p>Some important behaviors to note:</p><ul><li>If the <strong>buffer becomes full</strong>, a write operation will block until space becomes available.</li><li>If the <strong>buffer is empty</strong>, a read operation will block until data is written.</li><li>Once communication is complete, each process must close the file descriptors it no longer needs. When all ends are closed, the kernel deallocates the pipe’s buffer.</li></ul><p><strong>Function prototype:</strong></p><pre>int pipe(int pipefd[2]);</pre><h3>Using Pipes for Inter-Process Communication (IPC)</h3><p>Pipes enable a unidirectional flow of data from one process to another, typically from a parent process to its child, or vice versa.</p><p>After creation of pipe we call fork() so the file descriptors will be copied over ==&gt; If we close a file descriptor in one process it remains open in the other process since they are independent. So for this communication to work properly, each process must close the ends of the pipe it doesn’t use. This prevents deadlocks and ensures that the pipe behaves as expected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/285/1*7bTn6n4BVabgf01Hgbf6Bw.png" /></figure><p>Reading from and writing to a pipe is done using the standard read() and write() system calls. When data is written to the pipe, it is stored in a kernel buffer. The reading process retrieves this data from the buffer. If the buffer becomes full, the writing process is blocked until space becomes available. Likewise, if the buffer is empty, the reading process blocks until there is data to read unless the write end has been closed, in which case read() returns 0 (end-of-file).</p><p>Example 1 : Parent writing to pipe, Child reading</p><pre>#include &lt;stdio.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;sys/types.h&gt;<br>#include &lt;sys/wait.h&gt;<br><br>int main() {<br>    int fd[2];   // fd[0] - read end, fd[1] - write end<br>    pid_t pid;<br>    char buffer[100];<br><br>    if (pipe(fd) == -1) {<br>        perror(&quot;An error occurred when opening the pipe&quot;);<br>        return 1;<br>    }<br><br>    pid = fork();<br>    if (pid &lt; 0) {<br>        perror(&quot;An error occurred with fork()&quot;);<br>        return 1;<br>    }<br><br>    if (pid &gt; 0) {<br>        // Parent process<br>        int x;<br>        close(fd[0]);  // Close unused read end<br><br>        printf(&quot;Input a number: &quot;);<br>        scanf(&quot;%d&quot;, &amp;x);<br><br>        if (write(fd[1], &amp;x, sizeof(int)) == -1) {<br>            printf(&quot;An error occurred when writing in the pipe\n&quot;);<br>            return 1;<br>        }<br><br>        close(fd[1]);  // Close write end after writing<br>        wait(NULL);    // Wait for child to finish<br>    } <br>    else {<br>        // Child process<br>        int y;<br>        close(fd[1]);  // Close unused write end<br><br>        if (read(fd[0], &amp;y, sizeof(int)) == -1) {<br>            printf(&quot;An error occurred when reading from the pipe\n&quot;);<br>            return 1;<br>        }<br><br>        printf(&quot;Child received: %d\n&quot;, y);<br>        close(fd[0]);  // Close read end after reading<br>    }<br><br>    return 0;<br>}</pre><p>The output will look like:</p><pre>Input a number: 42<br>Child received: 42</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cbfb21844492" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Unix Processes in C: A practical introduction to fork() and wait()]]></title>
            <link>https://medium.com/@chelbiahmed501/understanding-unix-processes-in-c-a-practical-introduction-to-fork-and-wait-4c437d80daff?source=rss-a2c04884838b------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c437d80daff</guid>
            <category><![CDATA[syscalls]]></category>
            <category><![CDATA[embedded-linux]]></category>
            <category><![CDATA[unix-processes]]></category>
            <dc:creator><![CDATA[Chelbiahmed]]></dc:creator>
            <pubDate>Mon, 01 Dec 2025 19:48:07 GMT</pubDate>
            <atom:updated>2025-12-01T19:48:07.285Z</atom:updated>
            <content:encoded><![CDATA[<p>In Linux, the fork() function creates a new process (child process) that is an exact duplicate of the calling process (parent process). After fork() is called, both the parent and child processes run concurrently, executing the same instructions that follow fork() call.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/629/1*w2l8adytyjusjnYZUBZsgQ.png" /></figure><h3>How fork() Works</h3><p>When a process calls fork(), the kernel creates a <strong>new process</strong> by duplicating the current one. This new process is known as the <strong>child</strong>, and the one that invoked fork() becomes the <strong>parent</strong>.</p><p>Both processes continue executing the same program, starting immediately after the fork() call.<br> From that point on, they run independently.</p><h3>Characteristics of the child process</h3><ul><li>It receives its <strong>own PID</strong>.</li><li>It starts with a <strong>copy</strong> of the parent’s memory and open file descriptors.</li><li>It executes the same instructions following fork(), but along a separate execution path.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/527/1*f1opXhxsajC1J9gfNdRv6g.png" /></figure><h3>What does fork() return ?</h3><ul><li>In the parent process it returns the child process id.</li><li>In the child process it returns 0.</li><li>In case of failure it returns -1 (e.g., insufficient system resources).</li></ul><p>The value returned by fork() helps us to determinate whether we’re in the parent process or the child process.<br>Example 1 :</p><pre>#include &lt;stdio.h&gt;<br>#include &lt;unistd.h&gt;<br><br>int main() {<br>    pid_t pid = fork();<br><br>    if (pid &lt; 0) {<br>        perror(&quot;fork failed&quot;);<br>    } else if (pid == 0) {<br>        printf(&quot;Child: my PID is %d, my parent is %d\n&quot;, getpid(), getppid());<br>    } else {<br>        printf(&quot;Parent: I created a child with PID %d\n&quot;, pid);<br>    }<br><br>    return 0;<br>}</pre><p>Two simple system calls help gather information about running processes:</p><ul><li>getpid() — returns the current process ID</li><li>getppid() — returns the parent process ID</li></ul><p>These are especially helpful when working with multiple child processes.</p><h3>What Happens When You Call fork() Multiple Times?</h3><p>A critical concept in process creation is that <strong>each process executing a </strong><strong>fork() will itself be duplicated</strong>.</p><p>For example:</p><pre>#include &lt;stdio.h&gt;<br>#include &lt;unistd.h&gt;<br><br>int main() {<br>    printf(&quot;Program start — PID %d\n&quot;, getpid());<br><br>    fork(); // First fork<br>    fork(); // Second fork<br><br>    printf(&quot;Process PID %d, Parent PID %d\n&quot;, getpid(), getppid());<br><br>    return 0;<br>}</pre><p>Execution flow:</p><ol><li>The <strong>first fork</strong> creates one child process.</li><li>The <strong>second fork</strong> is executed by both:</li></ol><ul><li>The original parent</li><li>The first child</li></ul><p>This produces four total processes:</p><ul><li>Parent</li><li>Child1</li><li>Child2 (created by parent)</li><li>Child3 (created by Child1)</li></ul><h4>General rule</h4><p>If a program calls fork() <strong>n times</strong>, it creates <strong>2ⁿ total processes</strong>.</p><h3>Preventing Zombie Processes using wait()</h3><p>When creating processes using fork() we have to manage the execution of these processes to prevent the occurrence of zombies processes. In fact, a zombie process is a process that has no parent. So we have to make sure that the parent process always wait for its child process to finish execution before starting executing. In this context, the wait() function stops the<br>execution of the parent until a child has finished executing.</p><p>The wait() function returns :</p><ul><li>-1 if there is no children processes to wait for.</li><li>The process ID of the child process we are waiting for.</li></ul><p>When using the wait() function, the parent process waits until one of the children finishes executing. So, if the parent has two children, c1 and c2, and c1 finishes while c2 doesn’t, the parent will be resumed. To avoid this, we need to check that there are no children left to wait for by verifying the return value of wait().</p><p>Example:</p><pre>#include &lt;stdio.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;sys/wait.h&gt;<br><br>int main() {<br>    pid_t pid = fork();<br><br>    if (pid == 0) {<br>        // Child<br>        printf(&quot;Child: PID %d executing...\n&quot;, getpid());<br>        sleep(1);<br>        printf(&quot;Child: finishing work.\n&quot;);<br>    } else {<br>        // Parent waits for child<br>        printf(&quot;Parent: waiting for child...\n&quot;);<br>        wait(NULL);<br>        printf(&quot;Parent: child has exited.\n&quot;);<br>    }<br><br>    return 0;<br>}</pre><h3>Execution Output</h3><pre>Parent: waiting for child...<br>Child: PID 6220 executing...<br>Child: finishing work.<br>Parent: child has exited.</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c437d80daff" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting Started with Unix Processes in C]]></title>
            <link>https://medium.com/@chelbiahmed501/getting-started-with-unix-processes-in-c-077304ff6480?source=rss-a2c04884838b------2</link>
            <guid isPermaLink="false">https://medium.com/p/077304ff6480</guid>
            <category><![CDATA[embedded-linux]]></category>
            <dc:creator><![CDATA[Chelbiahmed]]></dc:creator>
            <pubDate>Mon, 01 Dec 2025 08:31:50 GMT</pubDate>
            <atom:updated>2025-12-01T08:31:50.837Z</atom:updated>
            <content:encoded><![CDATA[<h3>What Is a Process?</h3><p>A <strong>process</strong> is simply a program in execution. Whenever you open an application or run a command, Linux creates a dedicated process to handle that execution.</p><p>Each process comes with a set of key characteristics:</p><ul><li><strong>Process ID (PID):</strong><br> A unique identifier assigned by the operating system to track and manage the process.</li><li><strong>Dedicated Memory Space:</strong><br> Every process operates within its own isolated memory region, ensuring that processes do not interfere with one another.</li><li><strong>Process State:</strong><br> In Linux, a process transitions between different execution states depending on its activity and resource requirements. These states include:</li></ul><blockquote><strong>Running: </strong>The process is currently executing instructions on the CPU.</blockquote><blockquote><strong>Waiting (Blocked):</strong> The process is paused while waiting for a specific event or resource, such as I/O completion.</blockquote><blockquote><strong>Terminated: </strong>The process has finished executing or has been stopped by the system or the user.</blockquote><p>The diagram below illustrates how a process transitions between these states during its lifecycle:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5nI7LhQwPHIEiq-QOLc_iQ.jpeg" /><figcaption>Process State</figcaption></figure><h3>How Are Processes Created in Linux?</h3><p>In Linux, process creation is primarily handled through two fundamental system calls:</p><ul><li><strong>fork():</strong>Creates an exact copy of the current (parent) process. The newly created process is known as the <strong>child process</strong> and receives its own PID.</li><li><strong>exec():</strong>Replaces the current process’s memory with a new program. After calling exec(), the process begins executing the new program while keeping the same PID.</li></ul><p>Together, fork() and exec() form the core of process creation in Unix-like systems.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/412/1*ORPXkR5d69d7FpXT3mJuGw.png" /></figure><h3>Inter-Process Communication (IPC)</h3><p>In many cases, processes need to exchange data or coordinate their behavior. Linux provides multiple <strong>IPC mechanisms</strong> to support communication and synchronization:</p><ul><li><strong>Pipes: </strong>Unidirectional communication channels typically used between a parent process and its child.</li><li><strong>FIFOs (Named Pipes): </strong>Similar to pipes but can be used by <strong>unrelated</strong> processes and support bidirectional communication.</li><li><strong>Shared Memory: </strong>A fast IPC method that allows multiple processes to access the same memory region.</li><li><strong>Signals: </strong>Lightweight notifications used to inform a process of asynchronous events (e.g., SIGINT, SIGKILL).</li><li><strong>Message Queues: </strong>Allow processes to exchange structured messages stored in a queue.</li></ul><h3>Why Are Processes Important?</h3><p>Processes play a crucial role in Linux for several reasons:</p><ul><li><strong>Isolation: </strong>Each process runs independently, so if one crashes, it does not affect others.</li><li><strong>Multitasking: </strong>The Linux scheduler enables the system to execute multiple processes efficiently and seemingly simultaneously.</li><li><strong>Security and Stability: </strong>Individual memory spaces and permissions prevent unauthorized access between processes.</li></ul><h3>Useful Commands for Managing Processes</h3><ul><li><strong>ps</strong> – Displays a list of currently running processes.</li><li><strong>top / </strong><strong>htop</strong> – Shows real-time system activity and process usage.</li><li><strong>kill</strong> – Sends a signal to terminate or control a process.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=077304ff6480" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>