Operating System and Process Management Illustration

Operating system basics

An in-depth guide on operating system basics, process states, scheduling, synchronization, memory allocation, and management techniques.

Operating System Basics

What is an Operating System?

An Operating System (OS) is system software that manages hardware and software resources while providing common services for computer programs. It acts as an interface between users and computer hardware.

Key Responsibilities of an OS:

  1. Process Management – Handles process scheduling, execution, and resource allocation.
  2. Memory Management – Allocates and deallocates memory to processes.
  3. File System Management – Manages data storage, retrieval, and organization.
  4. Device Management – Controls communication between hardware devices and software.
  5. Security and Access Control – Protects data and system integrity.
  6. User Interface (UI) – Provides CLI (Command Line Interface) or GUI (Graphical User Interface).

Types of Operating Systems:

  1. Real-Time OS (RTOS) – Designed for real-time applications (e.g., FreeRTOS).
  2. Distributed OS – Runs on multiple machines to provide high availability.
  3. Multiprogramming OS – Allows multiple programs to execute simultaneously.
  4. Batch OS – Executes batches of jobs with minimal user interaction.
  5. Time-Sharing OS – Provides multiple users access to a system simultaneously.

Process Management

What is a Process?

  • A process is a program loaded in memory and currently executing.
  • It consists of four main sections:
    • Stack: Stores function parameters, return addresses, and local variables.
    • Heap: Dynamically allocated memory (e.g., using malloc or calloc).
    • Data: Stores global variables.
    • Text: Contains compiled code, processor registers, and current execution state.

Process States

A process goes through multiple states during execution:

  1. Start – Process is created.
  2. Ready – Process is waiting for CPU execution.
  3. Running – Process is being executed.
  4. Waiting – Process is waiting for I/O or other resources.
  5. Terminated – Process has completed execution.

Process Control Block (PCB)

  • The PCB is a data structure maintained by the OS for each process.
  • It stores essential information to track process execution.
  • Common PCB elements:
    • Process State
    • Privileges
    • Program Counter
    • Register Information

Process Scheduling

  • Process scheduling is managed by the OS to determine which process executes at a given time.
  • Scheduling falls into two categories:
    • Preemptive: The OS allocates CPU time for a fixed period before switching processes.
    • Non-Preemptive: A process runs until it completes or voluntarily releases CPU.

Context Switching

  • The mechanism of saving the current CPU state to PCB and restoring it when resuming the process.
  • This is computationally expensive, so some systems have multiple CPU register sets to optimize switching.

Scheduling Algorithms

  1. First-Come, First-Serve (FCFS) – Processes are executed in the order they arrive.
  2. Shortest Job Next (SJN) – The process with the shortest execution time runs first.
  3. Priority-Based Scheduling – Processes are assigned priorities; highest priority runs first.
  4. Shortest Remaining Time (SRT) – The process closest to completion gets priority but can be preempted.
  5. Round Robin (RR) – Each process gets a fixed time slice before switching.
  6. Multilevel Queue Scheduling – Multiple queues exist for different types of processes, each with its own scheduling algorithm.

Memory Management

What is Memory Management?

  • Memory is the primary storage space where programs and data reside during execution.
  • It is fast but limited compared to secondary storage.
  • The OS is responsible for:
    • Allocation: Assigning memory to processes.
    • Deallocation: Reclaiming memory after processes complete.
    • Fragmentation Management: Handling scattered free memory using paging or segmentation.
    • Protection: Preventing unauthorized memory access to ensure system stability.

Memory Management Techniques

Different systems use different techniques, or combinations of them:

  1. Contiguous Allocation:

    • Memory is divided into fixed-size blocks and allocated sequentially.
    • Issue: Leads to external fragmentation (unused memory between allocations).
  2. Paging:

    • Memory and processes are divided into fixed-size pages.
    • Allows non-contiguous allocation but may lead to internal fragmentation (unused memory within pages).
  3. Segmentation:

    • Process is divided into logical segments (e.g., code, data, stack).
    • Allows non-contiguous allocation, reducing fragmentation.
  4. Virtual Memory and Demand Paging:

    • Processes are partially loaded into memory, with additional parts loaded on demand.
    • Improves efficiency but may lead to page faults (frequent memory swaps).

Process vs. Thread

AspectProcessThread
DefinitionA program in execution with its own memory space.A lightweight process that shares memory with others.
MemoryHas its own memory space.Shares memory with other threads in the same process.
CommunicationRequires Inter-Process Communication (IPC).Shares memory, enabling faster communication.
OverheadHigher (due to memory allocation, scheduling).Lower (threads share system resources).
ExecutionProcesses run independently.Threads can run concurrently within a process.

Questions and Answers

  1. What is an operating system?

    • An operating system is a software program that manages and handles all programs and resources. It is responsible for managing, coordinating, and handling all system resources and activities.
  2. What are the main functions of an operating system?

    • Memory and Process Management
    • File Management and Resource Management
    • Providing a User Interface
    • Security
    • Error Detection
    • Scheduling of Resources and Jobs
  3. What are the different types of operating systems?

    • Real-Time Operating System (RTOS) – Example: FreeRTOS
    • Distributed Operating System
    • Time-Sharing Operating System
    • Multiprogrammed OS
    • Batch OS
  4. What is a multiprocessor operating system?

    • A system with more than one CPU that shares memory among processors. It improves performance by running multiple programs concurrently.
  5. What is a pipe? When is it used?

    • A pipe is a connection between two related programs. It is used for one-way interprocess communication to pass messages between programs.
  6. What are the different operations possible on a semaphore?

    • wait() & signal() operations.
  7. What is a Bootstrap?

    • Bootstrap is the first piece of code that executes when a device is turned on. It is stored at a fixed location and loads the operating system into memory for execution.