Definition
A process is a program in execution. It is an instance of a program that is running on the computer with its own memory space, resources, and execution context. A process is the fundamental unit of work in an operating system. One program can have multiple processes running simultaneously.
Process vs Program
Program:
- Static code stored on disk
- Collection of instructions
- Passive (just sitting there)
- Example: Microsoft Word application on disk
Process:
- Running instance of a program
- Includes: code, data, memory, resources
- Active (executing)
- Has unique identity and resources
- Example: Word document you’re editing right now
Analogy:
- Program = Recipe (just instructions)
- Process = Cooking dinner (actually doing the recipe)
Process Components (Process Image)
1. Code (Text Segment)
What:
- Actual program instructions
- Read-only in memory
- Shared between multiple instances of same program
Example:
- If 5 users run Word, code segment shared by all 5
2. Data (Data Segment)
What:
- Global and static variables
- Initialized at program start
- Separate copy for each process
- Readable and writable
Example:
int count = 0; // Global variable
char name[50]; // Static array
3. Heap
What:
- Dynamically allocated memory
- Grows as program allocates memory
- Separate for each process
- Must be freed by program
Example:
int *ptr = malloc(1000); // Allocate memory
ptr = NULL; // Memory freed
4. Stack
What:
- Local variables and function parameters
- Automatic allocation and deallocation
- Used for function calls
- Each thread has own stack
Example:
void function() {
int x = 5; // Local variable on stack
char name[20]; // Local array on stack
}
// x and name automatically freed when function returns
5. Process Control Block (PCB)
What:
- Data structure maintained by OS
- Contains process metadata
- Not part of process memory
Information:
- Process ID (unique identifier)
- Program counter (next instruction)
- CPU registers
- Memory allocated
- I/O devices used
- Process state
- Priority
- Parent process ID
- File descriptors (open files)
Process Creation
How a Process is Created
-
Program Execution Request
- User clicks program icon, or
- System call to execute program
-
OS Allocation
- OS allocates memory
- Creates PCB
- Initializes memory with program code
-
Loading
- OS loads program code into memory
- Sets up stack, heap
- Initializes data segment
-
Process Ready
- Process enters “ready” state
- Waits for CPU to execute
Parent and Child Processes
Parent Process: Process that creates another process
Child Process: Process created by parent
Process Family Tree:
init (Process 1)
|
bash (shell)
|
+-----+-------+
| | |
Word Firefox Chrome
Process Creation Call:
- In Unix/Linux: fork() and exec()
- In Windows: CreateProcess()
Process Types
1. System Processes
What: Created by operating system
- Handle system functions
- Background operation
- Not user-interactive
Examples:
- Init process (starts system)
- Kernel threads
- Device drivers
- System daemons
2. User Processes
What: Created by users
- Run user applications
- Can be interactive
- Use system resources
Examples:
- Word processor
- Web browser
- Calculator
- Games
Process Attributes/Properties
1. Process ID (PID)
Unique identifier assigned to each process
- Used by OS to track process
- Reused when process terminates
- Example: Process ID 1234
2. Process State
Current condition of process:
- New (just created)
- Ready (waiting for CPU)
- Running (executing)
- Waiting (waiting for I/O)
- Terminated (finished)
(Detailed in next topic “Process States”)
3. Process Priority
Determines scheduling priority:
- High priority = runs first
- Low priority = runs later
- Range: 0-31 or 1-40 (varies by OS)
- Real-time processes get higher priority
4. Working Directory
Current directory where process looks for files:
- Example: /home/user/documents
- Process can change with cd command
5. Environment Variables
Variables available to process:
- PATH: Directories to search for programs
- HOME: User’s home directory
- USER: Username
- Custom variables set by user
6. File Descriptors
Open files and devices:
- Standard input (stdin): Keyboard
- Standard output (stdout): Screen
- Standard error (stderr): Error output
- User-opened files
- Device connections
Process Resources
Memory
- Allocated memory for code, data, stack, heap
- Virtual memory allocated
- Managed by OS
CPU Time
- Allocated time slice in time-sharing systems
- Measured in milliseconds
- Tracked by OS
I/O Devices
- Devices needed for execution
- Printer, disk, network, etc.
- Allocated and released by OS
Other Resources
- File descriptors
- Locks
- Semaphores
- Sockets
Process Lifecycle
1. Creation Phase
- Program loaded
- Memory allocated
- PCB created
- Process enters “new” state
2. Scheduling Phase
- Process waits for CPU
- OS scheduler selects process
- Process enters “ready” state
3. Execution Phase
- CPU executes process
- Process in “running” state
- Time slice expires or waits for I/O
4. Waiting Phase
- Process waits for I/O
- Waiting state
- OS switches to another process
- When I/O completes, back to ready
5. Termination Phase
- Process finishes execution
- Cleanup occurs
- Deallocate memory and resources
- Process enters “terminated” state
Context of a Process
Context = Complete state of process including:
- All CPU registers
- Program counter (current instruction)
- Stack pointer
- Memory content
- I/O state
- Open files
Context Switching = Saving context of one process and loading context of another
- Allows OS to switch between processes
- Necessary for multitasking
Process Hierarchies
Unix/Linux Process Tree
init (PID 1) - First process created by kernel
|
+-- bash (shell)
| |
| +-- ls (list files)
| +-- vim (editor)
|
+-- sshd (ssh daemon)
|
+-- httpd (web server)
Parent-Child Relationship
- Parent: Process that creates another
- Child: Created process
- Child inherits some attributes from parent
- Child can create its own children
Process Group
- Group of processes
- Facilitates job control
- Example: Shell commands
Difference Between Process and Thread
| Aspect | Process | Thread |
|---|---|---|
| Memory | Separate | Shared within process |
| Resources | Own | Shared |
| Creation | Slower | Faster |
| Communication | Through IPC | Shared memory |
| Isolation | High | Low |
| Context Switch | High overhead | Lower overhead |
Important Process Concepts
Foreground Process
- Takes user input
- User waits for it
- Example: Running program interactively
Background Process
- Runs without user input
- User can do other things
- Example: Download in background
Daemon
- Special background process
- System-oriented
- Runs constantly
- Example: Web server
Zombie Process
- Process terminated
- Parent hasn’t collected exit status
- Shows in process list as "
" - Should be avoided
Orphan Process
- Parent process terminated
- Process becomes child of init
- Cleaned up by init
Exam Important Points
- Define process and difference from program
- Components of process (code, data, heap, stack, PCB)
- Process creation and process family
- Process types (system and user)
- Process attributes (PID, state, priority, etc.)
- Process lifecycle
- Context of process
- Context switching
- Foreground and background processes
- Zombie and orphan processes