os notes
IPC - Inter-Process Communication
Inter-Process Communication (IPC) enables communication between independent and cooperative processes running on an operating system, facilitating data sharing and resource management. IPC is crucial in multitasking and multi-user environments where processes need to synchronize their operations or exchange data efficiently.
Types of IPC:
Shared Memory:Shared memory allows multiple processes to access a common memory space. It is one of the fastest IPC methods since it provides direct access to the memory region where the data is stored. However, effective synchronization mechanisms (like semaphores) must be implemented to avoid race conditions and ensure orderly access to the memory.
Message Passing:In message passing, communication occurs through explicit messages sent between processes. This method is more structured than shared memory since it involves the sender and receiver defining a protocol for communication.
Messages can be:
Fixed size: Simplifying buffer management but potentially inefficient.
Variable size: More flexible but requires complex handling.
Communication can be:
Direct: Where processes send messages directly to each other.
Indirect: Where messages are sent to shared mailbox-like structures, decoupling sender and receiver.
Synchronous: Where the sender waits for the receiver to acknowledge receipt.
Asynchronous: Where the sender continues its execution without waiting for acknowledgment.
Memory Access and Process Synchronization
Direct communication in an operating system is only viable if a process has access to the shared memory address. This access must be controlled to prevent data inconsistencies and ensure data integrity.
Example: Producer-Consumer Problem
In the Producer-Consumer Problem, producer processes generate items and add them to a buffer, while consumer processes retrieve items from this buffer. Proper synchronization is critical to manage access to the shared buffer effectively:
Only one process can access the shared memory at one time, which prevents conflicts.
Appropriate signaling between producers and consumers must take place to ensure smooth operation without overfilling or emptying the buffer.
Buffer Types:
Bounded Buffer:
Has a fixed size. If the buffer is full, the producer must wait. If it is empty, the consumer must wait.
Designed to prevent resource exhaustion by limiting usage.
Unbounded Buffer:
Has no fixed limit to the buffer size. Producers can continue producing as long as the consumer processes the items in a timely manner.
Suitable for environments where resource availability is not strictly limited.
Threads and Processes
Threads are lightweight processes that can run independently within the same application, sharing resources but maintaining separate execution contexts, like stacks and registers.
A single-threaded process has one thread of execution, while a multi-threaded process contains multiple threads, allowing for parallel execution of tasks within an application.
Concurrency: Multiple processes are executed simultaneously, predominantly on a single CPU, which alternates execution between them.
Parallelism: Independent processes executing simultaneously across multiple CPUs, enabling better utilization of modern multi-core processors.
Thread Types:
User Level Threads:
Managed by user-level libraries, independent of the OS, which offers fast context switching.
Kernel Level Threads:
Managed by the OS kernel, enabling true multi-threading capabilities with better performance in multitasking environments.
Models of Multi-threading:
Many-to-One Model:
Many user-level threads are mapped to a single kernel thread, not ideal for multi-threading.
One-to-Many Model:
One kernel thread is mapped to many user threads, providing better performance and concurrency.
Many-to-Many Model:
Multiple user threads are allowed to run on multiple kernel threads, effectively utilizing system resources.
Advantages of Multi-threading:
Enhances responsiveness, allowing applications to remain active while performing background tasks.
Facilitates resource sharing and improves efficiency in processing.
Optimizes the utilization of multi-processor architectures, making it ideal for modern computing environments.
Threads typically consume fewer resources and require less time for creation and context switching compared to processes.
Process Synchronization
Synchronization is vital when multiple processes access shared data, ensuring correct outcomes and consistency.
Race Condition: Occurs when multiple processes access shared data simultaneously in an uncontrolled manner, potentially leading to unexpected results.
Critical Section: This part of the code accesses shared resources; proper locking mechanisms must ensure only one process executes in this section at any time.
Concurrency Issues:
Issues arising from concurrency include:
Deadlocks: Occur when multiple processes block each other, waiting for resources that are held by one another, leading to system standstill.
Proper synchronization is critical to prevent inconsistent data access, ensuring system stability.
Deadlock Conditions:
Conditions that must be met for a deadlock to occur include:
Mutual Exclusion: Resources are allocated in a way that only one process can use a resource at a time.
Hold and Wait: Processes holding resources are waiting for others, which creates a cycle of dependency.
No Preemption: Resources cannot be forcibly taken away; they can only be released voluntarily.
Circular Wait: A situation in which each process in a circular chain is waiting for a resource held by the next process in the chain.
Deadlock Solutions:
Methods for handling deadlocks include:
Prevention: Designing systems to eliminate the possibility of deadlock conditions through structuring of resource acquisition and allocation.
Avoidance: Dynamically assessing resource allocation requests to ensure they do not lead to deadlock situations.
Detection: Allowing deadlocks to occur but employing mechanisms to detect and recover from them effectively.
Resource Allocation Graph (RAG):
Utilized in deadlock detection. It visually represents the allocation of resources to processes, with edges indicating resource requests and allocations, helping identify deadlock conditions.
File Management
Files are organized in directories, each associated with inode numbers that uniquely identify them within the filesystem. Key operations include:
Create: Making a new file.
Open: Accessing an existing file.
Read: Retrieving data from a file.
Write: Storing data into a file.
Seek: Navigating within a file.
File Allocation Methods Include:
Contiguous Allocation: Placing files in contiguous blocks of storage for fast access but can lead to fragmentation.
Linked Allocation: Using pointers to link blocks of storage scattered across disk; flexible but slower due to pointer navigation.
Indexed Allocation: Maintaining an index block that contains addresses of all file blocks, improving management and access times.
Disk Scheduling Algorithms
Various algorithms manage disk I/O requests to optimize performance:
FCFS (First Come First Serve): Processes requests in the order they arrive, which can lead to long wait times.
SSTF (Shortest Seek Time First): Prioritizes requests that are closest to the current head position, reducing average wait time.
SCAN (Elevator Algorithm): Moves the disk arm in one direction, handling all requests before reversing direction.
C-SCAN (Circular SCAN): Similar to SCAN but returns to the beginning after reaching the end, providing a more uniform wait time.
LOOK and C-LOOK: Variations on SCAN strategies that prioritize minimizing unnecessary movement of the read/write heads.
Device Management
Manages I/O devices through techniques like:
Polling: The CPU repeatedly checks the status of an I/O device.
Interrupts: Devices send alerts to the CPU when they are ready for processing, allowing smoother multitasking.
Monitors: Aid in process synchronization by ensuring only authorized processes access specific resources at any given time.
Superblock and Inode Structure:
Superblock: Contains metadata about a filesystem including its total size, usage, and location of inode tables.
Inodes: Store essential file attributes (permissions, timestamps) and metadata required for file management, enabling effective access control and organization of files within a filesystem.