MA

Process Management

What is a Process?

  • A process is defined as the execution of a program that performs the actions specified in the program.
  • It represents a program in execution, consisting of the program code, current activity, and allocated resources.

Components of a Process in Memory

  • Text Section: Contains the compiled program code, brought in from non-volatile storage during execution.
  • Data Section: Stores global and static variables, initialized before the execution of the main function.
  • Heap: Used for dynamic memory allocation (managed via new, delete, malloc, free, etc.).
  • Stack: Used for local variables.
  • Memory Layout: The stack and heap start at opposite ends of the process's memory space and grow towards each other to avoid conflicts (stack overflow or allocation failures).

What is Process Management?

  • Process management involves various tasks including:
    • Creation of processes
    • Scheduling of processes
    • Termination of processes
    • Handling deadlocks
  • The operating system is responsible for resource allocation, allowing processes to share and exchange information efficiently.

Process States

  • A process state describes the current condition of a process at a specific time.
  • Five States of a Process:
    • New: The process is being created.
    • Ready: The process is loaded in memory, waiting to be executed.
    • Running: The process is being executed.
    • Waiting: The process is waiting for resources or events (e.g., I/O operations, inter-process messages).
    • Terminated/Halted: The process has completed execution.

Context Switching

  • Context switching involves saving the state of a currently running process and loading the state of another process.
  • It occurs when:
    • A high-priority process enters the ready state.
    • An interrupt is triggered.
    • A user/kernel mode switch occurs (not mandatory).
    • Preemptive CPU scheduling is implemented.

Context Switch vs Mode Switch

  • Context Switch:

    • Saves and restores the state of a CPU so multiple processes can share a single CPU resource.
    • Managed by the OS scheduler based on scheduling policies.
  • Mode Switch:

    • Refers to changing the CPU's privilege level (e.g., system call).
    • Occurs with system calls or faults.

Process Creation

  • Events leading to process creation include:
    • User request
    • System initialization
    • Batch job initialization
    • Execution of process creation system calls (e.g., fork(), spawn()).
  • Parent and Child Processes:
    • A process that creates another is the parent; the created process is the child.
    • fork() duplicates the current process in Unix-based systems; spawn() creates new processes at a higher level of abstraction.

Steps for Process Creation

  1. Initialization: Prepare for process creation.
  2. Allocation of Process ID (PID): A unique identifier for the new process.
  3. Memory Allocation: Allocate memory for code, data, heap, and stack.
  4. Creation of Process Control Block (PCB): Initialize the data structure managing the process.
  5. Scheduling: Schedule the process for execution based on OS policy.
  6. Execution: The process begins executing its instructions.

Process Termination

  • Occurs when the exit system call is invoked.

  • Resources such as memory and I/O devices are deallocated upon termination.

  • Causes of Termination:

    • The parent process terminating leads to child termination.
    • Memory scarcity (more memory needed than allocated).
    • Unauthorized resource access.
    • Resource allocation failures within the time limit.

Process Threads

  • A thread is a segment of a process. Multiple threads can exist within a single process.
  • Threads are like workers in a factory: each can perform tasks simultaneously under the supervision of the process.
  • Multithreading: The ability for a program to perform multiple threads concurrently, optimizing resource usage.

OS as a Scheduler

  • Types of schedulers:
    • Long-term Scheduler (Job Scheduler): Selects processes from the queue for memory allocation.
    • Short-term Scheduler (CPU Scheduler): Manages processes that are ready to execute and allocates CPU time.
    • Medium-term Scheduler (Swapper): Manages swapping of processes between main memory and secondary memory.

Process Scheduling

  • Process scheduling involves selecting which process to run and managing the removal of running processes from the CPU.
  • Goals include:
    • Keeping the CPU occupied.
    • Minimizing response time.

Scheduling Categories

  • Non-preemptive Scheduling: Once a process starts execution, it runs to completion.
  • Preemptive Scheduling: Allows a running process to be interrupted and moved back to the ready state.

Process Times

  • Arrival Time (AT): When the process enters the ready queue.
  • Completion Time: When a process finishes execution.
  • Burst Time (BT): Time required for CPU execution.
  • Turn Around Time (TT or TaT): TT = Completion Time - Arrival Time.
  • Waiting Time (WT): WT = Turn Around Time - Burst Time.

Process Scheduling Algorithms

  • Non-Preemptive Algorithms:

    • First Come First Serve (FCFS)
    • Shortest Job First (SJF)
    • Non-Preemptive Priority (NPP)
  • Preemptive Algorithms:

    • Round Robin (RR)
    • Shortest Remaining Time First (SRTF)
    • Preemptive Priority (PP)