Operating system

Course Contents Overview of Operating Systems

Operating system

An operating system (OS) is system software that manages computer hardware and software resources. It provides a variety of services to both application software and users.

The major functions of an operating system include process management, memory management, file system management, device management, user interface, security and access control, resource allocation, error detection and handling, networking, and system performance monitoring.

Operating systems can be categorized into various types, such as batch operating systems, time-sharing operating systems, distributed operating systems, embedded operating systems, and real-time operating systems.

Operating systems can be categorized into various types, each designed to address specific needs and constraints of target environments and applications. The major types include:

Batch Operating Systems

Processes are collected, batched, and run without user interaction. Examples: IBM OS/360

Time-Sharing Operating Systems

Multiple users share computer resources simultaneously. Examples: UNIX, Linux, and Windows

Distributed Operating Systems

Manages a group of independent computers and makes them appear to be a single computer. Examples: Google Chrome OS, Apache Hadoop

Embedded Operating Systems

Designed for specific devices and have limited functionalities. Examples: Android (in smartphones), iOS (in tablets and phones)

Real-Time Operating Systems

Provides immediate processing and response to input for applications that require timely outputs. Examples: QNX, VxWorks, RTEMS

Each type impacts how processes are managed and how resources are utilized, influencing their performance metrics.

Each type is designed to address specific needs and constraints of target environments and applications, impacting how processes are managed and how resources are utilized.

The efficiency and effectiveness of an OS are often evaluated based on its performance metrics including response time, throughput, resource utilization, and reliability.

OS also provides an interface for users, allowing them to interact with the computer system through command-line interfaces (CLI) or graphical user interfaces (GUI).

Key concepts in operating systems also include threads, scheduling algorithms, semaphores, and the principles of concurrency and synchronization.

  • Role & Purpose
      - Functionality mechanisms to support various systems such as client-server models and hand-held devices.
      - Address design issues influenced by security, networking, multimedia, and the Windows operating system.

1. Process Management

  • Responsible for creation, scheduling, and termination of processes.

  • Allocates CPU time to multitask different operations.

  • Ensures efficient execution of programs.

2. Memory Management

  • Manages the system’s main memory (RAM).

  • Allocates/deallocates memory to programs as required.

  • Prevents conflicts between processes using memory.

3. File System Management

  • Organizes and controls files and directories.

  • Manages storage, retrieval, naming, and protection of files.

  • Controls access permissions for files.

4. Device Management

  • Controls hardware devices such as printers, keyboards, and disks.

  • Uses drivers for hardware communication.

  • Allocates devices efficiently among different programs.

5. User Interface

  • Provides interaction methods for users with the computer.

  • Types include:
      - Command-Line Interface (CLI)
      - Graphical User Interface (GUI)

6. Security and Access Control

  • Protects data and system resources from unauthorized access.

  • Implements user authentication mechanisms (e.g., login systems).

  • Controls various permissions and privileges.

7. Resource Allocation

  • Distributes system resources (CPU, memory, devices) among users and programs.

  • Ensures fair and efficient resource usage.

8. Error Detection and Handling

  • Detects system errors and hardware failures.

  • Initiates corrective actions to maintain system stability.

9. Networking

  • Manages network connections and communication between systems.

  • Supports data sharing and remote access between computers.

10. System Performance Monitoring

  • Monitors system performance and usage metrics.

  • Helps optimize efficiency and troubleshoot technical issues.

Functionality Mechanisms to Support Client-Server Models

  • Operating systems provide specific mechanisms for efficient client-server operations, enabling communication, resource sharing, and coordination.

1. Inter-Process Communication (IPC)

  • Enables processes (clients and servers) to share data.

  • Common IPC methods include:
      - Message passing
      - Pipes
      - Shared memory
      - Facilitates smooth communication between processes on the same machine.

2. Networking Support

  • Provides networking protocols and communication interfaces.

  • Utilizes standard protocols such as TCP/IP to allow inter-machine communication.

3. Socket Mechanism

  • Sockets serve as communication endpoints between clients and servers.

  • Support both connection-oriented (TCP) and connectionless (UDP) communication protocols.

4. Process and Thread Management

  • Operating systems support concurrency through processes and threads.

  • Servers can handle multiple clients simultaneously via multi-threading or multi-processing to enhance responsiveness and scalability.

5. Remote Procedure Call (RPC)

  • Allows remote execution of procedures as if they were local, facilitating easier distributed computing.

6. Naming and Directory Services

  • Assists clients in locating servers by maintaining service names and network address mappings.

7. Synchronization Mechanisms

  • Ensures safe access to shared resources, employing tools like:
      - Semaphores
      - Mutexes
      - Monitors

  • Protects data consistency when accessed by multiple clients.

8. Security Mechanisms

  • Authentication: Confirms legitimate users/clients.

  • Authorization: Controls access rights to resources.

  • Encryption: Safeguards data during communications.

