Operating System Concepts: Memory Management, File Systems, and Processes

FILE ALLOCATION METHODS

Contiguous Allocation

  • Each file occupies a contiguous set of blocks on the disk.
  • Blocks assigned to a file are determined by the starting block address and file length.
  • The directory entry for a file with contiguous allocation contains the starting block address and allocated portion length.
  • Advantages: Supports sequential and direct accesses, and speed due to minimal seeks.
  • Drawbacks: Internal and external fragmentation, making it inefficient in memory utilization. Increasing file size is challenging due to the dependency on contiguous memory availability.

Linked List Allocation

  • Each file is a linked list of disk blocks, not contiguous.
  • Disk blocks can be scattered anywhere on the disk.
  • Directory entries contain pointers to the starting and ending file blocks.
  • Each block contains a pointer to the next block occupied by the file.
  • Advantages: Flexibility in file size and better memory utilization.
  • Drawbacks: Slower linked allocation due to random distribution of file blocks. Blocks cannot be directly accessed. Sequential access can be achieved by traversing blocks sequentially from the starting block.

Indexed Allocation

  • Stores pointers to all file-occupied blocks.
  • Multi-level index for each file in file-allocation table.
  • Indirection blocks introduced when total blocks exceed previous index allocation.

INTERNAL FRAGMENTATION

Internal fragmentation occurs in memory allocation when a process is allocated more memory than it actually needs. This results in wasted memory space within allocated blocks.

FILE ATTRIBUTES, TYPES, AND OPERATIONS

File

  • Represents the smallest allocation on logical secondary storage.
  • Essential for basic computer storage.

File Attributes

  • Name: The file’s name.
  • Identifier: A unique number identifying files within the file system.
  • Type: Specifies the file type, such as archive, source code, .docx, .txt.
  • Location: The file’s location on the device.
  • Size: The current and maximum file size.
  • Protection: Information about access control.
  • Time, Date, and User Identification: Details about the file’s creation, last modification, and user identification.

File Operations

  • Creating Files: Two steps are necessary to create a file. First, space in the file system must be found for the file. Second, an entry for the new file must be made in the directory.
  • Writing Files: To write to a file, a system call specifies the file name and data. The system locates the file and uses a write pointer to track and update the write location.
  • Reading a File: A system call specifies the file name and memory location for the data. The system locates the file and updates a read pointer to track the read location, often using a single pointer for both reading and writing.
  • Repositioning within a File: Repositioning, or file seek, updates the current file pointer to a specified value without actual I/O. The system searches the directory to find the file entry and adjusts the pointer accordingly.
  • Deleting a File: To delete a file, the system searches the directory for the named file. Having found the associated directory entry, it releases all file space so that it can be reused by other files and erases the directory entry.
  • Truncating a File: Truncating a file erases its contents but retains its attributes, resetting the file length to zero and releasing its space. This avoids the need to delete and recreate the file.

File Types

  • File types refer to the format of a file, which determines how the file is stored and displayed.

PROCESS AND PROCESS CONTROL UNIT

A process is a running instance of a program within an operating system. It represents the execution of a program’s instructions by the CPU and includes various elements such as program code, data, stack, registers, and a process control block (PCB).

  • Also called a task control block.
  • Contains information associated with a specific process, including these:
    • Process State: The state may be new, ready, running, waiting, or terminated.
    • Program Counter: Indicates the address of the next instruction to be executed.
    • CPU Registers: Accumulators, index registers, stack pointers, general-purpose registers, and condition code information.
    • CPU Scheduling Information: Process priority, pointers to scheduling queues, and other scheduling parameters.
    • Memory-Management Information: Includes values of base and limits registers, page tables, and segment tables.
    • Accounting Information: Information about the amount of CPU and real-time use, time limits, account number, and job or process number.
    • I/O Status Information: List of I/O devices allocated to the process and a list of open files.

VIRTUAL MEMORY

Virtual memory is a memory management technique used by operating systems to efficiently utilize physical memory (RAM) and extend available memory beyond physical limits. It creates an abstraction of memory, allowing processes to operate as if they have access to a large, contiguous address space, even if the physical memory is limited. Here are some advantages of virtual memory:

