What is Process ?
A process in computing is a program in execution. It represents the fundamental unit of work that a computer’s operating system manages. A process includes the program code and its current activity, such as the program counter, registers, and variables.
Simpler breakdown:
- Program: A set of instructions stored on disk (passive).
- Process: An instance of a program in execution (active).
A process typically consists of:
- Executable code: The instructions that make up the program.
- Process state: Includes the current activity represented by the value of the program counter and the contents of the processor’s registers.
- Memory: The address space that includes the executable code, process stack (for function calls and local variables), and data section (global variables).
- System resources: Such as open files and network connections.
Process Scheduling
Process scheduling is the activity of the operating system that handles the execution of processes. The OS schedules processes by deciding which process runs at any given time to optimize CPU usage, system responsiveness, and process throughput.
Types of Scheduling:
- Long-term scheduling: Decides which processes are admitted to the system for processing. Controls the degree of multiprogramming.
- Medium-term scheduling: Temporarily removes processes from main memory and places them in secondary storage (swapping).
- Short-term scheduling (CPU scheduling): Decides which process will execute next and allocates CPU.
Scheduling Algorithms:
- First-Come, First-Served (FCFS): Processes are scheduled in the order they arrive in the ready queue.
- Shortest Job Next (SJN): Processes with the shortest execution time are scheduled next.
- Priority Scheduling: Processes with the highest priority are scheduled first.
- Round Robin (RR): Each process gets a small unit of CPU time (time quantum), and after this time has elapsed, the process is preempted and added to the end of the ready queue.
- Multilevel Queue Scheduling: Multiple queues are used, each with its own scheduling algorithm.
- Multilevel Feedback Queue Scheduling: Processes can move between queues based on their behavior and requirements.
Operations on Processes
Processes are fundamental to the execution of programs. The OS provides mechanisms to create, manage, and terminate processes.
Operations:
- Process Creation: Typically, processes are created through system calls such as fork in UNIX-based systems, which creates a new process (child) that runs concurrently with the parent process.
- Process Termination: A process ends its execution via system calls like exit. The OS then deallocates resources used by the process.
- Process Control: Processes can be suspended, resumed, or aborted via signals and system calls.
Inter-process Communication (IPC)
Inter-process Communication (IPC) is a set of mechanisms that allow processes to communicate and synchronize their actions when executing concurrently. IPC is essential for multitasking operating systems, enabling processes to exchange data and coordinate their activities.
IPC Mechanisms
Shared Memory
- Description: Multiple processes can access the same memory space.
- Advantages: Fast data exchange since processes communicate by reading and writing to a shared memory area.
- Challenges: Requires synchronization mechanisms (like semaphores or mutexes) to prevent data corruption.
Message Passing
- Description: Processes communicate by sending messages to each other.
- Mechanisms:
- Pipes: Allow unidirectional data flow between two processes.
- Unnamed Pipes: Typically used for communication between related processes (parent-child).
- Named Pipes (FIFOs): Can be used by unrelated processes and allow bidirectional communication.
- Message Queues: Allow processes to send and receive messages in an organized queue.
- Sockets: Enable communication between processes over a network.
- TCP Sockets: Provide reliable, connection-oriented communication.
- UDP Sockets: Provide connectionless communication with lower overhead.
- Pipes: Allow unidirectional data flow between two processes.
Semaphores
- Description: Used to control access to a common resource by multiple processes to avoid critical section problems.
- Types:
- Binary Semaphores (Mutexes): Only take values 0 and 1, ensuring mutual exclusion.
- Counting Semaphores: Can take non-negative integer values, useful for managing a resource with a limited number of instances.
IPC in Client-Server Systems
In client-server architectures, IPC is crucial for the interaction between clients (requesters of services) and servers (providers of services).
- Remote Procedure Calls (RPCs): Allow a process to cause a procedure to execute in another address space (often on another machine). This abstracts the communication process into simple procedure calls.
- RESTful APIs: Use HTTP methods for communication, are stateless, and are often used in web services.
- WebSockets: Provide full-duplex communication channels over a single TCP connection, allowing real-time data exchange.
Synchronization in IPC
To ensure that processes do not interfere with each other while sharing data, synchronization mechanisms are crucial:
- Locks: Used to enforce mutual exclusion.
- Condition Variables: Allow processes to wait for certain conditions to be true.
- Barriers: Ensure that multiple processes or threads reach a certain point in their execution before any of them proceed.