Operating System Mca
Operating System Mca
Clustered System
Cluster systems are similar to parallel systems because both systems use multiple CPUs. The
primary difference is that clustered systems are made up of two or more independent systems
linked together. They have independent computer systems and a shared storage media, and all
systems work together to complete all tasks. All cluster nodes use two different approaches to
interact with one another, like message passing interface (MPI) and parallel virtual machine
(PVM).
1
Cluster operating systems are a combination of software and hardware clusters. Hardware
clusters aid in the sharing of highperformance disks among all computer systems, while
software clusters give a better environment for all systems to operate. A cluster system
consists of various nodes, each of which contains its cluster software.
There are two clusters available to make a more efficient cluster. These are as follows:
• Software Cluster
• Hardware Cluster
Software Cluster
The Software Clusters allows all the systems to work together.
Hardware Cluster
It helps to allow highperformance disk sharing among systems.
Types of Clustered Operating System
There are mainly three types of the clustered operating system:
• Asymmetric Clustering System
• Symmetric Clustering System
• Parallel Cluster System
2
Symmetric Clustering System
Multiple nodes help run all applications in this system, and it monitors all nodes
simultaneously. Because it uses all hardware resources, this cluster system is more reliable
than asymmetric cluster systems.
Parallel Cluster System
A parallel cluster system enables several users to access similar data on the same shared
storage system. The system is made possible by a particular software version and other apps.
Advantages of Clustered os:
• High Availability
• Scalability
• Load Balancing
• Fault Tolerance
• Improved Performance
• Cost Efficiency
Simple/Monolithic structure
Such operating systems do not have welldefined structures and are small, simple, and limited.
The interfaces and levels of functionality are not well separated. MSDOS is an example of
such an operating system.
3
MicroKernel Structure
This structure designs the operating system by removing all nonessential components from
the kernel and implementing them as system and user programs. This results in a smaller
kernel called the microkernel. Advantages of this structure are that all new services need to be
added to user space and does not require the kernel to be modified. Thus it is more secure and
reliable as if a service fails, then rest of the operating system remains untouched. Mac OS is
an example of this type of OS.
HybridKernel Structure
Hybridkernel structure is nothing but just a combination of both monolithickernel structure
and microkernel structure. Basically, it combines properties of both monolithic and
microkernel and make a more advance and helpful approach. It implement speed and design
of monolithic and modularity and stability of microkernel structure.
Advantages of HybridKernel Structure
• It offers good performance as it implements the advantages of both structure in it.
• It supports a wide range of hardware and applications.
ExoKernel Structure
Exokernel is an operating system developed at MIT to provide applicationlevel management
of hardware resources. By separating resource management from protection, the exokernel
architecture aims to enable applicationspecific customization. Due to its limited operability,
exokernel size typically tends to be minimal.
Advantages of ExoKernel
• Support for improved application control.
• Separates management from security.
• It improves the performance of the application.
4
Layered Structure
An OS can be broken into pieces and retain much more control over the system. In this
structure, the OS is broken into a number of layers (levels). The bottom layer (layer 0) is the
hardware, and the topmost layer (layer N) is the user interface. These layers are so designed
that each layer uses the functions of the lowerlevel layers. This simplifies the debugging
process, if lowerlevel layers are debugged and an error occurs during debugging, then the
error must be on that layer only, as the lowerlevel layers have already been debugged.
Modular Structure
It is considered as the best approach for an OS. It involves designing of a modular kernel.
The kernel has only a set of core components and other services are added as dynamically
loadable modules to the kernel either during runtime or boot time. It resembles layered
structure due to the fact that each kernel has defined and protected interfaces, but it is more
flexible than a layered structure as a module can call any other module.
5
The major operations of the operating system are process management, memory
management, device management and file management.
Process Management
The operating system is responsible for managing the processes i.e assigning the processor to
a process at a time. This is known as process scheduling. The different algorithms used for
process scheduling are FCFS (first come first served), SJF (shortest job first), priority
scheduling, round robin scheduling etc.
There are many scheduling queues that are used to handle processes in process management.
When the processes enter the system, they are put into the job queue. The processes that are
ready to execute in the main memory are kept in the ready queue. The processes that are
waiting for the I/O device are kept in the device queue.
Memory Management
Memory management plays an important part in operating system. It deals with memory and
the moving of processes from disk to primary memory for execution and back again.
The activities performed by the operating system for memory management are −
The operating system assigns memory to the processes as required. This can be done using
best fit, first fit and worst fit algorithms.
All the memory is tracked by the operating system
Device Management
There are many I/O devices handled by the operating system such as mouse, keyboard, disk
drive etc. There are different device drivers that can be connected to the operating system to
handle a specific device. The device controller is an interface between the device and the
device driver. The user applications can access all the I/O devices using the device drivers,
which are device specific codes.
6
File Management
Files are used to provide a uniform view of data storage by the operating system. All the files
are mapped onto physical devices that are usually non volatile so data is safe in the case of
system failure.
The files can be accessed by the system in two ways i.e. sequential access and direct access
Sequential Access
The information in a file is processed in order using sequential access. The files records are
accessed on after another. Most of the file systems such as editors, compilers etc. use
sequential access.
Direct Access
In direct access or relative access, the files can be accessed in random for read and write
operations. The direct access model is based on the disk model of a file, since it allows
random accesses.
Process Management
Process management is a key part of an operating system. It controls how processes are
carried out, and controls how your computer runs by handling the active processes.
The OS is responsible for managing the start, stop, and scheduling of processes, which are
programs running on the system. The operating system uses a number of methods to prevent
deadlocks, facilitate interprocess communication, and synchronize processes.
A process in memory is divided into several distinct sections, each serving a different
purpose. Here’s how a process typically looks in memory:
Text Section: A Process, sometimes known as the Text Section, also includes the current
activity represented by the value of the Program Counter.
Stack: The stack contains temporary data, such as function parameters, returns addresses, and
local variables.
Data Section: Contains the global variable.
7
Heap Section: Dynamically memory allocated to process during its run time.
Characteristics of a Process:
A process has the following attributes.
Process Id: A unique identifier assigned by the operating system.
Process State: Can be ready, running, etc.
CPU Registers: Like the Program Counter (CPU registers must be saved and restored when a
process is swapped in and out of the CPU)
Accounts Information: Amount of CPU used for process execution, time limits, execution ID,
etcI/O Status Information: For example, devices allocated to the process, open files, etc
States of Process
A process is in one of the following states:
New: Newly Created Process (or) beingcreated process.
Ready: After the creation process moves to the Ready state, i.e. the process is ready for
execution.
Running: Currently running process in CPU (only one process at a time can be under
execution in a single processor).
Wait (or Block): When a process requests I/O access.
Complete (or Terminated): The process completed its execution.
Suspended Ready: When the ready queue becomes full, some processes are moved to a
suspended ready state
Suspended Block: When the waiting queue becomes full.
8
o External Fragmentation
o Paging
o
Why Memory Management is Required?
Allocate and deallocate memory before and after process execution.
To keep track of used memory space by processes.
To minimize fragmentation issues.
To proper utilization of main memory.
To maintain data integrity while executing of process.
Logical and Physical Address Space
Logical Address Space: An address generated by the CPU is known as a “Logical Address”. It
is also known as a Virtual address. Logical address space can be defined as the size of the
process. A logical address can be changed.
Physical Address Space: An address seen by the memory unit (i.e the one loaded into the
memory address register of the memory) is commonly known as a “Physical Address”. A
Physical address is also known as a Real address.
Static and Dynamic Loading
Loading a process into the main memory is done by a loader. There are two different types of
loading :
Static Loading: Static Loading is basically loading the entire program into a fixed address. It
requires more memory space.
Dynamic Loading: The entire program and all data of a process must be in physical memory
for the process to execute. So, the size of a process is limited to the size of physical memory.
Swapping
When a process is executed it must have resided in memory. Swapping is a process of
swapping a process temporarily into a secondary memory from the main memory, which is
fast compared to secondary memory. A swapping allows more processes to be run and can be
fit into memory at one time. The main part of swapping is transferred time and the total time
is directly proportional to the amount of memory swapped. Swapping is also known as
rollout, or roll because if a higher priority process arrives and wants service, the memory
manager can swap out the lower priority process and then load and execute the higher priority
process.
9
Storage Management
Storage Management is defined as it refers to the management of the data storage
equipment’s that are used to store the user/computer generated data. Hence it is a tool or set
of processes used by an administrator to keep your data and storage equipment’s safe.
Storage management key attributes:
1. Performance
2. Reliability
3. Recoverability
4. Capacity
Feature of Storage management: There is some feature of storage management which is
provided for storage capacity. These are given below:
Storage management is a process that is used to optimize the use of storage devices.
Storage management must be allocated and managed as a resource in order to truly benefit a
corporation.
Storage management is generally a basic system component of information systems.
It is used to improve the performance of their data storage resources.
Advantage of storage management:
• It becomes very simple to manage a storage capacity.
• It generally reduces the time consumption.
• It improves the performance of system.
10
• In virtualization and automation technologies, it can help an organization improve its
agility.
11
These type of attacks do not allow the legitimate users to access a system. It overwhelms the
system with requests so it is overwhelmed and cannot work properly for other user.
Protection and Security Methods
The different methods that may provide protect and security for different computer systems
are −
Authentication
This deals with identifying each user in the system and making sure they are who they claim
to be. The operating system makes sure that all the users are authenticated before they access
the system. The different ways to make sure that the users are authentic are:
Username/ Password
Each user has a distinct username and password combination and they need to enter it
correctly before they can access the system.
User Key/ User Card
The users need to punch a card into the card slot or use they individual key on a keypad to
access the system.
User Attribute Identification
Different user attribute identifications that can be used are fingerprint, eye retina etc. These
are unique for each user and are compared with the existing samples in the database. The user
can only access the system if there is a match.
One Time Password
These passwords provide a lot of security for authentication purposes. A one time password
can be generated exclusively for a login every time a user wants to enter the system. It cannot
be used more than once. The various ways a one time password can be implemented are −
Random Numbers
The system can ask for numbers that correspond to alphabets that are pre arranged. This
combination can be changed each time a login is required.
Secret Key
A hardware device can create a secret key related to the user id for login. This key can change
each time.
Computing Environments
Computing environments refer to the technology infrastructure and software platforms that
are used to develop, test, deploy, and run software applications. There are several types of
computing environments, including:
Mainframe: A large and powerful computer system used for critical applications and
largescale data processing.
12
ClientServer: A computing environment in which client devices access resources and services
from a central server.
Cloud Computing: A computing environment in which resources and services are provided
over the Internet and accessed through a web browser or client software.
Mobile Computing: A computing environment in which users access information and
applications using handheld devices such as smartphones and tablets.
Grid Computing: A computing environment in which resources and services are shared across
multiple computers to perform largescale computations.
Embedded Systems: A computing environment in which software is integrated into devices
and products, often with limited processing power and memory.
Each type of computing environment has its own advantages
Types of Computing Environments :
Personal Computing Environment: Individual computers used by single users for personal
tasks.
TimeSharing Computing Environment: Multiple users share system resources simultaneously,
typically through terminals.
ClientServer Computing Environment: Clients request services, and servers provide them,
often over a network.
Distributed Computing Environment: Multiple interconnected computers share processing
tasks.
Grid Computing Environment: Multiple computers work together to solve large tasks by
dividing workloads.
13
Cloud Computing Environment: Internetbased services providing ondemand resources and
storage.
Cluster Computing Environment: Group of linked computers that work together, acting as a
single system.
ReactOS
Current Status:
• Development Stage: Alpha version, with ongoing efforts to improve stability and
compatibility.
• Compatibility: Focus on running Windows applications and drivers.
Operating systems (OS) provide a range of essential services to ensure the proper functioning
of computer systems and the smooth execution of applications. These services act as
intermediaries between hardware and software, offering functionalities that simplify user and
application interaction with the system.
1. Process Management
This service handles the creation, scheduling, and termination of processes (running
programs). It ensures that multiple processes can run efficiently and that system resources
(CPU, memory) are properly allocated. Key components include:
Process Scheduling: Determines the order in which processes are executed by the CPU.
2. Memory Management
Memory management involves handling the allocation and deallocation of memory space for
processes. It ensures efficient memory use and prevents issues like memory leaks or
segmentation faults. The OS uses techniques such as:
Virtual Memory: Extends physical memory by using disk space, allowing for larger programs
to run.
15
This service provides a structured way to store, retrieve, and manage data on storage devices
(hard drives, SSDs). The OS organizes data into files and directories, ensuring secure and
efficient data access. Key aspects include:
File Access Control: Manages permissions to control who can read, write, or execute files.
File Organization: Stores files in directories (or folders) and provides a hierarchical structure
for easier navigation.
Storage Management: Manages how files are physically stored on disk and ensures efficient
use of storage space.
4. Device Management
Device management handles communication between the system and hardware peripherals
(e.g., printers, scanners, keyboards). It uses device drivers, which are specialized programs
that allow the OS to interact with hardware. Key tasks include:
Device Drivers: Provides abstraction for hardware to ensure compatibility and proper
functioning of devices.
Buffering and Spooling: Buffers data to smooth out differences in device speeds and spools
output (e.g., printing jobs) for efficient processing.
The OS ensures the security of data, processes, and the system itself by protecting against
unauthorized access and ensuring data integrity. Key mechanisms include:
Encryption: Protects data by encoding it, making it unreadable without the correct decryption
key.
Audit Logs: Tracks and records user activities to detect and investigate security incidents.
16
System Call
A system call is a programmatic way in which a computer program requests a service from
the kernel of the operating system it is executed on. A system call is a way for programs to
interact with the operating system. A computer program makes a system call when it requests
the operating system’s kernel.
System call provides the services of the operating system to the user programs via the
Application Program Interface(API). It provides an interface between a process and an
operating system to allow userlevel processes to request services of the operating system.
System calls are the only entry points into the kernel system. All programs needing resources
must use system calls.
Services Provided by System Calls
• Process Creation and Management
• Main Memory Management
• File Access, Directory, and File System Management
• Device Handling(I/O)
• Protection
• Networking, etc.
Process Control: end, abort, create, terminate, allocate, and free memory.
File Management: create, open, close, delete, read files, etc.
Device Management
Information Maintenance
Communication
17
System calls are how programs request services from the operating system. They are grouped
into five main types:
1. Process Control
Manages process creation, execution, and termination.
fork(): Creates a new process by duplicating the calling process.
exec(): Replaces the current process image with a new process.
exit(): Terminates the current process and returns a status code.
wait(): Waits for a child process to finish execution.
kill(): Sends a signal to terminate or manage another process.
2. File Management
Handles file operations like opening, reading, writing, and closing files.
open(): Opens a file for reading, writing, or both.
read(): Reads data from a file into a buffer.
write(): Writes data from a buffer to a file.
close(): Closes an open file.
unlink(): Deletes a file.
3. Device Management
Manages communication with hardware devices.
ioctl(): Performs lowlevel devicespecific operations.
read(): Reads data from a device (same system call used for reading files).
write(): Writes data to a device (same system call used for writing to files).
4. Information Maintenance
18
Retrieves or sets system and process information.
getpid(): Retrieves the process ID of the current process.
getppid(): Retrieves the parent process ID.
getuid(): Retrieves the user ID of the calling process.
setuid(): Sets the user ID of the calling process.
times(): Returns the amount of CPU time used by the process.5. Communication
Communication
• Enables interprocess communication (IPC) between processes.
• pipe(): Creates a unidirectional data channel for IPC between related processes.
• shmget(): Allocates shared memory for processes.
• mmap(): Maps files or devices into memory for fast communication.
• msgsend(): Sends a message to a message queue.
System Programs
The system programs are responsible for the development and execution of a program and
they can be used by the help of system calls because system calls define different types of
system programs for different tasks.
File management − These programs create, delete, copy, rename, print, exit and generally
manipulate the files and directory.
Status information − It is the information regarding input, output process, storage and the
CPU utilization time how the process will be calculated in how much memory required to
perform a task is known by status information.
Programming language supports − compiler, assembler, interrupt are programming language
support used in the operating system for a particular purpose in the computer.
Programming Loading and execution − The needs to enter the program and after the loading
of a program it needs to execute the output of the programs and this task is also performed by
system calls by the help of system programs.
Communication − These services are provided by the user because by using this number of
devices communicates with each other by the help of device or wireless and communication
is necessary for the operating system.
Types of System programs
The types of system programs are as follows −
Utility program
It manages, maintains and controls various computer resources. Utility programs are
comparatively technical and are targeted for the users with solid technical knowledge.
Few examples of utility programs are: antivirus software, backup software and disk tools.
Device drivers
19
It controls the particular device connected to a computer system. Device drivers basically act
as a translator between the operating system and device connected to the system.
Example − printer driver, scanner driver, storage device driver etc.
Directory reporting tools
These tools are required in an operation system to have some software to facilitate the
navigation through the computer system.
Example − dir, ls, Windows Explorer etc.
System Boot
System boot is the process of starting a computer and loading the operating system (OS) into
memory so that it becomes operational. It involves several steps, beginning from when the
power is turned on to when the OS is fully loaded and ready for use.
Steps in the System Boot Process:
1. Power On and POST (PowerOn Self Test)
When the computer is powered on, the hardware performs a POST to check if all critical
hardware components (CPU, memory, keyboard, etc.) are functioning correctly. If a hardware
issue is detected, the boot process is halted, and an error message or beep code is provided.
2. Loading the Bootloader
After POST, the system's firmware (BIOS or UEFI) looks for the bootloader, a small program
stored on the disk's boot sector (Master Boot Record MBR or GPT). The bootloader’s job is
to load the operating system into memory.
BIOS (Basic Input/Output System): Older firmware used in many PCs.
UEFI (Unified Extensible Firmware Interface): A more modern alternative with better
features, like faster boot times and secure boot options.
3. Bootloader Execution
The bootloader (e.g., GRUB for Linux or Windows Boot Manager) is responsible for loading
the kernel of the OS into memory. It can also provide a boot menu to select different
operating systems if multiple OSes are installed.
4. Loading the OS Kernel
The bootloader locates and loads the OS kernel (the core part of the operating system) into
memory. The kernel takes over system management from the bootloader and initializes the
hardware components.
5. Hardware and Device Initialization
20
The kernel initializes the system’s hardware, including CPUs, memory, and input/output
devices (like keyboards, monitors, and storage drives). Device drivers are loaded to facilitate
communication with the hardware.
6. Starting System Services
The kernel starts essential system services and daemons (background processes), such as
networking services, security services, and other components required for the system to
function properly.
7. User Login and Shell
Once the system services are running, the system prompts the user to log in (via a login
screen or terminal). After logging in, the user interface (graphical or commandline) is
launched, allowing the user to interact with the operating system.
UNIT2
Process Concept
In an operating system (OS), a process is an instance of a program that is being executed. It is
a fundamental concept, as the OS manages multiple processes simultaneously, ensuring
efficient use of system resources like the CPU, memory, and I/O devices. Each process has its
own execution context, which includes its program code, data, and the state of the CPU
registers.
Process Scheduling
he process scheduling is the activity of the process manager that handles the removal of the
running process from the CPU and the selection of another process on the basis of a particular
strategy.
Process scheduling is an essential part of a Multiprogramming operating systems. Such
operating systems allow more than one process to be loaded into the executable memory at a
time and the loaded process shares the CPU using time multiplexing.
Categories of Scheduling
Nonpreemptive: Here the resource can’t be taken from a process until the process completes
execution. The switching of resources occurs when the running process terminates and moves
to a waiting state.
Preemptive: Here the OS allocates the resources to a process for a fixed amount of time.
During resource allocation, the process switches from running state to ready state or from
waiting state to ready state.
Process Scheduling Queues
Job queue − This queue keeps all the processes in the system.
Ready queue − This queue keeps a set of all processes residing in main memory, ready and
waiting to execute. A new process is always put in this queue.
21
Device queues − The processes which are blocked due to unavailability of an I/O device
constitute this queue.
Operations on Processess
Creation: Starting a new process, typically using system calls like fork() or CreateProcess().
Scheduling: Deciding which process runs and for how long using algorithms like Round
Robin or FCFS.
Termination: Ending a process, either normally or due to errors.
Context Switching: Saving and loading process states when switching between processes.
Synchronization: Managing processes to prevent conflicts using tools like semaphores or
mutexes.
Communication: Exchanging data between processes via mechanisms like pipes or message
queues.
Waiting: Processes waiting for events, like I/O completion, and moving through different
states (e.g., ready, running).
Process Control Block (PCB): A data structure storing information about a process, including
its state and resources.
Process States: Various stages a process goes through, including New, Ready, Running,
Waiting, and Terminated.
22
Types of Process
• Independent process
• Cooperating process
Independent Process: A process that operates on its own without relying on or affecting other
processes. It does not share data or resources with other processes.
Cooperating Process: A process that interacts with other processes, sharing data and
resources. It may need synchronization to avoid conflicts and ensure smooth operation.
The communication between these processes can be seen as a method of cooperation between
them. Processes can communicate with each other through both:
Methods of IPC
• Shared Memory
• Message Passing
1. Shared Memory: Multiple processes access and modify a common memory space directly.
Requires synchronization to avoid conflicts.
2. Message Passing: Processes communicate by sending and receiving messages through a
communication channel, which avoids direct access to shared memory.
Sockets Mechanism: Uses network sockets to establish a connection between client and
server for data exchange.
23
Remote Procedure Call (RPC): Allows a client to invoke procedures or functions on a remote
server as if they were local.
Message Passing: Involves sending and receiving messages between client and server to
exchange data.
24
Distributed File Systems: Allows clients to access and manage files on a remote server as if
they were on their local system.
MultiThreads
User Level User Level Thread is a type of thread that is not created using system calls. The
kernel has no work in the management of userlevel threads. Userlevel threads can be easily
implemented by the user.
Kernel Level Thread A kernel Level Thread is a type of thread that can recognize the
Operating system easily. Kernel Level Threads has its own thread table where it keeps track
of the system. The operating System Kernel helps in managing threads.
Hybrid Threads: Combines both userlevel and kernellevel threading. User threads are
managed by a userlevel library, but the kernel manages scheduling and resources for those
threads.
ManytoOne: Maps many userlevel threads to a single kernel thread. Thread management is
done in user space, and the kernel schedules one thread at a time.
OnetoOne: Maps each userlevel thread to a separate kernel thread. Allows multiple threads to
run in parallel, managed directly by the kernel.
25
ManytoMany: Maps many userlevel threads to many kernel threads. Provides flexibility and
allows multiple threads to run concurrently, with both user and kernel managing threads.
Scheduling Criteria
Scheduling criteria help determine the efficiency and performance of process scheduling in
operating systems. Key criteria include:
CPU Utilization: The percentage of time the CPU is active. Higher CPU utilization means the
CPU is used more efficiently.
Throughput: The number of processes completed per unit of time. Higher throughput
indicates better performance.
Turnaround Time: The total time taken from process submission to completion. Lower
turnaround time is preferable.
Waiting Time: The total time a process spends waiting in the ready queue. Lower waiting
time improves responsiveness.
Response Time: The time from process submission to the first response or output. Shorter
response times enhance interactivity.
Fairness: Ensures that all processes get a fair share of CPU time without indefinite
postponement.
Scheduling algorithms in os
• FirstCome, FirstServed (FCFS) Scheduling
• ShortestJobNext (SJN) Scheduling
• Priority Scheduling
• Shortest Remaining Time
• Round Robin(RR) Scheduling
• MultipleLevel Queues Scheduling
26
scheduler may preempt a low priority running process anytime when a high priority process
enters into a ready state.
P0 0 0=0
P1 5 1=4
P2 8 2=6
P3 16 3 = 13
27
• Best approach to minimize waiting time.
• Easy to implement in Batch systems where required CPU time is known in advance.
• Impossible to implement in interactive systems where required CPU time is not known.
• The processer should know in advance how much time process will take.
P0 0 5 0
P1 1 3 5
P2 2 8 14
P3 3 6 8
P0 0 0=0
P1 5 1=4
P2 14 2 = 12
28
P3 8 3=5
Given: Table of processes, and their Arrival time, Execution time, and priority. Here we are
considering 1 is the lowest priority.
P0 0 5 1 0
P1 1 3 2 11
P2 2 8 1 14
P3 3 6 3 5
29
Waiting time of each process is as follows −
P0 0 0=0
P1 11 1 = 10
P2 14 2 = 12
P3 5 3=2
• Shortest remaining time (SRT) is the preemptive version of the SJN algorithm.
• The processor is allocated to the job closest to completion but it can be preempted by a
newer ready job with shorter time to completion.
• Impossible to implement in interactive systems where required CPU time is not known.
• It is often used in batch environments where short jobs need to give preference.
30
Process Wait Time : Service Time Arrival Time
P0 (0 0) + (12 3) = 9
P1 (3 1) = 2
P3 (9 3) + (17 12) = 11
Multiplelevel queues are not an independent scheduling algorithm. They make use of other
existing algorithms to group and schedule jobs with common characteristics.
For example, CPUbound jobs can be scheduled in one queue and all I/Obound jobs in another
queue. The Process Scheduler then alternately selects jobs from each queue and assigns them
to the CPU based on the algorithm assigned to the queue.
Process Synchronization
Process Synchronization is the coordination of execution of multiple processes in a
multiprocess system to ensure that they access shared resources in a controlled and
predictable manner. It aims to resolve the problem of race conditions and other
synchronization issues in a concurrent system.
Process synchronization ensures that multiple processes or threads operate smoothly and
without conflict when accessing shared resources. Key concepts include:
Critical Section: A segment of code where shared resources are accessed. Only one process
should be in the critical section at a time to prevent conflicts.
Mutual Exclusion: Ensures that only one process can access the critical section at any given
time.
Semaphore: A synchronization tool that controls access to shared resources. Semaphores can
be binary (0 or 1) or counting, allowing multiple units of a resource.
31
Mutex (Mutual Exclusion): A type of semaphore used to enforce mutual exclusion, ensuring
only one thread or process can access a critical section at a time.
Monitor: An abstraction that provides a highlevel synchronization mechanism. Monitors
combine mutual exclusion and condition synchronization.
Condition Variable: Used with monitors to allow processes to wait for certain conditions to be
met before proceeding.
Deadlock: A situation where two or more processes are waiting indefinitely for each other to
release resources. Deadlock prevention and avoidance strategies help mitigate this issue.
Starvation: A situation where a process is perpetually denied necessary resources due to other
processes continuously being favored. Fairness and proper scheduling help prevent
starvation.
32
Bounded Waiting: There must be a limit on how long a process will have to wait before it can
enter the critical section. This prevents starvation, where a process waits forever due to others
continually entering the critical section.
Solutions:
Peterson's Solution: A softwarebased solution using two flags and a turn variable to ensure
mutual exclusion and progress.
Semaphore: A synchronization primitive that can be used to control access to the critical
section. Binary semaphores (mutexes) are used for mutual exclusion.
Mutex (Mutual Exclusion): A lock that ensures only one thread or process can access the
critical section at a time.
Monitor: A highlevel abstraction that combines mutual exclusion and condition
synchronization, allowing processes to wait for conditions to be met.
TestandSet Lock: A hardwarebased solution that uses atomic operations to achieve mutual
exclusion.
Peterson's solution
Peterson's solution is a method for ensuring that two processes can safely access a shared
resource without interfering with each other. It ensures that only one process can access the
critical section (the part of the code where shared resources are accessed) at a time.
Components
Flags: Each process has a flag to show whether it wants to enter the critical section.
Process 0 has a flag to indicate if it wants to enter.
Process 1 has a flag to indicate if it wants to enter.
Turn: A shared variable that indicates whose turn it is to enter the critical section.
33
How It Works
Desire to Enter: Each process sets its flag to show that it wants to enter the critical section.
Priority: Each process sets the turn variable to indicate that it is giving priority to the other
process. For example, Process 0 sets the turn variable to indicate that it is Process 1's turn,
and vice versa.
Waiting: Before entering the critical section, each process checks two things:
Whether the other process has indicated that it wants to enter the critical section (its flag is
set).
Whether it is the other process's turn to enter (based on the turn variable).
If the other process wants to enter and it is that process's turn, the current process will wait.
Entering: If the other process does not want to enter or it is not that process’s turn, then the
current process will enter the critical section.
Leaving: When the process finishes its work in the critical section, it clears its flag, indicating
that it no longer needs to enter the critical section.
Key Points
Mutual Exclusion: Ensures that only one process can be in the critical section at any time.
This is managed through the turn variable and the flags.
Progress: Ensures that if no process is in the critical section and one or both processes want to
enter, the process with the turn (or if both want, whoever is allowed by the turn and flags)
will get access.
Bounded Waiting: Ensures that no process will wait forever to enter the critical section. Each
process will eventually get a chance based on the turn variable.
Synchronization Hardware
1.Atomic Instructions:
Atomic instructions are operations that execute as a single, indivisible unit. This means they
complete without being interrupted by other processes or threads, ensuring consistency when
modifying shared resources. Key atomic instructions include:
TestandSet: This instruction reads a value from a memory location and sets it to a new value
in one atomic operation. It's commonly used to implement spinlocks. For instance, if a
process wants to acquire a lock, it can test if the lock is free and set it to "locked" atomically.
CompareandSwap (CAS): This instruction compares the current value of a memory location
with a given value. If they match, it updates the memory location with a new value. This is
34
useful for implementing locks and other synchronization primitives, ensuring that updates are
made only if no other process has modified the value in the meantime.
FetchandAdd: This instruction atomically adds a given value to a memory location and
returns the old value. It’s useful for counting or managing resources where multiple processes
or threads might be incrementing or decrementing a shared counter.
2. Locks
Locks are synchronization mechanisms to ensure mutual exclusion and avoid concurrent
access issues. Hardwaresupported locks include:
Spinlocks: These are simple locks where a process continuously checks if the lock is
available. Spinlocks are efficient for short critical sections but can waste CPU cycles if the
lock is held for a long time.
Mutexes: Short for "mutual exclusion," mutexes are more sophisticated than spinlocks. They
can block a process if the lock is not available and wake it up when the lock becomes
available. Mutexes are implemented using atomic operations and can support complex
locking strategies, such as recursive or timed locks.
3. Memory Barriers (Fences)
Memory barriers or memory fences are special instructions that enforce an ordering of
memory operations. They prevent the CPU from reordering read and write operations in ways
that could lead to race conditions or inconsistent views of memory. Memory barriers ensure
that:
Writes to a memory location are visible to other threads or processes in the intended order.
Reads and writes occur in a specific sequence as defined by the barrier.
4. Interrupt Disabling
Interrupt disabling is a technique used to prevent context switches by disabling interrupts
during critical sections. This ensures that a process or thread can execute a critical section
without being interrupted by other processes or threads, maintaining consistency. While
effective, it can be inefficient as it may lead to poor responsiveness in systems that rely
heavily on interrupts.
5. Atomic Variables
Atomic variables are special types of variables that support atomic operations. These
variables are designed to be used in concurrent programming and can be manipulated safely
by multiple threads without requiring additional synchronization mechanisms. Examples
include:
Atomic Counters: Used to count events or manage resources safely across multiple threads.
Atomic Flags: Used to indicate status or control flow in a threadsafe manner.
35
Semaphores
Semaphores are synchronization tools used in operating systems to manage concurrent access
to shared resources and coordinate process or thread execution. They help solve issues related
to mutual exclusion and synchronization.
Types of Semaphores
Binary Semaphore: Also known as a mutex (short for mutual exclusion). It can only have two
values: 0 or 1. It's used to control access to a critical section, ensuring that only one process
or thread can access the shared resource at a time.
Counting Semaphore: Can have a range of integer values. It is used to manage a resource that
has a limited number of instances. The value represents the number of available resources.
For example, a counting semaphore initialized to 5 can allow up to 5 concurrent accesses
before blocking.
Basic Operations
Semaphores provide two primary operations:
Wait (P or Down Operation): This operation decrements the semaphore’s value. If the
semaphore’s value is greater than zero, it decrements the value and allows the process or
thread to proceed. If the value is zero, the process or thread is blocked until the semaphore’s
value is greater than zero.
Signal (V or Up Operation): This operation increments the semaphore’s value. If there are
any processes or threads waiting (blocked) on the semaphore, one of them is unblocked.
How Semaphores Work
Initialization: A semaphore is initialized with a value that represents the number of available
resources or permissions. For binary semaphores, the initial value is typically 1, indicating
that the critical section is available. For counting semaphores, the initial value is set based on
the number of available instances of the resource.
Wait Operation (P): When a process or thread wants to enter a critical section or access a
shared resource, it performs a wait operation on the semaphore:
36
If the semaphore’s value is greater than 0, the process or thread decrements the value and
proceeds.
If the value is 0, the process or thread is placed in a waiting queue until the semaphore’s
value is incremented by another process or thread.
Signal Operation (V): After a process or thread finishes using the resource, it performs a
signal operation on the semaphore:
If there are processes or threads waiting, one of them is unblocked and allowed to proceed.
Benefits
Mutual Exclusion: Ensures that only one process or thread can access the critical section or
shared resource at a time.
Synchronization: Coordinates processes or threads to ensure correct execution order and
resource usage.
Deadlock Avoidance: By properly managing semaphore operations, it is possible to design
systems that avoid deadlocks and ensure that all processes eventually get access to resources.
System model
1.Process Model
2. Memory Model
5. Concurrency Model
6. Security Model
7. System Calls
System Calls: Interface for programs to request services from the OS.
Deadlock Characterization
A deadlock happens in operating system when two or more processes need some resource to
complete their execution that is held by the other process.
A deadlock occurs if the four Coffman conditions hold true. But these conditions are not
mutually exclusive. They are given as follows −
Mutual Exclusion
There should be a resource that can only be held by one process at a time. In the diagram below,
there is a single instance of Resource 1 and it is held by Process 1 only.
38
Hold and Wait
A process can hold multiple resources and still request more resources from other processes
which are holding them. In the diagram given below, Process 2 holds Resource 2 and Resource
3 and is requesting the Resource 1 which is held by Process 1.
No Preemption
A resource cannot be preempted from a process by force. A process can only release a resource
voluntarily. In the diagram below, Process 2 cannot preempt Resource 1 from Process 1. It will
only be released when Process 1 relinquishes it voluntarily after its execution is complete.
Circular Wait
A process is waiting for the resource held by the second process, which is waiting for the
resource held by the third process and so on, till the last process is waiting for a resource held
by the first process. This forms a circular chain.
39
Methods for handling deadlocks
1.Deadlock Prevention
Avoid Circular Wait: Ensure that processes acquire resources in a predefined order to prevent
circular dependencies.
Hold and Wait: Require processes to request all required resources at once, avoiding
situations where they hold some resources while waiting for others.
No Preemption: Allow resources to be preempted from processes if necessary to break the
deadlock.
Mutual Exclusion: Limit the use of resources that can be held exclusively, although this may
not always be feasible.
2. Deadlock Avoidance
Banker's Algorithm: Check resource allocation dynamically to ensure that the system remains
in a safe state. It calculates whether granting a resource request will lead to a safe or unsafe
state.
Resource Allocation Graph (RAG): Track the allocation and request of resources to ensure
that no circular wait conditions can occur.
3. Deadlock Detection
Resource Allocation Graph: Use a graph to detect cycles, which indicate deadlocks. The
system periodically checks for cycles in the graph.
Detection Algorithms: Implement algorithms that check for deadlocks by examining resource
allocation and process states, such as the WaitFor Graph.
4. Deadlock Recovery
40
Process Termination: Kill one or more processes involved in the deadlock to break the cycle.
This may be done selectively based on criteria like the least progress made.
Resource Preemption: Reclaim resources from processes to break the deadlock. This involves
forcibly taking resources away and reallocating them to other processes.
Rollback: Revert processes to a safe state before the deadlock occurred, often used with
checkpoints to restore processes.
Unit 3
Storage management
Storage management in an operating system (OS) is responsible for managing data storage on
a computer system, including how data is stored, accessed, and organized on physical and
virtual storage devices. It plays a critical role in ensuring that applications and processes can
efficiently read and write data while optimizing the use of available storage resources.
1. File Systems
A file system organizes and manages files on a storage device, such as a hard drive, SSD, or
USB stick. The OS uses file systems to control how data is stored and retrieved. Common file
systems include:
NTFS (New Technology File System) – used in Windows.
FAT32/exFAT – used for crossplatform compatibility.
EXT4 – used in Linux.
APFS – used in macOS.
2. Memory Management
Memory management refers to the process of controlling and coordinating computer
memory, including RAM and virtual memory. The OS ensures that each process has enough
memory to run efficiently by:
41
Allocating and deallocating memory space.
Managing virtual memory (which uses storage devices to simulate additional RAM).
Handling paging and swapping, where parts of memory are transferred to and from disk
storage.
3. Disk Scheduling
Disk scheduling algorithms determine the order in which read and write requests are
processed by the disk. Efficient disk scheduling improves performance by reducing the seek
time and rotational latency in hard drives. Some common algorithms include:
FCFS (FirstComeFirstServed): Processes requests in the order they arrive.
SSTF (Shortest Seek Time First): Processes requests closest to the current head position
first.
SCAN (Elevator Algorithm): Moves the disk arm across the disk in one direction,
processing requests along the way.
7. Cache Management
Caching helps speed up access to frequently used data by storing it in faster storage, such as
RAM or SSDs. The OS manages the cache to ensure that critical data is readily available
without excessive access to slower storage.
42
8. Security and Access Control
The OS manages access to storage resources by enforcing security policies, user
authentication, and permissions. This ensures that only authorized users can access or modify
certain files.
43
Disk scheduling algorithms became essential as hard drives got larger and more users
began to multitask on computers. These algorithms improved the speed and efficiency of
reading and writing data to disks.
44
o Swapping in an operating system is a process that moves data or programs between
the computer’s main memory (RAM) and a secondary storage (usually a hard
disk or SSD).
o This helps manage the limited space in RAM and allows the system to run more
programs than it could otherwise handle simultaneously.
o It’s important to remember that swapping is only used when data isn’t available in
RAM. Although the swapping process degrades system performance, it allows larger
and multiple processes to run concurrently.
o Because of this, swapping is also known as memory compaction. The CPU scheduler
determines which processes are swapped in and which are swapped out.
o Consider a multiprogramming environment that employs a prioritybased scheduling
algorithm.
o When a highpriority process enters the input queue, a lowpriority process is swapped
out so the highpriority process can be loaded and executed.
o When this process terminates, the lowpriority process is swapped back into memory
to continue its execution.
The below figure shows the swapping process in the operating system:
Swapping has been subdivided into two concepts: swapin and swapout.
• Swapout is a technique for moving a process from RAM to the hard disc.
• Swapin is a method of transferring a program from a hard disc to main memory, or
RAM.
Process of Swapping
45
• When the RAM is full and a new program needs to run, the operating system selects a
program or data that is currently in RAM but not actively being used.
• The selected data is moved to the secondary storage, making space in RAM for the
new program.
• When the swappedout program is needed again, it can be swapped back into RAM,
replacing another inactive program or data if necessary.
Contiguous Memory Allocation
46
Advantages of Contiguous Memory Allocation:
Simple and easy to implement: Since each process occupies a single block of memory, the
OS only needs to track the base and limit addresses.
Fast memory access: The CPU can quickly calculate memory addresses since all parts of the
process are stored contiguously.
Solutions to Fragmentation:
Compaction: A technique used to reduce external fragmentation by moving allocated
memory blocks together and consolidating free space into one large block. However,
compaction is timeconsuming and resourceintensive.
Example:
Suppose a system has 100 MB of memory, and three processes need 20 MB, 30 MB, and 10
MB. The operating system would allocate a continuous block of memory for each process
based on their requirements, but this could lead to unused gaps if the processes terminate or
new processes cannot fit into the available memory.
Paging
Paging is a memory management technique used in operating systems to avoid the issues of
fragmentation (both internal and external) that arise with contiguous memory allocation. In
paging, the physical memory is divided into fixedsize blocks called frames, and the logical
memory (the address space used by a process) is divided into blocks of the same size, called
pages. This allows noncontiguous allocation of memory, meaning that a process can be
loaded into memory in a scattered manner.
47
Key Concepts in Paging:
2. Page Table:
Each process has its own page table, which keeps track of where its pages are stored in
physical memory. The page table contains mappings from page numbers (logical addresses)
to frame numbers (physical addresses).
When a process references a memory address, the operating system uses the page table to
translate the logical page number to the corresponding physical frame.
3. Address Translation:
A logical address consists of two parts: the page number and the offset.
The page number identifies which page of the logical address space the address belongs to,
and the offset specifies the exact location within that page.
Using the page number, the system consults the page table to find the corresponding frame
number. The offset is then added to the starting address of the frame to determine the exact
physical memory address.
The system checks the page table to see which frame contains the second page and then
calculates the physical address by adding the offset.
Advantages of Paging:
1. Eliminates External Fragmentation: Since pages can be loaded into any available frame,
physical memory is used more efficiently, with no need for contiguous allocation.
48
2. Supports Larger Virtual Memory: Paging allows processes to use more memory than is
physically available by storing parts of the process in virtual memory (secondary storage).
3. Efficient Memory Utilization: Pages are loaded into memory as needed, which can
improve overall memory usage, especially when some parts of a program are not used
frequently.
Disadvantages of Paging:
1. Internal Fragmentation: Although paging eliminates external fragmentation, there may still
be some internal fragmentation if the last page of a process doesn’t fully use the allocated
frame, leaving some space wasted.
2. Overhead of Page Table Management: Maintaining page tables for each process requires
additional memory. For large processes, page tables can become very large, increasing
overhead.
3. Page Table Access Time: Every time a process accesses memory, the system must consult
the page table, which can slow down memory access. This can be mitigated using Translation
Lookaside Buffers (TLB), a type of cache that stores frequently used page table entries.
Types of Paging:
1. Demand Paging: Pages are only loaded into memory when they are needed. If a page is not
in memory when accessed, a page fault occurs, and the OS fetches the page from secondary
storage.
2. Multilevel Paging: For large address spaces, page tables themselves can become very
large. Multilevel paging breaks the page table into smaller parts, reducing memory usage.
Example of Paging:
Assume a process with a logical address space of 4000 bytes and a page size of 1000 bytes.
The process will be divided into 4 pages (Page 0, Page 1, Page 2, Page 3). These pages can be
loaded into any available physical memory frames, say Frame 5, Frame 2, Frame 8, and
Frame 4, respectively. The page table for the process will map each page to its corresponding
frame.
49
|||
| Page 0 | Frame 5 |
| Page 1 | Frame 2 |
| Page 2 | Frame 8 |
| Page 3 | Frame 4 |
Page Fault:
A page fault occurs when a process tries to access a page that is not currently loaded in RAM.
When this happens:
The OS retrieves the page from secondary storage (usually a hard disk or SSD) and loads it
into a free frame.
The page table is updated to reflect the new pageframe mapping.
1. Page Number: The index of the page in the process’s logical address space. The page table
is indexed by page numbers to look up frame numbers.
50
2. Frame Number: The entry in the page table corresponding to each page number. This
points to the physical frame in which the page is currently stored in the physical memory
(RAM).
Each row in the page table corresponds to a single page in the process’s logical address space
and maps to a frame in the physical memory.
Page Number: Indicates the index of the page in the logical address space.
Frame Number: Points to the corresponding frame in physical memory. If the page is not
currently in memory (for example, due to swapping), the frame number may be empty or
invalid.
Status (Valid/Invalid): Indicates whether the page is currently loaded in memory. A valid
entry means the page is in physical memory, while an invalid entry indicates the page is not
loaded (page fault will occur if accessed).
51
MultiLevel Page Tables:
For large address spaces, a singlelevel page table becomes inefficient, as the table itself could
be too large to fit into memory. To address this, multilevel page tables break down the page
table into smaller parts, reducing memory overhead.
Summary:
52
Page Table is a key component in paging, mapping virtual page numbers to physical frame
numbers.
Each Page Table Entry (PTE) contains the frame number and additional status bits like the
valid/invalid bit, dirty bit, and access permissions.
For large address spaces, multilevel page tables are used to manage memory more efficiently.
TLB is a cache used to speed up memory access by storing frequently accessed page table
entries.
This structure allows efficient memory management and enables the OS to handle large
processes that may not fit entirely in physical memory.
Segmentation
Segmentation is a memory management technique in operating systems that divides a
process's memory into logically distinct units called segments. Unlike paging, where the
memory is divided into fixedsize pages, segmentation divides memory into variablesized
segments, each representing a specific type of data or functionality. Each segment
corresponds to a logical division of a program, such as code, data, stack, heap, etc., allowing
for more meaningful memory organization.
Key Concepts of Segmentation:
1. Segment:
o A segment is a variablesized portion of a process's memory that corresponds
to a logical unit. Each segment can be thought of as a different part of the
program, like the code segment, data segment, stack segment, or heap
segment.
o Unlike paging, where all pages are the same size, segments can have different
sizes depending on the program’s needs.
2. Segment Table:
o The operating system uses a segment table to keep track of each segment's
base address (starting location in physical memory) and its limit (size of the
segment).
o Each process has its own segment table, which maps segment numbers to
physical memory addresses.
Structure of the Segment Table:
A segment table entry typically contains two fields:
• Base Address: The starting address of the segment in physical memory.
• Limit: The length (size) of the segment.
53
Segment Number Base Address Limit (Size)
0 3000 1200
1 4200 800
2 5000 1500
3 7000 1000
54
3. Limited Scalability: Handling large numbers of segments can be challenging, and
maintaining segment tables can consume significant memory.
Segmentation vs Paging:
• Memory Division: In segmentation, memory is divided into logical units (segments),
while in paging, memory is divided into fixedsize pages.
• Fragmentation: Segmentation suffers from external fragmentation (because of
variable segment sizes), while paging suffers from internal fragmentation (because
the last page may not be fully used).
• Logical Meaning: Segmentation corresponds to logical units like functions, arrays,
and data, while paging is purely a physical memory management technique.
• Address Structure: Segmentation uses a segment number and offset for address
translation, while paging uses a page number and offset.
Example of Segmentation:
Consider a process that is divided into four segments: code, data, stack, and heap. Each
segment has a different size and is stored at different locations in memory.
In this example:
• The code segment is 4000 bytes, starting at physical address 1000.
• The data segment is 3000 bytes, starting at physical address 5000.
• The stack and heap segments are of different sizes and have different starting points
in memory.
Segmentation with Paging:
To overcome the limitations of pure segmentation (especially external fragmentation), some
systems combine segmentation and paging. In this scheme:
• Each segment is divided into fixedsize pages, and those pages are loaded into
memory.
• This combines the benefits of logical segmentation and the efficient memory use of
paging.
55
Virtual memory background
Virtual memory is a memory management technique used in operating systems to give the
illusion of a large, continuous block of memory to each process, even though the physical
memory (RAM) is limited. It allows the system to execute programs that may require more
memory than what is physically available by temporarily transferring data between the
physical memory and secondary storage (like a hard disk or SSD).
56
The virtual address space is mapped to physical memory by the operating system, using a
combination of paging and segmentation.
2. Paging:
Paging is a key mechanism used in virtual memory. In paging, both the virtual memory and
physical memory are divided into fixedsize blocks: pages (in virtual memory) and frames (in
physical memory). The operating system maintains a page table that maps virtual pages to
physical frames.
When a process accesses a page that is not in physical memory, a page fault occurs. The
operating system then loads the missing page from secondary storage (disk) into RAM.
3. Demand Paging:
In demand paging, pages are loaded into memory only when they are needed. This
minimizes the amount of physical memory used at any given time, as pages are fetched on
demand rather than all at once.
5. Thrashing:
Thrashing occurs when a system spends more time swapping pages in and out of memory
than executing actual processes. This can happen when the system is overloaded and has
insufficient memory for the working set of all active processes. Thrashing severely degrades
performance.
57
2. Isolation and Security: Virtual memory provides each process with its own virtual address
space, protecting processes from each other. This isolation enhances both security and
stability, as one process cannot directly access another process’s memory.
4. Memory Protection: Since each process operates in its own virtual address space, the
operating system can enforce access control, protecting processes from accessing or
modifying the memory of other processes or the kernel.
3. Increased Resource Usage: The need for additional memory and CPU resources to
maintain page tables, manage page faults, and handle disk I/O can increase overall system
overhead.
58
2. Translation Lookaside Buffer (TLB): A cache that stores frequently used page table entries
to speed up address translation.
3. MMU (Memory Management Unit): A hardware component responsible for performing the
address translation from virtual to physical addresses.
Demand paging
Demand Paging is a memory management technique used in virtual memory systems where
pages of a process are loaded into memory only when they are needed, rather than loading the
entire process into memory at once. This lazy loading approach helps to conserve memory by
loading only the required pages and keeping the others on disk until they are accessed.
1. Lazy Loading:
In demand paging, when a process starts execution, no pages are loaded into memory
initially. Instead, the pages are brought into memory on demand, i.e., only when the process
tries to access them.
This approach contrasts with prepaging, where multiple pages may be loaded in
anticipation of future use.
2. Page Fault:
When a process tries to access a page that is not currently in physical memory (because it
hasn't been loaded yet), a page fault occurs.
A page fault is an interrupt that notifies the operating system to load the required page from
secondary storage (disk) into physical memory.
3. Page Replacement:
If physical memory is full and a new page needs to be loaded (due to a page fault), the
operating system must decide which page in memory to swap out. This is handled by page
replacement algorithms such as Least Recently Used (LRU), FIFO, or Clock algorithms.
4. Page Table:
Each process has a page table that keeps track of whether a page is in physical memory or
on disk. If the page is in memory, the page table entry has a valid bit set. If it's on disk, the
valid bit is cleared, and the page fault handler will load it into memory when accessed.
59
5. Secondary Storage (Swap Space):
Pages that are not currently in physical memory are stored in a special area of the disk
known as swap space. When a page fault occurs, the operating system retrieves the page from
this area and loads it into memory.
1. Process Execution:
When a process is started, its pages are not immediately loaded into memory. The
operating system only loads a minimal portion of the process into memory to begin
execution, typically the first instruction.
2. Page Access:
As the process executes, it will try to access various pages in its virtual address space. If
the required page is already in memory, the CPU accesses it normally.
1. The process tries to access P1, which triggers a page fault since P1 is not in memory.
2. The OS loads P1 from disk into memory.
60
3. The process now accesses P2, causing another page fault.
4. The OS loads P2 into memory, and so on.
If the process tries to access P4 when memory is full, the OS must decide which page to evict
(P1, P2, or P3) using a page replacement algorithm.
61
Clock Algorithm: A more efficient approximation of LRU that uses a circular buffer and a
reference bit.
Summary:
Demand paging is an efficient memory management technique that loads pages into memory
only when they are needed, reducing memory usage and allowing programs to run that are
larger than the physical memory available. It works through lazy loading and page faults,
with the operating system deciding which pages to swap in and out as processes execute.
While it offers several advantages, such as efficient memory usage and support for large
applications, it can lead to performance issues like page fault overhead and thrashing if not
managed properly.
CopyonWrite
CopyonWrite (COW) is an optimization technique used in memory management systems to
efficiently handle the duplication of data between processes. When two or more processes
share the same data (usually after a fork system call), instead of creating an immediate copy
of the data for each process, the operating system allows them to share the same memory
until one of the processes modifies the data. At that point, a copy of the shared data is made,
and the modification is applied to the new copy.
This technique is particularly useful in reducing the overhead of memory copying and
optimizing system performance, especially when working with processes that do not
immediately modify the shared data.
62
3. Page Table Marking:
Memory pages shared between processes are marked as readonly. This prevents any
process from modifying the data directly.
When a process attempts to write to a shared page, a write access violation occurs, which
triggers the CopyonWrite mechanism. The operating system then allocates a new page, copies
the data, and allows the process to make changes to the new copy.
4. Efficient Forking:
COW is commonly used during the fork system call in Unixlike operating systems.
Fork creates a new process (child) that is a duplicate of the calling process (parent).
Without COW, all memory pages of the parent would have to be copied to the child, even if
the child doesn’t modify them. With COW, the parent and child initially share all pages,
saving time and memory.
Only when the child or parent modifies a shared page is a copy made, reducing the
overhead of process creation.
Example of CopyonWrite:
Steps in CopyonWrite:
1. Forking a Process:
A parent process forks a child process. The operating system marks all pages of the parent
process as readonly and maps the child process to the same pages.
2. Read Access:
Both processes can read from the shared memory pages without issue, since the data hasn’t
been modified.
63
3. Write Access (COW Trigger):
If one of the processes (say the child) tries to modify a page, a page fault occurs because
the memory is marked as readonly.
The operating system then creates a copy of the page for the child, marking the new page
as writable. The child can now modify its copy without affecting the parent’s data.
4. Process Separation:
Over time, as both processes make modifications, they start operating on their own copies
of the memory pages. Pages that remain unchanged continue to be shared.
Advantages of CopyonWrite:
1. Memory Efficiency:
COW optimizes memory usage by sharing memory pages between processes until one of
them modifies the data. This reduces the need for redundant memory copying.
2. Performance Improvement:
By deferring the memory copy operation, COW improves performance, especially in
scenarios where many processes are created (like in forking), but only a few modifications
are made to the shared memory.
4. Lower Overhead:
COW minimizes the memory and CPU overhead by copying data only when necessary,
improving system performance in multitasking environments.
Disadvantages of CopyonWrite:
2. Complexity:
Implementing COW requires additional complexity in managing memory and handling
page faults. The operating system needs to carefully manage readonly and writable pages
across processes.
1. Process Forking:
In Unixbased systems, fork is a common system call used to create new processes. COW is
employed to minimize memory duplication when a process forks. For instance, after forking,
the child process may call `exec()` to replace its address space with a new program, making
the initial copy of the address space unnecessary.
3. File Systems:
Certain file systems, such as Btrfs and ZFS, use COW to handle file updates. Instead of
overwriting the original data when modifying a file, they create a new copy of the data. This
approach provides features like atomic updates, snapshots, and data integrity.
Summary:
CopyonWrite (COW) is a memory optimization technique where memory pages are shared
between processes until one process attempts to modify the page. At that point, a copy of the
page is made, allowing the modifying process to have its own copy without affecting the
other processes. COW is widely used in process creation, virtual machines, and file systems
to optimize memory usage and reduce overhead. However, it introduces complexity in
memory management and can incur page fault penalties during modifications.
65
Page replacement is a key aspect of memory management in operating systems, especially in
systems using paging or demand paging. It occurs when the operating system needs to bring a
new page into memory, but all physical memory (RAM) frames are already occupied. The OS
must then decide which existing page in memory should be removed (or evicted) to make
room for the new page. This decision is made by a page replacement algorithm.
1. Page Fault:
When a process attempts to access a page that is not currently in memory, a page fault
occurs.
The OS needs to load the requested page from secondary storage (disk) into RAM. If there
are no free frames available, it must replace an existing page.
2. Victim Page:
The victim page is the page selected to be evicted from memory to free up space for the
new page. This choice is critical to system performance, as choosing the wrong page can lead
to frequent page faults and slow down the system.
66
Description: This algorithm evicts the page that has not been used for the longest time. The
assumption is that pages used recently are more likely to be used again soon.
Advantages: Good approximation of the optimal algorithm, widely used.
Disadvantages: Can be costly to implement, as it requires tracking the order of page
accesses, typically via a linked list or stack, or through hardware support like timestamps.
Implementation:
Stackbased: A stack keeps track of the order in which pages are accessed, and the least
recently used page is at the bottom of the stack.
Counterbased: Each page has a counter that tracks when it was last accessed, and the page
with the oldest counter is evicted.
67
Disadvantages: Can suffer if pages are accessed heavily at the beginning but not used
again, as LFU might keep those pages in memory longer than necessary.
Implementation: Pages are tracked with a counter, which increments every time the page is
accessed. The page with the smallest counter is evicted.
1. Page Access:
When a process accesses a page, the OS checks whether the page is in memory. If the page
is not in memory, a page fault occurs.
3. Page Replacement:
The OS loads the new page from disk into memory and updates the page table to reflect the
new page’s presence in memory.
68
4. Updating the Page Table:
The page table entry of the newly loaded page is updated to mark it as resident in memory,
and the page table entry of the evicted page is updated to reflect its new status (either on disk
or invalid).
Performance Considerations:
2. Thrashing:
Thrashing occurs when the system spends more time handling page faults (swapping pages
in and out of memory) than executing actual processes. Thrashing often happens when the
working set (the set of pages a process needs to keep in memory to avoid frequent page
faults) is larger than the available physical memory.
69