Introduction to Operating Systems
OPERATING SYSTEM UNIT – I
Concept of Operating Systems
An operating system (OS) is software that acts as an intermediary between computer hardware and user applications. It provides services such as managing hardware resources, running applications, and providing an interface for users to interact with the computer.
Generations of Operating Systems
1. First Generation (1940s-1950s)
Technology:
Vacuum tubes were used as electronic components.
Characteristics:
- Large in size, consuming a lot of power and generating significant heat.
- Relatively slow and unreliable compared to modern standards.
- Often programmed using low-level machine language or assembly language.
Examples:
ENIAC, UNIVAC I.
2. Second Generation (1950s-1960s)
Technology:
Transistors replaced vacuum tubes, leading to smaller, more reliable, and more energy-efficient computers.
Characteristics:
- Faster and more reliable than first-generation computers.
- Magnetic core memory was used for storage.
- Assembly language and early high-level programming languages emerged.
Examples:
IBM 1401, IBM 7090.
3. Third Generation (1960s-1970s)
Technology:
Integrated circuits (ICs) allowed for further miniaturization and improved performance.
Characteristics:
- Smaller, faster, and more powerful than previous generations.
- Introduction of operating systems for multitasking and time-sharing.
- High-level programming languages became more widespread.
Examples:
IBM System/360, DEC PDP-11.
4. Fourth Generation (1970s-Present)
Technology:
Microprocessors enabled the development of small, affordable, and powerful computers.
Characteristics:
- Personal computers (PCs) became widely available, leading to the democratization of computing.
- Graphical user interfaces (GUIs) and networking capabilities were introduced.
- Evolution of programming languages, software development methodologies, and internet technologies.
Examples:
IBM PC, Apple Macintosh, Commodore 64.
5. Fifth Generation (Present and Beyond)
Technology:
Currently ongoing, characterized by advancements in artificial intelligence (AI), quantum computing, and other emerging technologies.
Characteristics:
- Focus on AI, machine learning, robotics, and other cutting-edge technologies.
- Continued miniaturization and integration of computing devices into various aspects of daily life.
- Exploration of new computing paradigms, such as quantum computing, DNA computing, and neuromorphic computing.
Examples:
Quantum computers (e.g., IBM Q System One, Google Quantum Computer).
Types of Operating Systems
- Batch OS: Processes data in batches without user interaction.
- Multiprogramming OS: Allows multiple programs to run concurrently.
- Time-sharing OS: Allows multiple users to interact with the computer simultaneously.
- Real-time OS: Provides guaranteed response times for critical tasks.
- Distributed OS: Manages a group of interconnected computers and makes them appear as a single system.
- Embedded OS: Designed to operate within embedded systems like appliances, vehicles, etc.
4. OS Services
- Program execution
- I/O operations
- File system manipulation
- Communication
- Error detection and handling
- Resource allocation
- Accounting
- Protection and security
5. System Calls
System calls are functions provided by the operating system that can be called by user programs to request services from the operating system, such as creating processes, accessing files, or managing memory.
6. Structure of an OS
- Layered OS: Divides the operating system into layers, with each layer providing services to the layer above it.
- Monolithic OS: All operating system components reside in a single kernel space.
- Microkernel OS: Moves as much from the kernel into “user space” as possible, thus reducing the kernel’s complexity.
Explanation
1. Layered Operating System
Concept:
In a layered operating system, the system is divided into a hierarchy of layers, with each layer providing a set of related functions and services. Each layer only interacts with the layer directly below it and provides services to the layer directly above it.
Characteristics:
- Each layer has a specific function, such as process management, memory management, file system management, etc.
- Communication between layers typically occurs through well-defined interfaces or APIs.
- Layers are arranged in a hierarchy, with lower-level layers dealing with hardware-related tasks and higher-level layers providing more abstract and user-oriented services.
- Changes to one layer usually don’t affect other layers, which can simplify maintenance and modification.
Advantages:
- Modularity: Easy to understand and modify because of the clear separation of concerns.
- Maintainability: Changes in one layer can often be made without affecting other layers.
- Portability: Layers can be easily ported to different hardware architectures.
Disadvantages:
- Overhead: Communication between layers can introduce overhead, impacting performance.
- Rigidity: Adding or removing layers can be challenging and may require significant restructuring of the system.
- Complexity: Managing interactions between layers can be complex, especially in large systems.
2. Monolithic Operating System
Concept:
In a monolithic operating system, all operating system components reside in a single kernel space. This means that the kernel provides all operating system services, including process management, memory management, device drivers, file system support, etc.
Characteristics:
- All operating system components are tightly integrated into a single executable image.
- System calls are used to request services from the kernel, which directly manipulates hardware resources.
- Components within the kernel can call each other directly, without going through well-defined interfaces.
Advantages:
- Efficiency: Direct communication between components can result in better performance compared to layered architectures.
- Simplicity: There is no overhead associated with inter-layer communication, making the system simpler and easier to understand.
Disadvantages:
- Lack of modularity: Changes to one part of the kernel can affect other parts, making the system harder to maintain and extend.
- Scalability: Monolithic kernels can become bloated as more features are added, making them less scalable for large systems.
- Reliability: A bug or crash in one part of the kernel can potentially crash the entire system.
3. Microkernel Operating System
Concept:
In a microkernel operating system, the kernel is kept as small as possible, with only essential functions such as process management, memory management, and inter-process communication (IPC) implemented in the kernel space. Additional services, such as device drivers and file systems, are implemented as user-space processes.
Characteristics:
- The kernel provides basic services and mechanisms for communication between user-space components.
- Most operating system services run as user-space processes, known as servers, which communicate with the kernel via message passing.
- Microkernel architectures aim to maximize modularity and flexibility by moving non-essential services out of the kernel space.
Advantages:
- Modularity: Easier to extend and maintain because services are implemented as separate user-space processes.
- Reliability: Fault isolation is improved because failures in one component (e.g., a device driver) are less likely to crash the entire system.
- Security: Reduced attack surface because only essential services run in kernel space.
Disadvantages:
- Performance: Message passing between user-space servers and the kernel can introduce overhead, potentially impacting performance.
- Complexity: Managing communication and synchronization between user-space servers and the kernel can be complex, especially in distributed systems.
- Development overhead: Implementing services as separate user-space processes can introduce additional development and testing overhead.
7. Concept of Virtual Machine
A virtual machine (VM) is an emulation of a computer system that runs on top of a physical hardware platform. It allows multiple operating systems to run simultaneously on a single physical machine, providing isolation, flexibility, and resource allocation. VMs are often used for server consolidation, testing environments, and running legacy software.
UNIT – II
Processes
- Definition: A process is an instance of a program in execution. It consists of the program code, data, and execution context (such as program counter, registers, and stack).
- Process Relationship: Processes can be related in various ways, such as parent-child relationships (where one process creates another), peer relationships (where processes share resources or communicate), and ancestor-descendant relationships (where processes share a common lineage).
- Different States of a Process:
- New: The process is being created.
- Ready: The process is ready to execute but waiting for the CPU.
- Running: The process is currently being executed.
- Blocked (Waiting): The process is waiting for an event (such as I/O completion) before it can continue.
- Terminated: The process has finished execution.
- Process State Transitions: Processes can transition between these states based on events and scheduling decisions. For example, a new process moves to the ready state when it’s ready to execute, and a running process moves to the blocked state when it requires I/O.
- Process Control Block (PCB): PCB is a data structure that contains information about a process, such as process ID, state, program counter, CPU registers, memory management information, and accounting information. It is used by the operating system to manage and control processes.
- Context Switching: Context switching is the process of saving the state of a running process, loading the state of another process, and switching execution from one process to another. It is necessary for multitasking and allows the operating system to efficiently manage multiple processes.
Threads
- Definition: A thread is the smallest unit of execution within a process. Threads share the same memory space and resources within a process and can execute independently.
- Various States: Threads typically have the following states:
- Ready: The thread is ready to execute but waiting for the CPU.
- Running: The thread is currently being executed.
- Blocked (Waiting): The thread is waiting for an event before it can continue.
- Benefits of Threads: Threads allow for concurrent execution within a process, enabling parallelism, improved responsiveness, and efficient resource utilization.
- Types of Threads: Threads can be either user-level threads (managed by a user-level library) or kernel-level threads (managed by the operating system).
- Concept of Multithreading: Multithreading is the ability of an application to create multiple threads of execution within a single process. It allows for concurrent execution of multiple tasks and can improve application performance and responsiveness.
Process Scheduling
- Foundation and Scheduling Objectives: Process scheduling is the act of deciding which process/thread to execute next on the CPU. The main objectives of process scheduling include maximizing CPU utilization, minimizing response time, maximizing throughput, and ensuring fairness.
- Types of Schedulers:
- Long-term Scheduler (Job Scheduler): Selects which processes should be admitted to the system for execution.
- Short-term Scheduler (CPU Scheduler): Selects which ready process/thread should be executed next on the CPU.
- Medium-term Scheduler: Swaps processes/threads between main memory and secondary storage (e.g., disk) to improve system performance.
- Scheduling Criteria: Various criteria are used to evaluate scheduling algorithms, including CPU utilization, throughput, turnaround time, waiting time, and response time.
- Scheduling Algorithms:
- Preemptive: Allows the scheduler to interrupt a currently running process/thread to start or resume another, typically based on priority or time quantum.
- Non-preemptive: Does not allow interruption of a running process/thread; the currently running process/thread must voluntarily release the CPU.
- FCFS (First-Come, First-Served), SJF (Shortest Job First), RR (Round Robin): Examples of common scheduling algorithms used in operating systems.
- Multiprocessor Scheduling: Involves scheduling processes/threads on systems with multiple CPUs or processor cores. Techniques include load balancing, affinity scheduling, and gang scheduling.
UNIT – III
Critical Section
- Definition: A critical section is a segment of code that accesses shared resources (such as variables, data structures, or devices) that must be executed atomically, i.e., without interruption by other processes or threads.
- Purpose: Critical sections are used to prevent race conditions and ensure mutual exclusion, where only one process or thread can access the shared resource at a time.
Race Conditions
- Definition: A race condition occurs when the behavior of a system depends on the sequence or timing of uncontrollable events, such as the execution order of concurrent processes or threads accessing shared resources.
- Example: Two processes incrementing a shared variable simultaneously without proper synchronization can lead to unpredictable behavior, as the final value depends on the interleaving of their executions.
Mutual Exclusion
- Definition: Mutual exclusion is a synchronization technique that ensures only one process or thread can access a shared resource at any given time. It prevents concurrent access that could lead to race conditions or data inconsistencies.
- Goal: To prevent interference between concurrent accesses and maintain data integrity.
- Mechanisms: Mutual exclusion can be achieved using software-based approaches (such as locks, semaphores, or monitors) or hardware-based solutions (such as atomic instructions or hardware locks).
Hardware Solution
- Definition: Hardware-based solutions for mutual exclusion typically involve special instructions or mechanisms provided by the processor or hardware architecture to support atomic operations or enforce mutual exclusion.
- Examples: Hardware locks (test-and-set, compare-and-swap), atomic instructions (load-linked/store-conditional), or processor interrupts and instructions that disable interrupts during critical sections.
Deadlocks
- Definition: A deadlock occurs in a system when two or more processes or threads are unable to proceed because each is waiting for a resource held by the other(s), resulting in a circular waiting dependency.
- Necessary Conditions for Deadlock:
- Mutual Exclusion: At least one resource must be held in a non-shareable mode (e.g., exclusive access).
- Hold and Wait: Processes must hold allocated resources while waiting for additional resources.
- No Preemption: Resources cannot be forcibly taken away from a process.
- Circular Wait: A cycle of processes exists, where each process is waiting for a resource held by the next process in the cycle.
Deadlock Prevention
- Approaches: Deadlock prevention techniques aim to ensure that at least one of the necessary conditions for deadlock cannot hold.
- Examples: Resource allocation strategies (e.g., only allocate resources when all are available), ordering resources (e.g., impose a total ordering and require processes to request resources in order), and deadlock detection mechanisms.
Deadlock Avoidance
- Banker’s Algorithm: A deadlock avoidance algorithm that dynamically allocates resources based on a system’s current state to avoid unsafe resource allocations that could lead to deadlock.
- Concept: Processes must declare their maximum resource requirements upfront, and the system uses the Banker’s algorithm to determine if allocating resources to a process would result in a safe state (i.e., no deadlock).
Deadlock Detection and Recovery
- Deadlock Detection: Periodically checks the system for the existence of a deadlock using resource allocation graphs or other techniques.
- Deadlock Recovery: Once a deadlock is detected, recovery strategies may involve aborting processes, rolling back transactions, or preempting resources to break the deadlock.
UNIT – IV
Memory Management
1. Basic Concept
- Definition: Memory management is the process of managing computer memory to accommodate multiple processes or programs running simultaneously. It involves allocating memory, keeping track of memory usage, and ensuring efficient utilization of available memory resources.
2. Logical and Physical Address Mapping
- Logical Address: The address generated by the CPU during program execution, also known as a virtual address.
- Physical Address: The actual address in the memory hardware where data is stored.
3. Memory Allocation
- Contiguous Memory Allocation:
- Fixed Partition: Divides memory into fixed-size partitions, each partition can accommodate one process.
- Variable Partition: Divides memory dynamically based on the size of the process, reducing internal fragmentation.
- Fragmentation:
- Internal Fragmentation: Wastage of memory within a partition due to the allocation of more memory than required by a process.
- External Fragmentation: Wastage of memory outside of allocated partitions that cannot be used for allocation.
- Compaction: Reorganizing memory to reduce fragmentation by moving processes to consolidate free memory into contiguous blocks.
4. Paging
- Concept: Memory is divided into fixed-size blocks called pages. Processes are also divided into fixed-size blocks called page frames.
- Hardware and Control Structures: Hardware support (e.g., page table) and control structures (e.g., page table entries) are used to map logical addresses to physical addresses.
- Locality of Reference: The tendency of programs to access memory locations near each other.
- Page Fault: Occurs when a program tries to access a page that is not currently in memory, leading to a page fault interrupt.
- Working Set: Set of pages that a process is currently using.
- Dirty Page/Dirty Bit: Indicates whether a page has been modified since it was last loaded into memory.
- Demand Paging: Only loads pages into memory when they are needed, reducing initial memory overhead.
- Page Replacement Algorithms: Used when a page needs to be evicted from memory to make space for a new page.
- Optimal: Replaces the page that will not be used for the longest time in the future (theoretically optimal but not practical).
- First In First Out (FIFO): Replaces the oldest page in memory.
- Second Chance (SC): A variation of FIFO that gives pages a second chance before replacement if they have been referenced recently.
- Not Recently Used (NRU): Classifies pages based on recent usage and replaces a page from the least referenced class.
- Least Recently Used (LRU): Replaces the page that has not been used for the longest time. Various implementations exist, such as keeping a counter or using a linked list.
UNIT – V
I/O Management, File Management, and Disk Management
1. I/O Hardware
- I/O Devices: Peripheral devices used to input or output data to/from a computer system, such as keyboards, mice, printers, monitors, disk drives, network cards, etc.
- Device Controllers: Hardware components responsible for managing communication between the CPU and I/O devices. They handle low-level operations such as data transfer, error detection, and device status monitoring.
- Direct Memory Access (DMA): A technique that allows certain devices (e.g., disk drives) to transfer data directly to/from memory without CPU intervention, improving overall system performance by reducing CPU overhead.
2. Principles of I/O Software
- Goals of Interrupt Handlers: Handle interrupts generated by I/O devices to notify the CPU of events requiring attention, such as data arrival, completion of I/O operations, or error conditions.
- Device Drivers: Software components that provide an interface between the operating system and hardware devices, allowing the OS to communicate with and control I/O devices.
- Device-Independent I/O Software: Abstraction layers that provide a uniform interface for accessing I/O devices regardless of their specific hardware characteristics, enabling portability and ease of development.
- Secondary Storage Structure: Hierarchical organization of secondary storage devices (e.g., hard disks, SSDs) into logical units such as partitions, volumes, and file systems, managed by the operating system.
3. File Management
- Concept of File: A named collection of data stored on secondary storage, organized and managed by the operating system. Files can represent various types of data, including text documents, images, programs, etc.
- Access Methods: Techniques for accessing and reading/writing data from/to files, such as sequential access, random access, or direct access methods.
- File Types: Different types of files, such as regular files, directories (folders), device files, symbolic links, etc.
- File Operations: Common operations performed on files, including creation, opening, closing, reading, writing, deleting, renaming, and seeking.
- Directory Structure: Organization of files into hierarchical directories (folders) to facilitate organization, navigation, and management.
- File System Structure: Internal organization and layout of files, directories, and metadata on secondary storage devices, managed by the file system.
- Allocation Methods: Techniques for allocating storage space to files on disk, including contiguous allocation, linked allocation, and indexed allocation. Each method has its advantages and disadvantages in terms of space efficiency, fragmentation, and access speed.
- Free-Space Management: Mechanisms for managing and tracking available space on disk to facilitate efficient allocation of storage for new files and data.
4. Disk Management
- Disk Structure: Physical layout and organization of data on a disk, including sectors, tracks, and cylinders. Disks may be partitioned into logical units called partitions or volumes.
- Disk Scheduling: Algorithms used to optimize the order in which disk requests are serviced to minimize seek time and improve disk throughput. Examples include FCFS (First-Come, First-Served), SSTF (Shortest Seek Time First), SCAN, C-SCAN, LOOK, and C-LOOK.
- Disk Reliability: Measures taken to ensure data integrity and prevent data loss on disk, such as redundancy (RAID), error detection and correction codes, and disk mirroring.
- Disk Formatting: Process of preparing a disk for use by creating a file system, initializing data structures, and marking bad sectors.
- Boot-Block: Special disk sector containing boot loader code used to initiate the boot process when the computer is powered on.
- Bad Blocks: Defective sectors on a disk that cannot reliably store data. Bad blocks are identified during disk formatting and may be marked as unusable to prevent data corruption.