Advantages of Virtual Memory

  • Simplified Programming: Provides a uniform address space, simplifying development without considering physical memory constraints.
  • Demand Paging: Loads only active portions of programs into memory, reducing loading time and conserving memory.
  • Flexibility: Dynamically allocates and deallocates memory based on process demands, optimizing memory usage.

OPERATING SYSTEM

An operating system (OS) is software that acts as an intermediary between computer hardware and user applications, providing a platform for efficient and organized management of system resources. Here’s an explanation of its structure, functions, and objectives:

Structure

  • Kernel: Core component for managing hardware resources and essential services to applications.
  • Device Drivers: Software components for communication between the OS and hardware devices.
  • System Libraries: Collections of reusable code modules providing common functions and services to applications.
  • User Interface: Interface for users to interact with the OS and its applications.

Functions

  • Process Management: The OS handles processes—executing program instances—managing scheduling, creation, termination, and inter-process communication.
  • Memory Management: Manages system memory efficiently, allocates it to processes, and ensures they access required memory without interference.
  • File System Management: Manages files and directories on secondary storage like hard drives or SSDs, handling creation, deletion, organization, and access control.
  • Device Management: Controls and coordinates interactions between hardware devices and software applications.
  • Security and Protection: Ensures system security through access control, authentication, encryption, and other features, guarding against unauthorized access, breaches, and malware.

Objectives

  • Resource Management: Efficiently allocate and manage system resources.
  • Abstraction: Provides standardized interfaces for applications to interact with system resources.
  • Reliability: Ensures stable and reliable operation of the system.
  • Security: Implements measures to protect system resources, data, and user privacy.
  • User Interface: Provides user-friendly interfaces.
  • Scalability: Supports scalability by adapting to changing workload demands and user requirements.

NECESSARY CONDITIONS FOR DEADLOCK

A necessary condition for deadlock is the occurrence of the following four conditions simultaneously:

  1. Mutual Exclusion: At least one resource must be held in a non-sharable mode, meaning only one process can use it at a time.
  2. Hold and Wait: Processes hold allocated resources while waiting to acquire additional resources held by other processes.
  3. No Preemption: Resources cannot be forcibly taken away from a process; they must be released voluntarily by the process holding them.
  4. Circular Wait: There exists a circular chain of two or more processes, each waiting for a resource held by the next process in the chain.

Deadlock Handling Strategies

1. Deadlock Avoidance

  • Resource Allocation Graph: Employed to detect and prevent deadlocks by ensuring that the system never enters a state where the resource allocation graph contains a cycle.
  • Banker’s Algorithm: Ensures that resources are allocated safely by examining each resource request to ensure that it does not lead to unsafe conditions.

2. Deadlock Recovery

  • Process Termination: Identify and abort one or more processes involved in the deadlock to release their allocated resources.
  • Resource Preemption: Temporarily preempt resources from one or more processes to break the deadlock. The preempted resources are then reallocated to the remaining processes to allow progress.
  • Rollback: Roll back the state of the system to a previously consistent state, releasing resources and allowing processes to restart from that point.
  • Killing and Restarting: Kill all processes involved in the deadlock and restart them. This approach may be used if it is easier to restart processes than to identify and resolve the deadlock.

THREADING AND MULTI-THREADING

Threading refers to the capability of a program to execute multiple parts (or threads) simultaneously. Each thread represents a separate flow of execution within the same process. Threads share the same memory space and resources of the process, allowing them to communicate and interact efficiently.

Multithreading:

Multithreading is a programming concept where multiple threads are used within a single application or process to perform concurrent tasks. In a multithreaded environment, multiple threads run concurrently within the same process, sharing resources such as memory, file handles, and CPU time. Multithreading allows developers to design applications that can perform multiple tasks simultaneously, improving performance and responsiveness.

Importance of Multithreading

  1. Concurrency: Allows applications to perform multiple tasks simultaneously, enhancing performance.
  2. Resource Utilization: Maximizes CPU and system resource utilization, improving efficiency.
  3. Responsiveness: Prevents long-running tasks from blocking user interactions, ensuring smooth experiences.
  4. Scalability: Enables applications to handle increasing workloads efficiently.

EXTERNAL FRAGMENTATION

External fragmentation occurs in memory allocation when there are small blocks of unallocated memory scattered throughout the address space that are too small to be used for any process, yet too large to be combined into a single contiguous block.