9. File and Resource Sharing

  • Allows servers to provide shared resources and manages consistent access to these resources.

10. Load Balancing and Scheduling

  • Distributes workloads effectively among servers or processes to prevent overload and optimize utilization.

Process Management: Processes, Threads, CPU Scheduling, and Synchronization

1. Introduction to Process Management

  • Core responsibility of an OS involving creation, execution, and termination of processes.

  • Aims for efficient CPU utilization and smooth program execution.

2. Processes Definition

  • A process is an executing program which includes:
      - Program code
      - Current activity (e.g., program counter, registers)
      - Allocated resources (memory, files, I/O devices)

Process States
  • New: Process is in creation phase.

  • Ready: Waiting for CPU assignment.

  • Running: Actively executing.

  • Waiting (Blocked): Awaiting I/O or events.

  • Terminated: Completed execution.

Process Control Block (PCB)
  • Represents each process, containing:
      - Process ID (PID)
      - Process state
      - CPU registers
      - Memory management information
      - Scheduling information

Operations on Processes
  • Includes:
      - Process creation (e.g., using fork or spawn functions)
      - Process termination
      - Process suspension and resumption

3. Threads Definition

  • A thread is the smallest execution unit within a process. Each process can contain multiple threads that share the same resources.

Types of Threads
  • User-level threads: Managed by user applications.

  • Kernel-level threads: Managed by the operating system.

Advantages of Threads
  • Faster context switching.

  • Improved responsiveness due to concurrent execution.

  • Efficient sharing of resources.

  • Enhanced parallel processing capabilities.

Multithreading Models
  • One-to-One

  • Many-to-One

  • Many-to-Many

4. CPU Scheduling Definition

  • Determines which process receives CPU time and at what intervals.

Scheduling Objectives
  • Maximize CPU utilization.

  • Minimize waiting time.

  • Minimize turnaround time.

  • Maximize throughput of processes.

  • Ensure fairness in process scheduling.

Types of Scheduling
  • Preemptive: The OS can interrupt a running process.

  • Non-preemptive: Process continues until it completes or is blocked.

Common Scheduling Algorithms
  1. First Come First Serve (FCFS)
       - Processes are executed in the order they arrive.
       - Simple but may lead to long waiting times.

  2. Shortest Job First (SJF)
       - Executes the job with the shortest duration first.
       - Considered optimal but difficult to predict.

  3. Round Robin (RR)
       - Each process receives a fixed time slot (quantum).
       - Suitable for time-sharing systems.

  4. Priority Scheduling
       - Assigns priority levels to processes for execution.
       - Risk of starvation for lower priority processes.

  5. Multilevel Queue Scheduling
       - Classifies processes into distinct queues based on priority levels.

5. Process Synchronization Definition

  • Ensures that multiple processes can safely access shared resources without conflicts.

Critical Section Problem
  • Critical Section: Part of the code that accesses shared resources. Requirements:
      - Mutual Exclusion: Only one process in the critical section at any time.
      - Progress: No unnecessary delays for waiting processes.
      - Bounded Waiting: Limited waiting time for processes.

Synchronization Mechanisms
  1. Mutex (Mutual Exclusion Locks)
       - Ensures that only one process can access a resource at any moment.

  2. Semaphores
       - Integer variables that manage access.
       - Types include:
         - Binary Semaphore (0 or 1)
         - Counting Semaphore

  3. Monitors
       - Higher-level synchronization constructs utilizing condition variables.

  4. Spinlocks
       - A method where a process loops while waiting for a lock.

6. Problems in Process Synchronization

  • Deadlock:
      - Processes waiting indefinitely for resources held by each other.

  • Starvation:
      - A process is indefinitely delayed from receiving resources.
      - Simple analogy: A queue where VIPs continually push ahead, leaving others without service.

  • Race Condition:
      - Occurs when output depends on the order of execution, leading to inconsistencies.

Key Characteristics for Deadlock
  • Mutual Exclusion: Some resources cannot be shared.

  • Hold and Wait: Processes hold resources while they wait for additional resources.

  • No Preemption: Resources can't be forcibly taken from processes.

  • Circular Wait: Forming a circular chain of waiting processes.

7. Summary of Process Management

  • Enables the efficient execution of multiple programs through:
      - Management of processes and threads.
      - Effective CPU resource scheduling.
      - Safe access to shared resources via synchronization.

  • Crucial for system performance, fairness, and stability.

Comparison of Starvation, Deadlock, and Livelock

  • Starvation: An individual process waits too long due to priority biases.
      - Example: Low-priority processes that never execute.

  • Deadlock: Processes are indefinitely blocked from proceeding due to mutual dependency.
      - Example: Process A waiting for B and vice versa leading to a standstill.

  • Livelock: Processes constantly change status without making progress.
      - Example: Two processes continuously yielding to each other without completing tasks.

