<?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[ByteCraft Journal]]></title><description><![CDATA[A journal of things I learn, build, and discover while exploring technology, software, and engineering.]]></description><link>https://bytecraft-journal.hashnode.dev</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1593680282896/kNC7E8IR4.png</url><title>ByteCraft Journal</title><link>https://bytecraft-journal.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Mon, 31 Aug 2026 19:55:35 GMT</lastBuildDate><atom:link href="https://bytecraft-journal.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Day 1: Understanding What Happens When a Program Runs]]></title><description><![CDATA[I've worked with JavaScript and Node.js before, but whenever I try to recall what actually happens behind the code, I find myself going back to the same basic questions.
What happens when I run a prog]]></description><link>https://bytecraft-journal.hashnode.dev/day-1-understanding-what-happens-when-a-program-runs</link><guid isPermaLink="true">https://bytecraft-journal.hashnode.dev/day-1-understanding-what-happens-when-a-program-runs</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Node.js]]></category><category><![CDATA[programming fundamentals]]></category><dc:creator><![CDATA[Akshat Gupta]]></dc:creator><pubDate>Sat, 29 Aug 2026 20:34:37 GMT</pubDate><content:encoded><![CDATA[<p>I've worked with JavaScript and Node.js before, but whenever I try to recall what actually happens behind the code, I find myself going back to the same basic questions.</p>
<p><strong>What happens when I run a program? Where does the code actually run? What is a process? What is a thread?</strong></p>
<p>I realized that I was trying to understand higher-level concepts without having a clear picture of these fundamentals.</p>
<p>So today, instead of memorizing more definitions, I decided to go back to the basics and understand what actually happens when a program runs.</p>
<p>Let's start from the simplest question:</p>
<h3><strong>What actually happens when a program runs?</strong></h3>
<h6>Lets first understand a simple program</h6>
<p><strong>A program is essentially a set of instructions or instructions that tells computer what to do.</strong></p>
<h4>Lets forget computers for a moment and think about your body.</h4>
<blockquote>
<p>Your body can perform many activities:</p>
<ul>
<li><p>walk</p>
</li>
<li><p>run</p>
</li>
<li><p>lift</p>
</li>
<li><p>eat</p>
</li>
<li><p>speak</p>
</li>
<li><p>hear</p>
</li>
<li><p>breathe</p>
</li>
<li><p>sleep</p>
</li>
</ul>
</blockquote>
<p>But you body does not perform these activities randomly. They are coordinated by your brain.</p>
<blockquote>
<p><em><strong>For example</strong></em>: You decide to pick a glass.</p>
<p>Your brain has to coordinate a series of actions:</p>
<ol>
<li><p>Decide to pick up glass</p>
</li>
<li><p>Move your hand</p>
</li>
<li><p>Make sure the hand has reached the glass correctly</p>
</li>
<li><p>Close your fingers</p>
</li>
<li><p>Lift the glass</p>
</li>
</ol>
</blockquote>
<p>Now connect this to programming:</p>
<blockquote>
<p><strong>A program is like a set of instructions for a computer</strong></p>
</blockquote>
<p>Just like your body needs instructions to perform an action, a computer needs instruction to perform a tasks.</p>
<blockquote>
<p>Lets take an example of simple calculator program, what will be the task that it will perform lets analyse:</p>
<ul>
<li><p>Take two numbers as input</p>
</li>
<li><p>Take the operation to be performed</p>
</li>
<li><p>Performs the calculation</p>
</li>
<li><p>Store the result</p>
</li>
<li><p>Display the result</p>
</li>
</ul>
</blockquote>
<p>Just like picking up a glass required multiple coordinated actions, even a simple computer requires multiple instructions.</p>
<blockquote>
<p>Now lets understand how program works within a computer in detail:</p>
</blockquote>
<p><strong>Program → Program starts running → Process → Thread → Instructions are executed</strong></p>
<blockquote>
<p><strong>Program = Instructions</strong></p>
</blockquote>
<p>Okay, but what happens when these instructions actually starts running.</p>
<p>This is where the concept of a <strong>Process</strong> comes in.</p>
<blockquote>
<p><strong>Process is also known as running of a program.</strong></p>
</blockquote>
<p>A process is a running instance of a program.</p>
<blockquote>
<p><strong>Think your body as a system that can perform different activities.</strong></p>
<p>When you are sitting still, nothing is necessarily "running" in the programming sense we're trying to illustrate.</p>
<p>Now imagine you decide to go for a run. Your brain gives instructions to your muscles, your muscles start working, and your body actively performs the activity.</p>
<p>In a similar way, a <strong>program is a set of instructions</strong>. When we start the program, the operating system creates a <strong>running instance of that program — a process</strong>.</p>
</blockquote>
<h3>Okay, so we understand that a process is a running instance of a program. But who actually executes the instructions?</h3>
<p>A process contains one or more threads, and threads provide the execution paths through which instructions are executed.</p>
<blockquote>
<p><strong>A thread is an active execution path within a process that executes instructions.</strong></p>
</blockquote>
<p>Process is just the container that hold the memory and resources of a program, the thread is the moving part that reads code line by line and get the work done.</p>
<blockquote>
<p><strong>Let's take an example from our daily life.</strong></p>
<p>Think of your body as a program.</p>
<p>You feel hungry and decide to eat something.</p>
<p>Your body now needs to perform a series of actions to accomplish this task. We can think of this as a process of eating.</p>
<p>Within this process, different parts of your body are involved in performing different tasks.</p>
<p>For the purpose of this analogy, let's think of different parts of the body as workers performing different pieces of work.</p>
<p>For example:</p>
<ul>
<li><p>👀 Eyes → locate the food</p>
</li>
<li><p>✋ Hands → reach for and pick up the food</p>
</li>
<li><p>👄 Mouth → eat the food</p>
</li>
</ul>
</blockquote>
<p>Each worker has a particular job to perform.</p>
<p>Similarly, in a computer program, a <strong>thread provides a path through which instructions are executed</strong>.</p>
<blockquote>
<p>A process can have <strong>one thread or multiple threads</strong>.</p>
</blockquote>
<p>With multiple threads, different work can potentially make progress concurrently.</p>
<p>Imagine a program that needs to download a file while also processing some data. With multiple threads, different threads can execute different work concurrently, depending on the program and runtime.</p>
<blockquote>
<p>Lets understand the program with an example:</p>
</blockquote>
<pre><code class="language-javascript">const http = require("http");

