Zamil's CSE Directory

programming

What Happens When a Program Runs

Understand the lifecycle of execution, from writing code to CPU instruction processing.

#programming#execution#cpu#memory#operating-systems
programming, execution, cpu, memory, operating-systems guides

In the previous chapter we built a mental model for programming.

We said that programs are instructions that automate tasks.

Now we need to connect that idea to the machine itself:

How do written instructions become actual computation?


The Program Lifecycle

At a high level, most programs go through this flow:

graph LR
  A[Write Source Code] --> B[Compile or Interpret]
  B --> C[Load Into Memory]
  C --> D[CPU Executes Instructions]
  D --> E[Program Produces Output]

Each stage has a specific responsibility.


1. Writing Source Code

Humans write code in a programming language such as Python, C, JavaScript, or Go.

This code is called source code.

Source code is readable by humans but not directly executable by the CPU in most cases.


2. Compiling or Interpreting

Computers ultimately execute low-level machine instructions.

So source code must be translated.

Two common approaches:

  • Compiled languages translate code ahead of time into machine code (or an intermediate binary).
  • Interpreted languages execute code through an interpreter at runtime.

Different languages and runtimes use different combinations of these ideas.


3. Loading Into Memory

Before execution starts, the operating system loads the program into RAM.

This includes:

  • the executable instructions
  • program data
  • stack and heap regions

The OS also creates a process and allocates resources such as file handles and memory space.


4. CPU Execution

The CPU runs a repeated cycle:

  1. Fetch instruction from memory.
  2. Decode instruction.
  3. Execute instruction.
  4. Move to the next instruction.

This happens extremely quickly, millions or billions of times per second.

A single line of high-level code can expand into many low-level CPU instructions.


5. Producing Results

As instructions execute, the program may:

  • compute values
  • read or write files
  • send network requests
  • render UI
  • store data

Eventually the process exits, or it keeps running if it is a long-lived service.


The Role of the Operating System

The operating system is the coordinator between software and hardware.

It is responsible for:

  • process scheduling
  • memory protection
  • device access
  • filesystem operations
  • networking interfaces

So when we say “a program runs,” it is actually running inside an OS-managed environment.


Why This Mental Model Helps

Understanding the execution pipeline makes many programming topics easier:

  • performance issues relate to CPU, memory, and I/O
  • runtime errors relate to how execution actually proceeds
  • language differences often come from runtime and compilation models

This is where programming connects directly to computer architecture and operating systems.


Key Ideas to Remember

  • Programs start as source code.
  • Source code is translated through compilation/interpreting.
  • The OS loads programs into memory as processes.
  • CPUs execute instructions through a fetch-decode-execute cycle.
  • Real-world behavior depends on both language runtime and OS support.

What Comes Next

Now that we understand program execution, we can discuss the tools we use to express instructions:

programming languages, why they exist, and how they differ.