A process is an instance of a program in execution.
It's a dynamic entity that requires system resources like CPU time, memory, and I/O devices to perform its tasks. Key characteristics of a process include:
Program Counter: Points to the next instruction to be executed.
Registers: Stores temporary data used in calculations.
Stack: Manages function calls and local variables.
Heap: Dynamically allocates memory during program execution.
Process Creation and Termination
Process Creation:
System call: A program requests the OS to create a new process.
OS allocates resources: Memory, CPU time, and other resources are assigned.
New process is created: A new process control block (PCB) is created to manage the process's state.
Process Termination:
Normal termination: The process completes its execution.
Abnormal termination: The process crashes or is killed by the OS.
OS reclaims resources: The OS deallocates the resources used by the terminated process.
Process Control Block (PCB)
The PCB is a data structure that contains information about a process, including:
Process ID (PID): A unique identifier.
Process State: Current state (running, ready, waiting, etc.).
Program Counter: Points to the next instruction.
Registers: Stores the process's registers.
Memory Limits: Defines the memory allocated to the process.
Open Files: List of open files.
Parent Process: The process that created the current process.
Child Processes: List of child processes.
Priority: Determines the process's importance.
Process Life Cycle
A process goes through different states during its lifetime:
New: The process is being created.
Ready: The process is waiting to be assigned to a CPU.
Running: The process is currently executing on a CPU.
Waiting: The process is waiting for an event (e.g., I/O operation).
Terminated: The process has finished execution.
Process Management in Windows and Linux
Windows:
Task Manager: Visualizes running processes, CPU usage, memory usage, and network activity.
Command Prompt: Uses commands like tasklist, taskkill, and start to manage processes.
PowerShell: A more powerful command-line interface for advanced process management tasks.
Linux:
Terminal: Uses commands like ps, top, kill, and nice to manage processes.
Graphical User Interface (GUI): Provides a visual interface for process management (e.g., GNOME, KDE).
Commonly Used Commands and Tools
ps: Lists running processes.
top: Displays real-time system information, including process usage.
kill: Terminates a process.
nice: Changes a process's priority.
renice: Changes the priority of running processes.
tasklist: Lists running processes in Windows.
taskkill: Terminates a process in Windows.
Task Manager: Visualizes and manages processes in Windows.
Activity Monitor: Visualizes and manages processes in macOS.
CPU Scheduling
CPU scheduling is the process of determining which process should be executed next on a CPU. The goal is to optimize resource utilization and system performance.
Types of Scheduling
Long-term scheduling: Determines which processes are admitted into the system.
Short-term scheduling: Selects the next process to be executed by the CPU.
Medium-term scheduling: Swaps out processes to secondary storage to free up memory.
Process Scheduling Algorithms
First-Come, First-Served (FCFS): Processes are executed in the order they arrive in the ready queue.
Shortest Job First (SJF): The process with the shortest estimated burst time is executed first.
Priority Scheduling: Processes are assigned priorities, and the highest-priority process is executed first.
Round Robin: Each process is allocated a fixed time slice.
Shortest Remaining Time First (SRTF): Similar to SJF but considers the remaining burst time of processes.
Inter-Process Communication (IPC)
Inter-process communication (IPC) is a mechanism that allows different processes to communicate and synchronize their activities.
Common IPC Mechanisms:
Pipes: A unidirectional communication channel that allows data to flow from one process to another.
Message Queues: A queue of messages that can be accessed by multiple processes.
Shared Memory: A block of memory that can be accessed by multiple processes.
Semaphores: A synchronization mechanism that controls access to shared resources.
Sockets: A network programming interface that allows processes to communicate over a network.
Process Synchronization
Process synchronization is the coordination of multiple processes or threads that access shared resources to ensure data consistency and prevent race conditions.
Types of Process Synchronization
Mutual Exclusion: Ensures that only one process can access a shared resource at a time.
Cooperation: Involves coordination among processes to achieve a common goal.
Critical Section
A critical section is a code segment that accesses shared resources. It's crucial to ensure that only one process enters the critical section at a time to avoid race conditions.
Synchronization Mechanisms
Semaphores: A semaphore is an integer variable that can be accessed only through two atomic operations: wait and signal.
Mutexes: A mutex (mutual exclusion) is a synchronization primitive that allows only one thread to access a shared resource at a time.
Monitors: A high-level synchronization construct that encapsulates shared data and the procedures that operate on it.
Spinlocks: A low-level synchronization primitive that continuously checks a lock variable until it becomes available.
Deadlock in Operating Systems
Deadlock is a situation in which two or more processes are blocked indefinitely, waiting for each other to release resources. This can occur when processes compete for shared resources and the system fails to allocate resources in a way that prevents circular waiting.
Conditions for Deadlock
Four necessary conditions must hold for a deadlock to occur:
Mutual Exclusion: At least one resource must be held in a non-sharable mode, meaning only one process can use it at a time.
Hold and Wait: A process must hold at least one resource and be waiting to acquire additional resources held by other processes.
No Preemption: Resources cannot be forcibly taken away from a process holding them.
Circular Wait: A circular chain of two or more processes exists, where each process is waiting for a resource held by the next process in the chain.
Strategies for Handling Deadlock
Deadlock Prevention:
Resource Ordering: Impose an ordering on resources and require processes to request resources in increasing order.
Resource Allocation: Implement a resource allocation policy that prevents circular wait conditions.
Deadlock Avoidance:
Banker's Algorithm: A complex algorithm that analyzes resource allocation requests to ensure that a safe state can always be reached.
Deadlock Detection:
Resource Allocation Graph: A graphical representation of processes and resources to detect cycles that indicate potential deadlock.
Deadlock Recovery:
Process Termination: Terminate one or more processes to break the deadlock cycle.
Resource Preemption: Forcibly take away resources from processes to break the deadlock cycle.