const server = http.createServer((req, res) =&gt; {
    if (req.url === "/login") {
        // Task 1: Check user credentials
        // Task 2: Get user data
        // Task 3: Send response

        res.end("Login successful");
    }
});

server.listen(3000);
</code></pre>
<p>This is a program named <code>login-server.js</code></p>
<blockquote>
<p>It is simply a collection of instructions:</p>
<ol>
<li><p>Create a server</p>
</li>
<li><p>Listen for requests</p>
</li>
<li><p>When <code>/login</code> arrives</p>
</li>
<li><p>Check the request</p>
</li>
<li><p>Process Login</p>
</li>
<li><p>Send response</p>
</li>
</ol>
</blockquote>
<h4>Now we run the program</h4>
<p>We execute:</p>
<pre><code class="language-shell">node login-server.js
</code></pre>
<p>The operating system now creates a running instance of your program.</p>
<p>That running instance is a:</p>
<blockquote>
<p><strong>Process</strong></p>
</blockquote>
<p>The process gets things such as:</p>
<ul>
<li><p>Memory</p>
</li>
<li><p>Resources</p>
</li>
<li><p>Execution context</p>
</li>
<li><p>One or more threads</p>
</li>
</ul>
<blockquote>
<p>The program is the instructions.<br />The process is the running instance of those instructions.</p>
</blockquote>
<p>Now our process needs to actually execute the instructions, that's where <strong>thread</strong> comes in.</p>
<p>A thread is essentially an <strong>execution path</strong> through the program.</p>
<p>So when the server starts:</p>
<blockquote>
<p>Program → Process created → Thread executes the program → Server starts listening</p>
</blockquote>
<p>Now user sends a request:</p>
<p>Suppose you open a browser:</p>
<blockquote>
<p><a href="http://localhost:3000/login">http://localhost:3000/login</a></p>
</blockquote>
<p>When a user sends a request, the process now has work to perform. We can think of this work as a task.</p>
<p>Task might involved:</p>
<ul>
<li><p>Read the request</p>
</li>
<li><p>Check credentials</p>
</li>
<li><p>Fetch user data</p>
</li>
<li><p>Send response</p>
</li>
</ul>
<p>So:</p>
<blockquote>
<p>Task = a piece of work that needs to be performed.</p>
</blockquote>
<h4>How Does a Thread Execute a Task?</h4>
<p>Okay, so now we know that a <strong>process contains one or more threads</strong> and that a thread is responsible for executing instructions.</p>
<p>But there is one thing I still want to understand:</p>
<h3><strong>How does a thread actually get the work done?</strong></h3>
<p>Let's go back to our simple calculator example.</p>
<p>Suppose our program has this code:</p>
<pre><code class="language-javascript">function add(a, b) {
    return a + b;
}

