Fundamentals of technology: programs, operating systems, memory and storage
Advertisement
Before you write your first line of code, it helps to know what you are actually talking to. When a beginner asks "why does my program lose its data when it closes?" or "what is the difference between a compiler and an editor?", the answers are not in any programming language; they are in how a computer works. This post covers the basics in plain language: what a language and a program are, the tools you use to write one, what the operating system does, the difference between memory and storage, how all information becomes numbers, and what happens when you press "run". The small code examples were run to confirm the numbers.
What is a language?
A language is a way for two parties to exchange information. Humans use Hindi, English and Tamil; both sides must share the same rules (vocabulary and grammar) for the message to work. A computer, too, only understands one thing natively: patterns of electrical on and off signals, written as 0 and 1, called machine code.
What is a programming language?
A programming language is a language humans can read and write that can be translated into something the machine runs. Writing machine code by hand is slow and error-prone, so we write in languages such as C, Python, Java or PHP, with words like print, if and while, and a translator converts them.
Programming languages are often grouped by how close they are to the machine:
| Level | Examples | Idea |
|---|---|---|
| Machine code | binary instructions | what the CPU actually runs |
| Assembly | mov, add |
a readable name for each machine instruction |
| High level | C, Java, Python, PHP, JavaScript | readable, portable, handles many details for you |
What is a program?
A program is a set of instructions that tells a computer how to perform a task, written in a programming language. The instructions are followed in order, and they can take input, make decisions, repeat steps and produce output. Everything you use is a program: a calculator, a browser, a game, the code that runs a bank's transfers.
Programs usually do three things: take input (keyboard, file, network), process it (calculate, compare, search), and produce output (screen, file, sound). A tiny example in Python:
name = input("Your name: ") # input
greeting = "Hello, " + name + "!" # process
print(greeting) # output
Running it and typing Asha printed Your name: Hello, Asha! when the answer was piped in; on a terminal you would see the greeting on the next line.
Source code, compiler and interpreter
The text you write is source code. Machines cannot run it directly, so it must be translated. There are two common approaches:
- A compiler translates the whole program into machine code once, producing an executable file. C and C++ work this way:
cc hello.c -o hello, then./hello. The result runs fast and without the compiler, but you must recompile after changes. See the C language guide. - An interpreter reads and runs the source code line by line, every time. Python and PHP work this way. It is convenient (you can run code immediately), but typically slower. See the Python introduction.
- Some languages mix both. Java compiles to bytecode, which a virtual machine (the JVM) then runs on any operating system. Python also compiles to bytecode internally.
Errors fall into two kinds that you will meet constantly: a syntax error (the code breaks the language's grammar, so it cannot be translated) and a runtime error or logic error (it runs, but crashes or gives the wrong answer).
What is a text editor?
A text editor is a program for writing plain text, with no formatting such as fonts or colours: Notepad, TextEdit (in plain mode), Vim, Nano. Source code must be saved as plain text, which is why a word processor like Microsoft Word is the wrong tool: it adds hidden formatting that compilers cannot read.
A code editor (Visual Studio Code, Sublime Text) is a text editor with extras for programmers: colouring of keywords (syntax highlighting), automatic indentation, suggestions, and search across files. An IDE (integrated development environment) such as IntelliJ IDEA, Eclipse or Visual Studio goes further and bundles a compiler or runner, a debugger, a project manager and testing tools. For a beginner, VS Code plus a terminal is a good, free combination.
What is an operating system?
An operating system (OS) is the main program of the computer. It starts when the computer boots, manages the hardware, and lets all other programs share it. Windows, macOS, Linux, Android and iOS are operating systems. It does five jobs:
- Process management: it decides which program runs on the processor and when, so many programs seem to run at once.
- Memory management: it gives each program its own share of RAM and stops one program from damaging another's.
- File management: it organises data on storage into files and folders, and controls who may read or change them.
- Device management: it talks to the keyboard, screen, printer and network through drivers.
- User interface and security: windows and icons (a GUI) or a command line, plus logins and permissions.
Programs do not control the hardware directly. When a program wants to read a file, it asks the OS through a system call, and the OS does it. This is why a program compiled for Windows does not run on Linux: the system calls and file formats differ, which is the problem Java's virtual machine solves.
You can ask the OS about itself. In Python:
import os, platform
print(platform.system(), os.cpu_count() > 0, os.getcwd().startswith("/"))
On the machine used for this article it printed Darwin True True: the OS name (macOS reports itself as Darwin), a check that there is at least one CPU core, and that the working folder path starts with / (on Windows it would start with a drive letter).
The hardware that matters
| Part | What it does |
|---|---|
| CPU (processor) | executes instructions: arithmetic, comparisons, jumps. Speed is measured in GHz and number of cores. |
| RAM (memory) | fast, temporary workspace for running programs and their data |
| Storage (SSD, hard disk) | permanent home for files; slower but large and keeps data without power |
| Input/output devices | keyboard, mouse, screen, network card |
| GPU | processor specialised for graphics and parallel maths |
RAM versus storage
This is the question beginners most often get wrong. RAM (random access memory) is where the CPU keeps what it is working on right now: your running programs and their variables. It is very fast, but it is volatile: when the power goes off, or the program closes, its contents vanish. That is why a program's variables disappear when it ends. Storage (an SSD or hard disk, a pen drive) is non-volatile: files stay when the power is off, but access is much slower than RAM.
So the flow is: a program lives on storage; when you start it, the OS loads it into RAM; the CPU runs it from RAM; and if you want data to survive, the program must save it from RAM to storage (a file or a database). If you do not save your document and the computer crashes, the unsaved work is lost, because it lived only in RAM. This is exactly why programs write to files and databases (see file handling in Python or transactions in a database).
A rough comparison of speed, to show why the layers exist: the CPU's own tiny caches are fastest, then RAM (tens of nanoseconds), then an SSD (tens of microseconds), then a hard disk (milliseconds), and then a network. Each step down is much bigger and much slower.
How information is stored: bits and bytes
Digital hardware has only two states, so all information is stored as bits (binary digits), each 0 or 1. Eight bits make a byte. Counting in binary works like decimal, but with powers of 2:
for n in [0, 1, 2, 5, 10, 255]:
print(n, format(n, "b").zfill(8), hex(n))
print(int("1010", 2), 2 ** 10, 2 ** 8 - 1)
0 00000000 0x0
1 00000001 0x1
2 00000010 0x2
5 00000101 0x5
10 00001010 0xa
255 11111111 0xff
10 1024 255
One byte can hold 256 different values (0 to 255). Larger units are multiples: a kilobyte is about a thousand bytes (1 KiB = 1,024), a megabyte about a million, a gigabyte about a billion, a terabyte about a trillion.
Text is stored by giving each character a number (a character encoding): A is 65, a is 97, and the digit 5 is 53. Unicode with the UTF-8 encoding covers every language and emoji, using one to four bytes per character.
print(ord("A"), ord("a"), ord("5"), chr(66))
print(len("hello".encode()), len("नमस्ते".encode("utf-8")), len("नमस्ते"))
print("é".encode("utf-8"), "€".encode("utf-8"))
65 97 53 B
5 18 6
b'\xc3\xa9' b'\xe2\x82\xac'
The English word "hello" takes 5 bytes, but the six Hindi characters (code points) of "नमस्ते" take 18 bytes, three each. That is why a character count and a byte count are different things. Images are grids of pixels with a colour number for each, sound is a list of measurements taken thousands of times per second, and programs themselves are just numbers (machine instructions). To the computer, everything is bits.
How a program runs, step by step
- You write source code in an editor and save it to storage.
- A compiler or interpreter translates it.
- You start the program; the OS loads the executable (or the interpreter) from storage into RAM and creates a process for it.
- The CPU fetches, decodes and executes instructions from RAM, one after another, billions of times per second: this is the fetch-decode-execute cycle.
- The program asks the OS for input and output, such as reading the keyboard, drawing on the screen or writing a file.
- When it ends, the OS reclaims its memory. Anything not saved to storage is gone.
Algorithms and data
Two ideas sit above all this. An algorithm is a step-by-step method for solving a problem: a recipe. Good programs are good algorithms written down precisely; you saw how a better algorithm beats a faster computer in the prime number program. Data structures (lists, dictionaries, trees) are ways to organise data so that algorithms can use it efficiently, as in lists and dictionaries. Together they are the core of computer science.
Common beginner confusions
- "Is a file in memory?" A file is on storage; the copy you are editing is in RAM until you save.
- "Why can't I use Word to write code?" Word saves formatting; compilers need plain text.
- "Is the internet the same as the web?" The internet is the global network; the web (pages and sites) is one service on it, alongside email and others.
- "Is a 'bug' a virus?" A bug is a mistake in a program; a virus is malicious software.
- "Does more RAM make everything faster?" It helps when you run out; beyond that, the CPU and storage speed matter.
- "Why is my program's output different on another computer?" Different OS, language version or character encoding: always note what you ran it on.
Where to go next
Choose a first language and write something small every day. Python is the easiest start; C shows you what is underneath, and C++ builds on it; Java introduces virtual machines and strict typing. The free Python course takes you through installing and running your first programs, and how to prepare for a programming exam helps you study effectively.