Comparison Table

Feature

Starvation

Deadlock

Livelock

Basic Idea

Unfair waiting

Circular waiting

Endless reacting without progress

Cause

Priority bias or unfair scheduling

Circular resource dependency

Excessive cooperation

Progress

Some processes continue progressing

No process continues

No useful progress

CPU Usage

Low (waiting)

None (blocked)

High (active looping)

Solution

Aging, fair scheduling

Resource allocation strategies

Add randomness or better coordination

Easy Way to Remember
  • Starvation → "Ignored forever"

  • Deadlock → "Stuck forever"

  • Livelock → "Moving but going nowhere"

Real-Life Analogies
  • Starvation: Someone never gets food because others keep cutting the line.

  • Deadlock: Two cars in a narrow street cannot move.

  • Livelock: Two people stepping aside to let each other pass but blocking each other.

Memory Management and Virtual Memory

1. Introduction to Memory Management

  • Memory management is a fundamental OS function that oversees:
      - Allocation and deallocation of memory.
      - Tracking of memory usage.
      - Efficient and safe access to memory resources.

Objectives
  • Maximize memory utilization.

  • Minimize fragmentation in memory usage.

  • Enable effective multitasking.

  • Protects memory spaces of processes.

2. Types of Memory

  • Primary Memory (RAM): Fast, volatile memory directly accessed by the CPU.

  • Secondary Memory: Slower, non-volatile storage (like hard drives).

  • Cache Memory: Very fast, small-sized memory closer to the CPU.

3. Memory Allocation Techniques

3.1 Contiguous Allocation
  • Each process is allocated a single, continuous block of memory.

  • Types of Contiguous Allocation:
      - Fixed Partitioning: Predefined block sizes resulting in possible internal fragmentation.
      - Dynamic Partitioning: Partitions adjusted based on process size, reducing internal fragmentation but risking external fragmentation.

3.2 Non-Contiguous Allocation
  • Allows processes to be divided and allocated memory in separate locations using:
      - Paging
      - Segmentation

4. Paging

  • Memory is divided into frames, while processes are segmented into pages.

  • Address Translation: Logical addresses are converted to physical addresses using a page table.

  • Advantages:
      - Eliminates external fragmentation.
      - Efficient memory usage.

  • Disadvantages:
      - Internal fragmentation can occur due to unused space in allocated pages.
      - Overhead related to maintaining page tables.

5. Segmentation

  • Memory divided into logical segments (e.g., code, data, stack).

  • Features:
      - Each segment specified by a base address and limit.
      - Aligns with the programmer's perspective of memory.

  • Advantages:
      - Easier for developers to manage.
      - Supports modular programming principles.

  • Disadvantages:
      - Possibility of external fragmentation.

6. Swapping

  • Temporarily transfers a process from RAM to disk to free up memory resources.

7. Virtual Memory

Definition
  • Virtual Memory: A mechanism allowing the execution of processes larger than the physical memory by utilizing disk space as RAM extension.

8. How Virtual Memory Works

  • Only portions of a process are loaded into RAM, with the remainder stored on disk. Pages are loaded as needed, a method known as Demand Paging.

9. Demand Paging

  • Pages are loaded into RAM only when required, triggering a Page Fault when a required page is absent.

Page Fault Handling Steps
  1. Verify access validity.

  2. Locate the needed page on disk.

  3. Load the page into memory.

  4. Update the page table accordingly.

10. Page Replacement Algorithms

  • When memory is at capacity, the OS replaces existing pages using various algorithms, such as:
      - FIFO (First-In First-Out)
      - LRU (Least Recently Used)
      - Optimal Algorithm (theoretical best but impractical).

11. Thrashing

  • Occurs when frequent page faults lead to excessive swapping, slowing the system more than executing processes.

12. Fragmentation

  • Internal Fragmentation: Wasted space within allocated blocks.

  • External Fragmentation: Free memory divided into scattered small chunks.

13. Memory Protection

  • Ensures processes don't access each other's memory.

  • Techniques include using:
      - Base and limit registers
      - Access control bits

14. Advantages of Virtual Memory

  • Enables execution of larger programs.

  • Promotes efficient memory usage.

  • Supports multitasking capabilities.

  • Reduces input/output operations.

15. Disadvantages of Virtual Memory

  • Performance may decrease due to slower disk access during page faults.

  • Implementation complexity is heightened.

  • Risks of thrashing due to excessive memory swapping.

16. Summary of Memory Management

  • Crucial for effective RAM usage by employing paging and segmentation for flexibility.

  • Virtual memory allows for disk use as an extension of RAM.

  • Appropriate algorithms are essential to mitigate performance drawbacks.

Exam Tips

  • Distinguish clearly between paging and segmentation.

  • Be prepared to describe the page fault process and replacement algorithms.

  • Understand and explain concepts of thrashing and fragmentation.