Program #
A program is a collection of instructions stored in a file (.exe, .py, .sh etc..). They reside on a disk (secondary storage), until picked up by the CPU for execution.
Components of a Program #
Along with the instructions, a program consists of different sections that organize and store data during its execution. These sections remain available throughout the entire program lifecycle. The main components of a program include:
- Code Segment
- Data Segment
- Heap
- Stack
- System Table
- Metadata
Converting source code to an executable file. #
The beauty of abstraction is that it allows end users to run applications without needing to understand the underlying details of how the application is made available during its execution. In this section, let’s explore what really happens behind the scenes when a user executes a program.
Programs are mainly classified into two types based on their languages:
- Compiled Languages
- Interpreted Languages
Complied Language. #
In compiled languages, the source code is transformed into machine code (binary instructions specific to the target system) by a program called a compiler. This conversion typically happens before the program is run. Once compiled, the resulting executable can be directly run by the operating system, and it usually offers faster execution since all translation work is done ahead of time.
Examples: C, C++, Rust, Go.
Characteristics:
- Compilation is a one-time step, required only when code changes.
- Errors are detected at compile time, before execution begins.
- The resulting executable can be distributed and run independently of the source code.
Below are the sample files that gets generated during each stage of compilation.
- Assembly code
gcc -S prog.c -o prog.s
Options:
- -S : Stop the compilation process after generating assembly code
- -o : output file name
- Machine code
gcc -c prog.s -o prog.o
Options:
- -c : Compile or assemble the input, but do not link
- -o : output file name
The below command produces an executable file by performing linking.
gcc prog.o -o prog
Options:
- prog.o: The compiled object file (machine code).
- -o : output file name
Running the executable file
./prog
Output:
Program under exectuion!
Interpreted Language. #
In interpreted languages, the source code is executed line-by-line or statement-by-statement by an interpreter program at runtime.There is no separate compilation step that creates a standalone binary; instead, the interpreter reads and performs instructions directly each time the program runs.This can make interpreted programs slower than compiled ones, since translation occurs dynamically.
Examples: Python, JavaScript, Ruby, PHP.
Characteristics:
- Programs are typically easier to modify and test due to dynamic execution.
- Errors are detected at runtime as code is executed.
- The source code must be present, and the interpreter program is needed every time the program runs.
The below command produces bytecode
python3 -m dis prog.py
Lifecycle of a program (from the operating system perspective) #
Now that we understand what happens behind the scenes when a program is executed and the difference between compiled and interpreted languages, let’s explore the end-to-end lifecycle of a program—from source code to execution and eventual termination—from an operating system perspective.