const result = add(10, 20);

console.log(result);
</code></pre>
<p>The task of this program is pretty simple:</p>
<blockquote>
<p><strong>Take two numbers, add them, and display the result.</strong></p>
</blockquote>
<p>But the computer cannot just look at the code and magically know that it needs to add <code>10 + 20</code>.</p>
<p>The code has to be <strong>executed</strong>.</p>
<p>We can roughly visualize what happens like this:</p>
<pre><code class="language-text">Program
   │
   ▼
Process
   │
   ▼
Thread
   │
   ▼
Execute instructions
   │
   ├── Call add()
   ├── Pass 10 and 20
   ├── Perform the addition
   ├── Store the result
   └── Display the result
</code></pre>
<p>The <strong>task</strong> is:</p>
<blockquote>
<p>Calculate <code>10 + 20</code></p>
</blockquote>
<p>The <strong>instructions</strong> are the individual steps required to complete that task.</p>
<p>The <strong>thread</strong> provides the execution path through which those instructions are executed.</p>
<hr />
<h3>Let's bring the human-body example back</h3>
<p>Imagine you decide to pick up a glass.</p>
<p>The task is:</p>
<blockquote>
<p><strong>Pick up the glass.</strong></p>
</blockquote>
<p>Your body has to perform several actions to accomplish it:</p>
<blockquote>
<ol>
<li><p>Locate the glass</p>
</li>
<li><p>Move your hand</p>
</li>
<li><p>Reach the glass</p>
</li>
<li><p>Close your fingers</p>
</li>
<li><p>Lift the glass</p>
</li>
</ol>
</blockquote>
<p>You don't think about every muscle movement individually. Your brain coordinates the actions, and your body carries them out.</p>
<p>We can use a similar idea to understand program execution.</p>
<p>A computer program contains instructions, and those instructions need to be executed to accomplish a task.</p>
<hr />
<h3>But who actually executes the instructions?</h3>
<p>This is where the <strong>CPU</strong> comes into the picture.</p>
<p>A thread provides the execution path for the work, and the CPU executes the actual machine instructions.</p>
<p>A very simplified view would be:</p>
<blockquote>
<p>PROCESS → THREAD → Instructions → CPU → Execution</p>
</blockquote>
<p>The CPU repeatedly performs operations on instructions. At a very high level, this is often described as:</p>
<blockquote>
<p>Fetch → Decode → Execute</p>
</blockquote>
<p>The details are much more complicated than this, but we don't need to go that deep yet.</p>
<p>For now, the important thing to remember is:</p>
<blockquote>
<p><strong>A thread provides an execution path, and the CPU executes the instructions associated with that thread.</strong></p>
</blockquote>
<p>And this also explains why I don't want to think of a <strong>task</strong> and a <strong>thread</strong> as the same thing.</p>
<p>A task is <strong>the work that needs to be done</strong>.</p>
<p>A thread is <strong>an execution path through which the instructions for that work can be executed</strong>.</p>
<p>The thread gets instructions, the CPU executes them, and eventually the task is completed.</p>
<p><strong>But what happens when a task takes some time to complete?</strong></p>
<p>What if our program needs to:</p>
<ul>
<li><p>read a file</p>
</li>
<li><p>query a database</p>
</li>
<li><p>make a network request</p>
</li>
<li><p>wait for some external operation</p>
</li>
</ul>
<p>Does the thread just sit there and wait until everything is finished?</p>
<p>I don't want to get into that part just yet. For now, I think I have a much clearer picture of what happens when a program runs.</p>
<p><strong>A program is a set of instructions. A process is a running instance of that program. A thread provides the execution path for those instructions, and the CPU executes them to get the work done.</strong></p>
<p>This gives me the foundation I was missing before. Now I can move on to the next concept with a much better understanding of what's actually happening behind the code.</p>
]]></content:encoded></item></channel></rss>