Operating Systems · Process Concept and States
Program vs Process
Overview
A program and a process are two distinct terms in process management. A program is a file stored on disk, while a process is a data structure the operating system creates in main memory so that the program can actually run.
Key concepts
- Program — the file you write and/or the executable file produced from it; it resides in secondary memory (hard disk). Both the high-level source file (for example
example.c) and the compiled machine-code file (a.out) are called programs. - Process — a data structure created by the operating system in main memory in order to execute a program. Before a program can be executed, the OS must create a process for it.
- Primary memory = main memory = RAM; secondary memory = hard disk (disk) — the two levels of storage.
- Executable code — machine code contained in the executable file such as
a.out. - Analogy — the process is the body and the program is the soul: the process is real, the program is virtual.
How it works
- Write the source program in a high-level language, for example
example.cin C. - Give the program to the compiler, which converts the high-level language (C) into a low-level language — machine-level language — that can be executed on the machine.
- The operating system's task is not to take a high-level language program and execute it on the machine.
The program must first be converted to machine-level language, which the OS understands; then it can run directly on the hardware. - The compiler generates something like
a.out, a file that contains executable code, i.e. machine code. - Both the source file (
example.c) and the executable file (a.out) are called programs only. - The program resides in the hard disk of the computer, i.e. in secondary storage.
- To execute the program, the operating system takes that program and puts it into the main memory, then starts executing it.
- When the OS puts the program into main memory, it creates some kind of structure — a data structure — and that data structure is what is called a process. The process is something created by the operating system in order to execute a program; before you execute a program, you are supposed to create the process.
- Relationship between the two: a process is something which is real, and a program is something which is virtual. The soul is a virtual concept that cannot actually be seen or handled; the body exists physically.
Whenever you want to execute a program, the operating system creates such a structure in the main memory.
Comparison
| Aspect | Program | Process |
|---|---|---|
| What it is | The source file (example.c) or the compiled machine-code file (a.out) | A data structure created by the OS to execute a program |
| Where it resides | Secondary memory (hard disk) | Main memory (RAM) |
| Who creates it | Written by the programmer; executable produced by the compiler | Created by the operating system |
| Nature | Virtual (the "soul") | Real (the "body") |
| Role | Passive entity that has to be executed | The entity that is actually executed |
| Timing | Exists before execution | Must be created before the program is executed |
Revision summary
- Program and process are not the same; the difference is the starting point of process management.
- The compiler converts high-level code into machine-level code; the OS cannot execute high-level code by itself.
a.out(the executable file) contains executable code = machine code.- Both
example.canda.outare called programs. - Primary memory = RAM = main memory; secondary memory = hard disk = disk.
- A program is stored on the hard disk (secondary storage) when it is not running.
- To execute a program, the OS moves it into main memory and builds a data structure — that structure is the process.
- Memorise the analogy: process = body (real), program = soul (virtual);
a process must be created before the program is executed.
Previous-year exam insights
- Standard definition to memorise: a process is a program in execution — an active entity, whereas a program is a passive entity (just a file on disk).
Asked inISRO 2009 - A process in memory consists of four sections: text (code), data (global/static variables), heap (dynamic allocation, grows upward) and stack (function calls, local variables, grows downward).
- A process in execution passes through states: new, ready, running, waiting (blocked), terminated; only one process per CPU core can be in the running state at a time.
- The OS represents each process by a PCB (Process Control Block) holding the process state, program counter, CPU registers, scheduling and memory-management information, and I/O status.
- A program can have several processes running the same code (e.g. multiple shells); each process has its own PCB, address space and program counter.
- Context switch = saving the state of the running process in its PCB and loading the saved state of the next process;
it is pure overhead (no useful work done).