Process Management

Reading Time: 5 minutes

Process management

Process

A process is basically a program that is currently running. Think of it like a running app on your phone or computer — it’s more than just the code; it includes its current state, memory, and resources.

Process states

A process moves through different states during its life.

State     :    Description

  • New : process is being created.
  • Ready : process is loaded into memory and waiting for cpu.
  • Running : process is currently being executed by cpu.
  • Waiting : process is waiting for some event (like i/o completion).
  • Terminated : process has finished execution.

Process scheduling

Process scheduling is how the operating system decides which process gets the cpu and when. Since cpu’s have limited resources, scheduling ensures efficient and fair execution. There are many scheduling algorithms.

Context switching

When the CPU switches from one process to another, it saves the current process’s state and loads the new process’s state. It allows multitasking—multiple processes share the CPU by taking turns, even though only one process runs at a time.

Interprocess communication

Interprocess communication (ipc) is a mechanism that allows two or more processes to exchange data, signals, or messages. It deals with how processes communicate and coordinate with each other.

Communication in client–server systems

Communication in client–server systems refers to the way two types of processes— clients and servers—exchange information over a network.

Client–server model

Client : initiates requests and waits for responses. Example : a web browser requesting a webpage.

Server : waits for requests, processes them, and sends back responses. Example : a web server sending html pages to a browser.

Communication mechanisms

Socket-based communication

Sockets are endpoints for sending and receiving data over a network.

How it works:

  • Server creates a socket, binds it to a port, and listens for connections.
  • Client creates a socket and connects to the server’s address and port.
  • They exchange data over this connection (TCP for reliable streams, UDP for messages).

Example: a web browser (client) connects to a web server (server) via TCP/IP.

Remote procedure calls

It allows a client to call a function on a remote server as if it were local.

How it works:

  • Client calls a local stub function.
  • A stub is a small piece of code that simulates a real function and returns predefined responses.
  • Stub sends the request over the network to the server.
  • Server executes the function and sends the result back.
  • Example: a client calls a remote database query function on a server.

Process synchronization

It is a mechanism to coordinate multiple processes that share resources so that they operate correctly and predictably without any conflict.

Why it’s needed ?

Race condition : It happens when two or more processes access and modify shared data concurrently, and the final result depends on the execution order. Example: two bank account processes try to withdraw money simultaneously → account balance may become incorrect.

how synchronization works ?

The goal is to ensure mutual exclusion and proper sequencing.

Mutual exclusion : Only one process can access the critical section (shared resource) at a time.

Event ordering / coordination : Processes may need to wait for certain conditions

Critical section

A critical section is a part of a program where a process accesses shared resources such as variables in shared memory, files or databases, i/o devices etc.

Critical section problem

The critical-section problem arises when multiple processes try to execute their critical sections concurrently, potentially causing data inconsistency or race conditions.

Solution

Software solutions (for simple systems) : use flags or turn variables to indicate which process can enter.

Hardware solutions : use special cpu instructions , that instructions atomically check and modify a variable, ensuring mutual exclusion

Operating system solutions : use locks / mutexes / semaphores provided by the operating system.

Example

Imagine two processes, p1 and p2, accessing a shared bank account balance:

Critical section : balance = balance – 100

if both processes execute this concurrently without synchronization:

  • Both read the same balance.
  • Both subtract 100 → balance is wrong.

Using a critical section solution, only one process updates the balance at a time → correct result.

Peterson’s solution

Peterson’s solution is a software-based algorithm to solve the critical-section problem for two processes.

it guarantees

Mutual exclusion – only one process in the critical section at a time

Progress – no unnecessary delay if the critical section is free

Bounded waiting – a process will eventually get a chance to enter

variables used

flag[2] – array of two boolean values.

flag[i] = true ( process i wants to enter its critical section )

turn – integer indicating whose turn it is to enter the critical section.

It helps resolve conflicts when both processes want to enter at the same time.

Semaphores

A semaphore is an integer variable used by an operating system to control access to shared resources and coordinate processes.

It prevents race conditions and ensures mutual exclusion when multiple processes access critical sections.

Semaphores can also manage access to multiple instances of a resource.

Deadlock

A deadlock occurs when two or more processes are stuck waiting for each other to release resources, and none can proceed.

Deadlock prevention is a set of strategies to ensure that at least one of the necessary conditions for deadlock cannot occur, so deadlocks never happen.