Process Concept
An operating system manages and executes programs, each running as a separate process. Early systems only allowed one program to run at a time, while modern systems support concurrent execution of multiple programs in memory
What is a Process?
Process: A program in execution, managed by the OS; process execution must progress in sequential fashion. NO parallel execution of instructions of a single process
Each process consists of multiple parts:
-
Program code (text section)
-
Current activity (program counter, processor registers)
-
Stack (temporary data like function parameters, return addresses, and local variables)
-
Data section (global variables)
-
Heap (memory allocated dynamically during runtime).
-
Program is passive entity stored on a disk (executable file)
-
Process is active (Program becomes process when an executable file is loaded into memory)
-
One programme can be several processes (consider multiple users executing the same program)
Process in Memory
- Stack : Local variables and function parameters are stored in stack
- Heap : The Dynamic Memory Area, where memory is allocated on runtime
- Data : Static and global variables
- Text : The Executable Code of the program
Memory Layout of a C Porgram
Process VS Program
Program | Process |
---|---|
A program is a set of instructions | A Process is a Program in Execution |
A program is passive/static entity | Process is Active/dynamic Entity |
Longer life span, stored in disk forever | Process has a limited life span. It is created when execution starts and terminated when execution finished |
Require space on disk to store all instructions | A process contains various resources like : memory address ,disk ,printer as per requirement |
Process State
- New: The process is being created.
- Ready: The process is ready and waiting to be assigned to the CPU.
- Running: Instructions are being executed.
- Waiting (Blocked): Process is waiting for some event to occur (e.g., I/O completion).
- Terminated: The process has completed execution.
Process Control Block (PCB) / Task Control Block
- Each process in an operating system is represented by a PCB
- The PCB is a data structure that stores all necessary information about a process. It includes:
- Process Identifier : (PID)
- Process State: Current status of the process.
- Program Counter: Next instruction address.
- CPU Registers: CPU-specific information.
- Memory Limits: Memory allocated to the process.
- Accounting Information: CPU usage, time elapsed, etc.
- I/O Status: Resources like files, I/O devices.
Process Scheduling
The OS uses a scheduler to determine which process to run next based on scheduling policies (e.g., First-Come-First-Served, Round Robin).
Scheduler Functions
- Context Switching: Saves the current process state and restores the next process’s state.
- Process Selection: Chooses the next process based on a scheduling policy.
- Execution Continuation: Resumes the selected process’s execution.
Types of Processes
Processes can be categorized as either independent or cooperating:
- Independent processes do not interact with other processes.
- Cooperating processes can work together, affecting or being affected by other processes.
The operating system allows cooperating processes to:
- Enable information sharing
- Allow access to resources
- Increase computation speed
What are threads
- Threads are smaller units of a process that can run independently.
- Think of process as a big task, and a thread is smaller tasks within that big task
Why use Threads?
- Efficient Resource Sharing: Threads within the same process share memory and resources.
- Parallel Execution: Threads can run on different CPU cores.
- Responsive Programs: If one thread is blocked, others can still execute.
Example: In a word processor:
- Thread 1 handles user input.
- Thread 2 checks spelling.
- Thread 3 saves work periodically.
Why not Multiple Processes?
- Memory Consumption
- Running multiple processes (copying of the same program) uses a lot of memory because each process has its own memory space.
- Swapping overhead
- Process often swap between main memory and secondary storage, which can slow down the execution
- the swapping is called “overhead” and its inefficient for heavy processes.
Threads vs processes?
Aspect | Threads | Processes |
---|---|---|
Weight | Lightweight | Heavyweight |
Memory Usage | Share the same memory space of a process | Have separate memory space |
Context Switching | More efficient and faster context switching / Avoid Overhead | Swapping makes a process slow and inefficient |
Summary about Threading
- Threads are like smaller tasks within a big task (process).
- They help make programs more efficient and responsive.
- Threads use less memory and are faster than running multiple processes.
- Swapping threads is quicker than swapping processes.
- Multithreading allows multiple threads to share resources and run in parallel.