<?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"><channel><title><![CDATA[Fairuz Codes]]></title><description><![CDATA[Fairuz Codes]]></description><link>https://fairuzcodes.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 15 Sep 2026 21:54:42 GMT</lastBuildDate><atom:link href="https://fairuzcodes.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[I Built a Spotify Clone in C (To Learn Operating Systems the Hard Way)]]></title><description><![CDATA[Most people learn Operating Systems from textbooks. I decided to learn it by building a music streaming service from scratch.
My goal was simple: Create a system where a Python Server streams raw audio to a C Client in real-time, played entirely insi...]]></description><link>https://fairuzcodes.hashnode.dev/i-built-a-spotify-clone-in-c-to-learn-operating-systems-the-hard-way</link><guid isPermaLink="true">https://fairuzcodes.hashnode.dev/i-built-a-spotify-clone-in-c-to-learn-operating-systems-the-hard-way</guid><category><![CDATA[C]]></category><category><![CDATA[operatingsystems ]]></category><category><![CDATA[systemprogramming]]></category><category><![CDATA[linux for beginners]]></category><dc:creator><![CDATA[Fairuz]]></dc:creator><pubDate>Wed, 21 Jan 2026 05:20:04 GMT</pubDate><content:encoded><![CDATA[<hr />
<p>Most people learn Operating Systems from textbooks. I decided to learn it by building a music streaming service from scratch.</p>
<p>My goal was simple: Create a system where a <strong>Python Server</strong> streams raw audio to a <strong>C Client</strong> in real-time, played entirely inside the terminal.</p>
<p>No high-level audio libraries. No frameworks. Just raw bytes, TCP sockets, and the chaos of multithreading.</p>
<h3 id="heading-the-challenge"><strong>THE CHALLENGE</strong></h3>
<p>Streaming music sounds easy: Download file</p>
<p>Play sound.</p>
<p>But if you do this in a single loop, the UI freezes while downloading. If you try to do both at once without safeguards, you get a <strong>Race Condition</strong>—the audio glitches, memory corrupts, and the app crashes.</p>
<p>To make this work, I had to engineer a manual <strong>Producer-Consumer Architecture</strong>.</p>
<h3 id="heading-how-it-works-under-the-hood">How It Works under the Hood</h3>
<p>The system relies on two distinct threads fighting over a single piece of shared memory.</p>
<ol>
<li><p><strong>The Producer (Network Thread):</strong> Grabs data from the internet.</p>
</li>
<li><p><strong>The Consumer (Audio Thread):</strong> Feeds that data to the speakers.</p>
</li>
</ol>
<h3 id="heading-the-ring-buffer-strategy">The "Ring Buffer" Strategy</h3>
<p>I couldn't load the whole song into RAM. Instead, I built a <strong>Circular Buffer</strong>. Imagine a bucket that holds 1MB of audio. The downloader fills it, and the player empties it. When they reach the end, they wrap back around to the start continuously.</p>
<h3 id="heading-the-traffic-light-mutexes">The Traffic Light (Mutexes)</h3>
<p>This was the hardest part. If the Downloader tries to write to memory at the <em>exact same nanosecond</em> the Player tries to read from it, the system crashes.</p>
<p>I implemented <strong>Mutex Locks</strong>, which act like a traffic light. They force one thread to wait while the other touches the memory, ensuring perfectly synchronized, glitch-free audio.</p>
<h3 id="heading-the-tech-stack">The Tech Stack</h3>
<ul>
<li><p><strong>Language:</strong> C (Client) &amp; Python (Server)</p>
</li>
<li><p><strong>Networking:</strong> Raw Linux TCP Sockets</p>
</li>
<li><p><strong>Concurrency:</strong> POSIX Threads (pthreads)</p>
</li>
<li><p><strong>Audio:</strong> SDL2 (PCM Output)</p>
</li>
</ul>
<h3 id="heading-whats-next">What's Next?</h3>
<p>This project handles the <strong>Operating System</strong> layer. In future posts, I plan to expand this into a full distributed system:</p>
<ol>
<li><p><strong>The Cloud:</strong> Moving the server to <strong>AWS</strong> for real remote streaming.</p>
</li>
<li><p><strong>The AI:</strong> Building a Neural Network to recommend the next song.</p>
</li>
</ol>
<h3 id="heading-try-it-yourself">Try It Yourself</h3>
<p>I made this open source so you can run it on your Mac or Linux machine.</p>
<p><a target="_blank" href="https://github.com/fairuzanika-11/spotify-terminal-clone.git">https://github.com/fairuzanika-11/spotify-terminal-clone.git</a></p>
]]></content:encoded></item></channel></rss>