Operating SystemUnit 26 min read

Processes & Threads: Definitions, States, Models & Multithreading

Unit 2 of Operating System covers the core concepts of processes (PCB, states, creation/deletion), threads (lightweight processes, types, advantages), inter-process communication (IPC), and real-world applications in banking, e-commerce, and mobile apps like Khalti or Pathao.

Key Concepts: Processes

A process is an instance of a program in execution, with its own memory space, resources, and execution context. It is managed by the OS and is the basic unit of work in a system.

1. Process Definition & Components

A process consists of:

  • Program Code: The executable instructions.
  • Data: Inputs, outputs, and variables.
  • Process Control Block (PCB): A data structure that holds process metadata.
classDiagram
    class Process {
        +PID: int
        +Process State: {New, Ready, Running, Waiting, Terminated}
        +Program Counter: int
        +CPU Registers: array
        +CPU Scheduling Info: struct
        +Memory Limits: struct
        +I/O Status Info: struct
        +Accounting Info: struct
    }
    class PCB {
        <<Data Structure>>
        +Process ID (PID)
        +Process State
        +Program Counter
        +CPU Registers
        +CPU Scheduling Info
        +Memory Management Info
        +I/O Requests
        +Accounting Info
    }
    Process --> PCB : "Contains"

2. Process States & Transitions

A process goes through five states during its lifecycle:

stateDiagram-v2
    [*] --> New: Process Creation
    New --> Ready: Admitted to Ready Queue
    Ready --> Running: CPU Allocation
    Running --> Waiting: I/O or Event Request
    Waiting --> Ready: I/O Completion
    Running --> Terminated: Process Exit
    Terminated --> [*]

Example:

  • Khalti App (Mobile Banking): When you request a fund transfer, the process moves from Ready → Running (CPU executes the transaction), then to Waiting (awaiting bank server response), and finally back to Ready after confirmation.

Key Concepts: Threads

A thread is a lightweight subprocess within a process, sharing the same memory space but having its own stack and registers.

1. Thread vs. Process

Feature Process Thread
Definition Independent execution unit Lightweight subprocess
Memory Space Separate (isolated) Shared (same as parent process)
Creation Time Slow (OS overhead) Fast (less overhead)
Communication IPC (Inter-Process Communication) Shared memory (faster)
Example Running Chrome, Firefox separately Multiple tabs in a single browser

2. Types of Threads

  • User-Level Threads (ULT): Managed by a thread library (e.g., POSIX threads).
  • Kernel-Level Threads (KLT): Managed by the OS (e.g., Linux threads).
  • Hybrid Threads: Combines ULT and KLT (e.g., Solaris).

Example:

  • WhatsApp (Mobile App):
    • Uses multiple threads for:
      • UI rendering (separate thread to keep chat smooth).
      • Network requests (background thread for downloading media).
      • Message processing (separate thread for encryption/decryption).

Process & Thread Management

1. Process Creation

  • Parent Process (fork() in Unix): Creates a child process.
  • Child Process (exec() in Unix): Replaces the child’s memory with a new program.

Example (Linux Command):

fork() → Creates a child process with PID.
exec("ls") → Replaces child’s memory with the `ls` command.

2. Thread Creation (Pthreads Example)

#include <pthread.h>
void* thread_function(void* arg) {
    printf("Thread executing\n");
    return NULL;
}
int main() {
    pthread_t thread_id;
    pthread_create(&thread_id, NULL, thread_function, NULL);
    pthread_join(thread_id, NULL); // Wait for thread to finish
    return 0;
}

3. Inter-Process Communication (IPC)

Methods for processes to communicate:

  • Shared Memory: Fastest (threads use this).
  • Message Passing: Slower (e.g., pipes, sockets).
  • Semaphores & Mutexes: Synchronization tools.

Example:

  • Nepal Stock Exchange (NEPSE) Trading System:
    • Uses message queues to pass buy/sell orders between processes.
    • Semaphores ensure only one order is processed at a time to avoid conflicts.

In the Real World

  1. Khalti (Mobile Banking App)

    • Uses multiple threads for:
      • UI updates (smooth transactions).
      • Network calls (secure API requests to banks).
      • Background sync (updating transaction history).
  2. Pathao (Ride-Hailing App)

    • Processes:
      • Driver location tracking (separate process).
      • Payment processing (separate process for security).
    • Threads:
      • Real-time GPS updates (separate thread).
      • Ride matching algorithm (CPU-intensive thread).
  3. NTC (Nepal Telecom) Call Routing

    • Processes:
      • Incoming call handler.
      • Billing system.
    • Threads:
      • Multiple calls handled in parallel (thread pool).
      • Background call logging (separate thread).

Exam Tip

  1. Process vs. Thread: Always compare memory usage, speed, and communication methods.
  2. PCB Structure: Know the key fields (PID, state, registers, scheduling info).
  3. Thread Types: Differentiate between user-level and kernel-level threads.
  4. Real-World Examples: Relate processes/threads to apps like Khalti, Pathao, or NEPSE.
  5. State Transitions: Draw the 5-state process diagram in exams.
  6. IPC Methods: Know when to use shared memory vs. message passing.

Worked Example: Bank Loan Processing (Processes & Threads) Scenario: A bank processes loan applications with:

  • Processes:
    • Credit Checker (separate process).
    • Approval Engine (separate process).
  • Threads:
    • UI Handler (updates loan status in real-time).
    • Database Writer (logs approvals asynchronously).

Trace:

  1. User submits loan → New Process (Credit Checker starts).
  2. Credit Checker runs → Running State.
  3. If documents missing → Waiting State (awaits user input).
  4. Approval Engine runs in parallel (separate thread).
  5. Final decision → Terminated (process ends).

Final Note:

  • Processes = Heavyweight, isolated execution units.
  • Threads = Lightweight, share memory, faster communication.
  • Real-world apps (Khalti, Pathao, NEPSE) rely on both for efficiency.

Practice:

  1. Draw the 5-state process diagram.
  2. Write a pthread example for a chat app.
  3. Compare process vs. thread in a table.
  4. Explain how NTC call routing uses processes/threads.

Based on the TU BITM syllabus for Operating System (IT241), unit 2.

Discussion

Loading…