Process & Memory
Definition
In computer science, a Process is an instance of a computer program that is being executed by one or many threads. It includes the program code, its current activity, the stack, the heap, and a set of system resources. Memory refers to the physical or virtual storage area (RAM) where the operating system allocates space for these processes to store data and instructions while they are running.
Main Content
1. Process Control Block (PCB)
- A PCB is a data structure used by the operating system to store all the information about a specific process.
- It contains the Process ID (PID), program counter, CPU registers, scheduling information, and memory management data.
2. Address Space
- Every process is provided with its own virtual address space, which is a set of memory addresses it can use.
- The address space typically consists of the Text segment (code), Data segment (variables), Heap (dynamic memory), and Stack (function calls).
3. Memory Hierarchy
- Processes utilize a hierarchy of memory ranging from fast CPU registers and Cache to slower Main Memory (RAM).
- The Operating System manages this through "Memory Management Units" (MMU) which translate virtual addresses to physical addresses.
[ Process Layout in Memory ]
+---------------------+
| Stack | <- Local variables, function calls
|---------------------|
| Heap | <- Dynamic memory allocation
|---------------------|
| Data Segment | <- Global and static variables
|---------------------|
| Text (Code) | <- Instructions to be executed
+---------------------+
Working / Process
1. Process Creation
- The Operating System creates a new process by allocating a new PCB and assigning a unique PID.
- It loads the program code from the storage (disk) into the allocated memory space of the process.
2. Memory Allocation
- The system allocates a block of physical RAM to the process or uses Virtual Memory.
- If RAM is insufficient, the system uses "Swapping" or "Paging" to move inactive process parts to the hard drive.
3. Execution and Context Switching
- The CPU executes instructions from the Process's Text segment while using the Stack and Heap for data manipulation.
- When the OS switches from one process to another, it saves the current process's state into its PCB and loads the next one.
Advantages / Applications
- Multitasking: Allows multiple applications (like a browser and a music player) to run simultaneously without interfering with each other.
- Protection: Through memory isolation, one process cannot access or corrupt the memory space of another process.
- Efficiency: Demand paging allows processes to run even if their entire code does not fit into physical memory at once.
Summary
A process is an active program execution unit, while memory acts as the workspace where the process stores its necessary data. The operating system coordinates these two to ensure secure, efficient, and simultaneous execution of multiple tasks.
Important terms to remember: - PID (Process ID): A unique number assigned to every process. - Context Switch: The mechanism of storing a process state to pause and resume later. - Virtual Memory: A technique that provides the illusion of larger memory than physical RAM. - Stack & Heap: Essential memory segments for dynamic and static data management.