Complete OS Notes
Complete OS Notes
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
OS Definition
Operating system is a system software that acts as an intermediary between the user of a computer and computer hardware. It is
considered as the brain of the computer. It controls the internal activities of the computer hardware and provides the user interface.
This interface enables a user to utilize the hardware resources very efficiently. It is the first program that gets loaded into the
computer memory through the process called “booting”.
Some popular operating System are windows9x(95,98), Linux, Unix, Windows xp. vista etc.
A computer system has many resources (hardware and software), which may be required to complete a task. The commonly required
resources are input/output devices, memory, file storage space, CPU, etc. The operating system acts as a manager of the above
resources and allocates them to specific programs and users, whenever necessary to perform a particular task. Therefore the
operating system is the resource manager i.e. it can manage the resource of a computer system internally.
In simple terms, an operating system is an interface between the computer user and the machine.
Below we have an abstract view of the components of the computer system.
3.Memory Management:
It keeps track of the resources (memory),what part of memory is in use and by whom, which part of the memory is not in use.
4.Process Management:
A process(task) is an instance of a program in execution. A program is just a passive entity, but a process is an active entity. To
accomplish its task ,a process needs certain resources like CPU time, memory, files and I/O devices. These resources are allocated
to process either at the time of creation or when it is executing. The operating system is responsible for functions related to process
management.
6.File Management:
A file is a collection of related information or record defined by the user. The operating system is responsible for various activities
of file management like Creation and deletion of files ,Creation and deletion of directories Manipulation of files and directories.
8.Protection/Security Management:
If a computer system has a multiple processor, then the various processes must be protected of one another’s activities. Protection
refers to mechanism for controlling user access of programs or processes or user to resources defined by the computer system.
The terms Operating System (OS) functions and OS services are often used interchangeably, but they refer to slightly different
aspects of an operating system. Here's a clear differentiation:
OS Services
OS services refer to the specific functionalities or tools provided by the operating system to users and applications. These services
are built on top of the core OS functions and are designed to make the system more user-friendly and efficient. Like-
Program Execution: Loads and runs programs. Example: Running a web browser or a word processor.
I/O Operations: Facilitates input and output operations for devices. Example: Reading from a file or printing a document.
File System Manipulation: Allows users to create, delete, and modify files. Example: Saving a document or renaming a file.
Communication: Enables communication between processes or systems. Example: Sending data over a network or using inter-
process communication (IPC).
Error Detection and Handling: Detects and resolves errors in hardware or software. Example: Displaying an error message
when a program crashes.
User Interface: Provides interfaces (CLI, GUI, or touch-based) for user interaction. Example: Using a graphical interface to
open apps or typing commands in a terminal.
Resource Allocation: Allocates resources like CPU, memory, and devices to processes. Example: Assigning more CPU power
to a high-priority task.
Protection and Security: Ensures data and system integrity. Example: Requiring a password to log in or encrypting files.
Analogy:
OS Functions: Like the engine of a car (internal operations that keep the car running).
OS Services: Like the steering wheel, pedals, and dashboard (tools provided to the driver to control and interact with the car).
OS Service Errors or Abnormal Behaviours
1. Infinite Loop: Program gets stuck in an endless loop.
2. Crash: Program terminates unexpectedly.
1. Program Execution 3. Memory Leak: Program fails to release memory.
4. Deadlock: Program waits indefinitely for resources.
5. Incorrect Output: Program produces wrong results due to logic errors.
1. Device Not Found: I/O device is not detected.
2. I/O Errors: Errors during read/write operations.
2. I/O Operations 3. Buffer Overflow: Data exceeds allocated buffer size.
4. Device Busy: Device is already in use.
5. Data Corruption: Data is altered or lost during I/O operations.
1. File Corruption: File data becomes unreadable.
2. Permission Denied: Unauthorized access to files.
3. File System Manipulation 3. Disk Full: No storage space available.
4. File Not Found: Attempt to access a non-existent file.
5. Fragmentation: Files stored in non-contiguous blocks, reducing performance.
1. Network Congestion: High traffic slows down communication.
2. Packet Loss: Data packets are lost during transmission.
4. Communication 3. Connection Timeout: Network connection fails due to delays.
4. DNS Failure: Domain name resolution fails.
5. Unauthorized Access: Intruder gains access to the network.
1. Undetected Errors: Errors go unnoticed by the OS.
2. Incorrect Error Handling: Errors are handled improperly.
5. Error Detection and Handling 3. Silent Failures: Errors occur without notification.
4. Recovery Failure: System fails to recover from an error.
5. False Positives: Normal behavior is incorrectly flagged as an error.
1. UI Freeze: Interface becomes unresponsive.
2. Display Glitches: Visual artifacts or incorrect rendering.
6. User Interface 3. Input Lag: Delayed response to user input.
4. Crash: Application or system crashes, closing the UI.
5. Accessibility Issues: UI not usable by people with disabilities.
1. Resource Starvation: Process denied necessary resources.
2. Over-Allocation: Too many resources allocated to a single process.
7. Resource Allocation 3. Under-Allocation: Insufficient resources allocated, causing poor performance.
4. Deadlock: Resources held by processes waiting for each other.
5. Resource Leak: Resources not released after use.
1. Unauthorized Access: Unauthorized user accesses system resources.
2. Virus/Malware Infection: Malicious software compromises system security.
8. Protection and Security 3. Data Breach: Sensitive data is accessed or stolen.
4. Privilege Escalation: User gains higher access rights illegally.
5. Firewall Failure: Unauthorized network access due to firewall misconfiguration.
1. Incorrect Logging: System fails to log job activities accurately.
2. Data Loss: Job accounting data is lost due to system errors.
9. Job Accounting 3. Unauthorized Access: Unauthorized user accesses job accounting data.
4. Resource Overuse: System fails to track resource usage accurately.
5. Incomplete Records: Job accounting records are missing or incomplete.
OS
OS Services Real-Life Examples
Functions
Resource Program Execution Running a web browser or a game on your computer.
Management Resource Allocation Allocating more CPU power to a video editing software for faster rendering.
Program Execution Switching between multiple apps on your smartphone
Process
Management Communication Using inter-process communication (IPC) for apps to share data (e.g., copying text
from one app to another).
Your computer slowing down when too many apps are open due to insufficient
Memory Resource Allocation
RAM.
Management
Program Execution Loading a game level into memory for faster access.
Saving a Word document to the "Documents" folder or organizing photos into
File System File System Manipulation folders.
Management
I/O Operations Copying files from a USB drive to your computer.
Device I/O Operations Printing a document from your laptop to a wireless printer.
Management Resource Allocation Allocating printer resources to one user at a time in a shared office environment.
Security and Protection and Security Logging into your computer with a password or fingerprint.
Protection Error Detection and Handling An antivirus blocking unauthorized access or malware.
Your computer showing a "Blue Screen of Death" (BSOD) and restarting to
Error Detection and Handling
Error recover.
Handling A program crashing and the OS displaying an error message like "This program has
Program Execution
stopped working."
Communication Browsing the internet, streaming Netflix, or sending an email.
Networking
Resource Allocation A server distributing bandwidth to multiple users accessing a website.
User User Interface Using a touchscreen on your smartphone or typing commands in the terminal (CLI).
Interface Program Execution Opening apps using a graphical user interface (GUI).
Notes : S3-Classification of Operating Systems
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
2. Multiprogramming Systems
Sharing the processor, when two or more programs reside in memory at the same time, is referred as multiprogramming.
Multiprogramming assumes a single shared processor. Multiprogramming increases CPU utilization by organizing jobs so that the
CPU always has one to execute. The following figure shows the memory layout for a multiprogramming system.
constraints, otherwise the system will fail. For example, scientific experiments, medical image systems, industrial control systems,
weapon systems, robots, air traffic control systems, etc. There are two types of real-time operating systems.
Advantages of RTOS:
Maximum Consumption: Maximum utilization of devices and system, thus more output from all the resources
Task Shifting: Time assigned for shifting tasks in these systems are very less. For example in older systems it takes
about 10 micro seconds in shifting one task to another and in latest systems it takes 3 micro seconds.
Focus on Application: Focus on running applications and less importance to applications which are in queue.
Real time operating system in embedded system: Since size of programs is small, RTOS can also be used in embedded
systems like in transport and others.
Error Free: These types of systems are error free.
Memory Allocation: Memory allocation is best managed in these types of systems.
Disadvantages of RTOS:
Limited Tasks: Very few tasks run at the same time and their concentration is very less on few applications to avoid
errors.
Use heavy system resources: Sometimes the system resources are not so good and they are expensive as well.
Complex Algorithms: The algorithms are very complex and difficult for the designer to write on.
Device driver and interrupt signals: It needs specific device drivers and interrupts signals to response earliest to
interrupts.
Thread Priority: It is not good to set thread priority as these systems are very less prone to switching tasks.
Examples of Real-Time Operating Systems are: Scientific experiments, medical imaging systems, industrial control systems,
weapon systems, robots, air traffic control systems, etc.
Advantages of Multi-User OS
Resource Sharing – Multiple users can share hardware and software resources efficiently.
Cost-Effective – Reduces the need for individual computers by allowing multiple users on one system.
Centralized Data Management – Users can store and access data from a central location, improving collaboration.
Improved Security – User accounts and permissions help maintain data privacy and restrict unauthorized access.
Remote Accessibility – Users can access the system from different locations over a network.
Disadvantages of Multi-User OS
High Cost of Maintenance – Requires advanced hardware and software to support multiple users efficiently.
Slower Performance – If too many users are active, the system might experience delays.
Security Risks – A vulnerability in one user’s session could impact the entire system.
Complex System Management – Requires system administrators to manage users, permissions, and resources.
Dependence on Network – If the network fails, user access and system functionality can be disrupted.
**Parallel Processing:
Parallel processing requires multiple processors and all the processor works simultaneously in the system. Here, the task is divided
into subparts and these subparts are then distributed among the available processors in the system. Parallel processing completes
the job on the shortest possible time. All the processors in the parallel processing environment should run on the same operating
system. All processors here are tightly coupled and are packed in one casing. All the processors in the system share the common
secondary storage like the hard disk.
The user need not to be aware of the inner architecture of the machine. He should feel that he is dealing with the single processor
only and his interaction with the system would be the same as in a single processor.
Advantages
1. It saves time and money as many resources working together will reduce the time and cut potential costs.
2. It can be impractical to solve larger problems on Serial Computing.
3. It can take advantage of non-local resources when the local resources are finite.
4. Serial Computing ‘wastes’ the potential computing power, thus Parallel Computing makes better work of the hardware.
Disadvantages
1. It addresses such as communication and synchronization between multiple sub-tasks and processes which is difficult to achieve.
2. The algorithms must be managed in such a way that they can be handled in a parallel mechanism.
3. The algorithms or programs must have low coupling and high cohesion. But it’s difficult to create such programs.
4. More technically skilled and expert programmers can code a parallelism-based program well.
Operating systems (OS) can be classified based on various criteria, such as their architecture, purpose, number of users, task
handling, and interactivity. Below is a detailed classification of operating systems:
1. Based on Architecture
2. Based on Purpose
5. Based on Interactivity
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
Hardware Layer:
o This is the lowest layer and interacts directly with the physical hardware (e.g., CPU, memory, storage devices).
o It handles hardware-specific operations and provides an abstraction to the upper layers.
o Example: Managing memory allocation or handling interrupts.
Kernel Layer:
o The kernel is the core of the operating system and resides above the hardware layer.
o It manages system resources like memory, CPU, and I/O devices.
o Example: Scheduling processes, managing memory, and handling device communication.
System Call Layer:
o This layer provides an interface between the kernel and user applications.
o It allows applications to request services from the kernel, such as file operations or process management.
o Example: A program requesting to read a file from the disk.
Application Layer:
o This is the topmost layer and is closest to the user.
o It includes user applications like web browsers, word processors, and games.
o Example: Running a web browser or editing a document.
Types of OS structure:
1. Simple Structure
▪ MS-DOS is an operating system created for personal computers. It was developed by Microsoft. It is a classic example of
an operating system with a layered structure.
▪ MS-DOS operating system is split into various layers and each of the layers have different functionalities.
▪ Layering provides a distinct advantage in the MS-DOS operating system because all the layers can be defined separately
and interact with each other as required.
▪ Also, it is easier to create, maintain and update the system if it is done in the form of layers. Change in one layer
specification does not affect the rest of the layers.
▪ However, the layers in MS-DOS are not that sharply defined and the layer specifications often bleed into each other.
2. Monolithic systems
Entire operating system is working in kernel space.
A set of primitives or system calls implement all operating system services such as process management, concurrency,
and memory management. Device drivers can be added to the kernel as modules.
User services and kernel services are implemented under same address space. As both services are implemented
under same address space, this makes operating system execution faster.
If any service fails the entire system crashes, and it is one of the drawbacks of this kernel. The entire operating system
needs modification if user adds a new service.
Acts as a virtual machine which controls all hardware parts
Examples: Linux, Unix like kernels, BSD, OS/360
1. Layered systems
A layered system consists of a series of layers, each of which depends only on the correct operation of the
layer immediately beneath it.
The lowest layer represents the hardware interface, and the highest layer the application interface.
All the layers can be defined separately and interact with each other as required. Also, it is easier to create,
maintain and update the system if it is done in the form of layers. Change in one layer specification does not
affect the rest of the layers.
A key problem in a layered system is deciding on the ordering of the layers. (This is critical because of the
requirement that each layer can ultimately only use services provided by layers below it - so the ordering
cannot have any cycles.)
Examples OS/2, window NT
2. Microkernel structure
Kernel is made as small as possible only the most important services are put inside the kernel and rest of the
OS service are present in the system application program.
The user can easily interact with those not-so important services within the system applications kernel is
solely responsible for the three most important services of operating system namely:
o Inter-Process communication
o Memory management
o CPU scheduling
Microkernel and system applications can interact with each other by message passing as and when required.
This is extremely advantageous architecture since burden of kernel is reduced and less crucial services are
accessible to the user and hence security is improved too. It is being highly adopted in the present-day systems.
MacOSX, Eclipse IDE is a good example of Microkernel Architecture.
3. Modular structure
▪ A modular operating system is built with its various functions broken up into distinct processes, each with its own
interface.
▪ The primary benefit of the modular approach is that each process operates independently, and if one of them fails or
needs an update, it won’t affect any of the other functions.
▪ The main elements of a modular operating system are a kernel and a set of dynamically loadable applications with their
own discrete memory spaces. The kernel is protected from service and application failures.
▪ The Linux kernel is modular, which means it can extend its capabilities through dynamically-loaded kernel modules.
Comparison table for the types of operating system (OS) structures, highlighting their key characteristics, advantages, and
disadvantages:
OS Structure Description Advantages Disadvantages Examples
- Difficult to maintain and
- High performance due to update.
The entire OS runs as a single
Monolithic direct communication - Lack of modularity. UNIX, MS-
program in kernel space. All
OS between components. - Vulnerable to crashes (one DOS
components are tightly coupled.
- Simple design. failure affects the entire
system).
- Modular and easy to
- Slower performance due to
Only essential services (e.g., memory extend.
frequent context switching
Microkernel management, process scheduling) run - More stable (kernel
between user and kernel modes. Mach, QNX
OS in kernel space. Other services run in crashes are less likely).
- Increased complexity in
user space. - Easier to maintain and
design.
debug.
- Balances performance
- More complex than
Combines features of monolithic and and modularity.
monolithic OS.
microkernel architectures. Some - More stable than Windows NT,
Hybrid OS - May still have performance
services run in kernel space, while monolithic OS. macOS
overhead due to user-kernel
others run in user space. - Easier to extend than
mode switching.
monolithic OS.
- Modular and easy to
The OS is divided into layers, where design. - Performance overhead due to
each layer provides services to the - Easier to debug and layer-to-layer communication. THE OS,
Layered OS
layer above and uses services from the maintain. - Rigid structure (changes in MULTICS
layer below. - Clear separation of one layer may affect others).
concerns.
- High performance and
Provides minimal abstraction over flexibility. - Complex to develop and use.
ExOS,
Exokernel OS hardware, allowing applications to - Applications have fine- - Requires applications to
Nemesis
manage resources directly. grained control over handle low-level details.
resources.
- Isolation between OS
instances.
Provides an abstraction of the - Performance overhead due to
- Efficient resource VMware,
Virtual underlying hardware, allowing virtualization.
utilization. VirtualBox,
Machine OS multiple OS instances to run on a - Requires significant hardware
- Supports running multiple Xen
single physical machine. resources.
OS environments
simultaneously.
- Modular and scalable.
- Performance overhead due to
- Easier to maintain and
Divides the OS into client and server communication between clients
Client-Server update.
components, where clients request and servers. Amoeba,
OS - Fault isolation (server
services from servers. - Requires robust networking
crashes do not affect
infrastructure.
clients).
Notes: S 5- Components of Operating System
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
Key components of an operating system (OS) along with their related sub-activities:
OS Component Related Sub-Activities Examples
- Creation/Termination: Opening and closing a web browser.
- Process creation and termination.
- Scheduling: Allocating CPU time to a video editor.
1. Process - Process scheduling.
- IPC: Sharing data between a music player and a speaker driver.
Management - Inter-process communication (IPC).
- Deadlock Handling: Resolving a situation where two processes
- Deadlock handling.
are waiting for each other to release resources.
- Allocation: Assigning memory to a new application.
- Allocation and deallocation of
- Virtual Memory: Using disk space as RAM when physical
memory.
2. Memory memory is full.
- Virtual memory management.
Management - Memory Protection: Preventing a game from accessing system-
- Memory protection.
critical memory.
- Swapping.
- Swapping: Moving a background process to disk to free up RAM.
- File creation, deletion, and renaming. - File Creation: Saving a Word document.
3. File System - Directory management. - Directory Management: Organizing files into folders.
Management - File access control. - Access Control: Setting read-only permissions for a file.
- Disk space allocation. - Disk Allocation: Allocating space for a new file on the hard drive.
- Device driver management. - Driver Management: Installing a printer driver.
4. Device - Input/output operations. - I/O Operations: Typing on a keyboard or printing a document.
Management - Plug and Play. - Plug and Play: Automatically detecting a USB drive.
- Buffering and caching. - Buffering: Temporarily storing data from a network download.
- User authentication. - Authentication: Logging into a computer with a password.
5. Security and - File and resource permissions. - Permissions: Restricting access to a confidential file.
Access Control - Firewall and antivirus management. - Firewall: Blocking unauthorized network access.
- Encryption and decryption. - Encryption: Securing sensitive data with AES encryption.
- Command-Line Interface (CLI)
- CLI: Running commands in Terminal (Linux) or Command
management.
Prompt (Windows).
- Graphical User Interface (GUI)
6. User Interface - GUI: Clicking icons on a desktop.
management.
- Touch Interface: Swiping on a smartphone screen.
- Touch interface management.
- Voice Commands: Using Siri or Google Assistant.
- Voice command processing.
- Network communication management. - Communication: Sending an email over the internet.
- Network device management. - Device Management: Configuring a router.
7. Networking
- Network security. - Security: Using a VPN to encrypt internet traffic.
- Data packet routing. - Routing: Sending data packets between devices on a network.
- Providing an interface for applications
to request OS services.
- Handling system calls for file - File Operations: Opening or saving a file.
8. System Calls operations, process control, and device - Process Control: Starting or stopping a process.
management. - Device Management: Requesting access to a printer.
- Managing communication between
user programs and the OS kernel.
- Managing system resources.
- Resource Management: Allocating CPU time to a process.
- Handling interrupts and exceptions.
- Interrupts: Handling a keyboard input.
9. Kernel - Ensuring system stability and security.
- Stability: Preventing a crash when a program misbehaves.
- Managing communication between
- Communication: Sending data from an application to a printer.
hardware and software.
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
Process
A process is a program in execution. It is the basic unit of work in an operating system . Each process has its own address space,
which includes the code, data, and stack. Processes are managed by the operating system, which allocates resources and ensures
that processes do not interfere with each other.
Process Generation :
In Unix-like systems, processes are fundamental to the execution of programs. Below is an explanation of process
creation, execution, and synchronization in Unix-like systems, along with simple C programs to demonstrate these concepts.
#include <unistd.h>: This header file provides access to POSIX operating system APIs (Application Programming Interfaces).
pid_t: A data type defined in <sys/types.h> (included by <unistd.h>). Used to store process IDs (PIDs).
fork(): A system call that creates a new process by duplicating the calling process. Returns:
0 to the child process.
Child's PID to the parent process.
-1 if the fork() fails.
o If fork() returns -1, it means the system call failed (e.g., due to insufficient resources).
o The program prints an error message and returns 1 to indicate failure.
getpid(): A system call that returns the PID of the calling process.
1. Process Creation
In Unix-like systems, processes are created using the fork() system call. The fork() function creates a new process (child process)
by duplicating the calling process (parent process). Both processes continue execution from the point where fork() was called.
Example: Process Creation Using fork()
Explanation:
The fork() system call creates a child process.
The child process executes the code inside the if (pid == 0) block.
The parent process executes the code inside the else if (pid > 0) block.
2. Process Execution
After creating a process, you can use the exec() family of system calls to replace the current process image with a new program.
This is often used in the child process to run a different program.
Example: Process Execution Using execl()
Explanation:
The child process replaces itself with the ls -l command using execl().
The parent process continues execution independently.
3. Process Synchronization
Process synchronization ensures that processes coordinate their actions, especially when sharing resources. In Unix-like systems,
the wait() system call is commonly used to synchronize parent and child processes.
Example: Process Synchronization Using wait()
Explanation:
The parent process waits for the child process to terminate using wait().
The child process simulates some work using sleep(2) and then exits.
After the child process terminates, the parent process resumes execution.
Explanation:
The child process runs the ls -l command using execl().
The parent process waits for the child to finish using wait().
After the child process terminates, the parent process resumes execution.
System call
A system call is a mechanism used by programs to request services from the operating system's kernel. It acts as an interface
between user-level applications and the operating system, allowing programs to perform tasks that require privileged access to
hardware or system resources, such as file operations, process control, or network communication.
System calls are fundamental to how applications interact with the operating system and are a core concept in operating system
design and programming.
Example in C:
In this example, the write() function is for the write system call, which writes data to a file .
Table summarizing the common types of system calls along with examples:
1. fork()
Purpose: Creates a new process by duplicating the calling process. The new process is called the child process, and the original
process is called the parent process.
How it works: The child process is an exact copy of the parent process, including code, data, and open files. Both processes
continue execution from the point where fork() was called.
Example:
Explanation:
1. fork() creates a new child process by duplicating the parent process.
2. Both the parent and child processes continue execution from the point where fork() was called.
3. The order of execution between the parent and child processes is non-deterministic. This means either the parent or child
process may execute first, depending on the OS scheduler.
4. The if (pid == 0) block is executed by the child process, and the else block is executed by the parent process.
2. exec() Family
Purpose: Replaces the current process image with a new program. This is often used after fork() to run a different program in the
child process.
Variants: execl(), execv(), execle(), execve(), etc.
How it works: The exec() system call loads a new program into the current process's memory space and starts its execution.
The original program's code, data, and stack are replaced by the new program.
Example:
Explanation:
1. execl() replaces the current process image with the program specified (/bin/ls in this case).
2. Once execl() is called, the current program stops executing, and the ls -l command takes over.
3. The return 0; statement is never reached because the process image is replaced by ls.
3. exit()
Purpose: Terminates the calling process and returns an exit status to the parent process.
How it works: The exit status is a small integer value (typically 0 for success and non-zero for failure). All open file descriptors
are closed, and any child processes are inherited by the init process.
4. wait()
Purpose: Suspends the calling process until one of its child processes terminates.
How it works: The parent process waits for the child process to finish and collects its exit status. If the child has already
terminated, wait() returns immediately.
Example:
Explanation:
1. fork(): Creates a child process by duplicating the parent process. The child process starts execution from the point
where fork() was called. The parent process receives the PID of the child process, and the child process receives 0.
2. Child Process: The if (pid == 0) block is executed by the child process. It prints "Child Process" and then calls exit(0) to
terminate.
3. Parent Process: The else block is executed by the parent process. The parent calls wait(NULL), which suspends its
execution until the child process terminates. After the child process terminates, the parent resumes execution and
prints "Parent Process".
4. Order of Execution: The child process always prints "Child Process" first because the parent process waits for the child to
finish before printing "Parent Process".
5. waitpid()
Purpose: Similar to wait(), but allows the parent process to wait for a specific child process.
How it works: The parent process specifies the PID of the child process to wait for. Additional options can be provided
(e.g., WNOHANG to return immediately if no child has exited).
Example:
Explanation:
1. fork(): Creates a child process by duplicating the parent process. The child process starts execution from the point
where fork() was called. The parent process receives the PID of the child process, and the child process receives 0.
2. Child Process: The if (pid == 0) block is executed by the child process. It prints "Child Process" and then calls exit(0) to
terminate.
3. Parent Process: The else block is executed by the parent process. The parent calls waitpid(pid, NULL, 0), which suspends
its execution until the specific child process (with the PID stored in pid) terminates. After the child process terminates,
the parent resumes execution and prints "Parent Process".
4. Order of Execution: The child process always prints "Child Process" first because the parent process waits for the
specific child to finish before printing "Parent Process".
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
Process in Memory
A process resides in memory through following section i.e.
1) Stack : Stack section contains local variable
2) Heap : Heap section contains memory which will be dynamically allocated during runtime.
3) Data : Data section contains global variable
4) Text : Text section contains code or instruction.
Context Switching
When CPU switches to another process, the system must save the state of the old process and load the saved state for the new
process via a context switch .Context of a process represented in the PCB. Context-switch time is overhead; the system does no
useful work while switching. CS time dependent on hardware support.
The OS uses the PCB to perform context switching and manage processes effectively.
The Process Table keeps a list of all active processes, and the PCB holds details about each process. The PCB enables smooth
process switching, effective multitasking, and efficient resource allocation.
Notes : S8-Process States and Transition Diagram
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
Process States
A process can be in one of several states during its lifecycle. These states represent the current activity or status of the process.
The operating system manages these states and ensures that processes transition smoothly between them.
Process States
New: The process is being created.
Ready: The process is waiting to be assigned to the CPU.
Running: The process is currently being executed by the CPU.
Waiting: The process is waiting for an event to occur (e.g., I/O completion).
Terminated: The process has finished execution.
Transition Diagram
The transition diagram shows the possible states of a process and the events that cause transitions between these states.
The diagram typically includes the following transitions:
The flow in the image represents the lifecycle of a process in an operating system, showing how a process transitions between
different states from creation to termination. Here's a step-by-step explanation of the flow:
1. New: The process is created and enters the "New" state.
2. Admit: The process is admitted to the system and moves to the "Ready" state, where it waits to be assigned to a
processor.
3. Ready: The process is in main memory and is ready to execute. It waits in the ready queue for the scheduler to dispatch it
to the CPU.
4. Dispatch: The scheduler selects the process from the ready queue and dispatches it to the CPU for execution.
5. Running: The process is now executing on the CPU.
6. Timeout: If the process's time slice expires (in a time-sharing system), it is moved back to the "Ready" state to allow
other processes to run.
7. Event Wait: If the process needs to wait for an event (like I/O completion), it moves from the "Running" state to the
"Blocked" state.
8. Blocked: The process is waiting for an event to occur and cannot proceed until the event happens.
9. Event Occur: Once the event the process was waiting for occurs, the process moves back to the "Ready" state.
10. Release: After the process completes its execution, it moves to the "Release" state.
11. Exit: The process is terminated and removed from the system.
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
Schedulers
The scheduler is a program that decides:
1. Which process should run next on the CPU.
2. How long a process should run (time slicing in time-sharing systems).
3. The order in which processes are executed.
It ensures that the CPU is used efficiently, processes are handled fairly, and system resources are allocated optimally.
There are three main types of schedulers:
1. Long-Term Scheduler (Job Scheduler):
2. Short-Term Scheduler (CPU Scheduler):
3. Medium-Term Scheduler:
It controls the Degree of Multi-programming, i.e., the number of processes present in a ready state or in main memory at
any point in time.
It is important that the long-term scheduler make a careful selection of both I/O and CPU-bound processes. I/O-bound
tasks are which use much of their time in input and output operations while CPU-bound processes are which spend their
time on the CPU. The job scheduler increases efficiency by maintaining a balance between the two.
In some systems, the long-term scheduler might not even exist. For example, in time-sharing systems like Microsoft
Windows, there is usually no long-term scheduler. Instead, every new process is directly added to memory for the short-
term scheduler to handle.
Slowest among the three (that is why called long term).
Medium-Term Scheduler:
Medium Term Scheduler (MTS) is responsible for moving a process from memory to disk (or swapping).
It reduces the degree of multiprogramming (Number of processes present in main memory).
A running process may become suspended if it makes an I/O request. A suspended processes cannot make any progress
towards completion. In this condition, to remove the process from memory and make space for other processes, the
suspended process is moved to the secondary storage. This process is called swapping, and the process is said to be
swapped out or rolled out. Swapping may be necessary to improve the process mix (of CPU bound and IO bound)
When needed, it brings process back into memory and pick up right where it left off.
It is faster than long term and slower than short term.
Short-Term Scheduler:
Maximizes CPU utilization and system throughput.
STS (Short Term Scheduler) must select a new process for the CPU frequently to avoid starvation.
The CPU scheduler uses different scheduling algorithms to balance the allocation of CPU time.
It picks a process from ready queue.
Its main objective is to make the best use of CPU.
It mainly calls dispatcher.
Fastest among the three (that is why called Short Term).
Comparison of the Long-Term Scheduler, Medium-Term Scheduler, and Short-Term Scheduler in operating systems:
Aspect Long-Term Scheduler Medium-Term Scheduler Short-Term Scheduler
Also Known As Job Scheduler Swapper CPU Scheduler
Controls the admission of Selects which process in the ready
Manages the swapping of processes between
Purpose processes into the ready queue will execute next on the
memory and disk.
queue. CPU.
Frequency of Infrequent (when a new Very frequent (every few
Moderate (depends on system load).
Operation process is created). milliseconds).
Decides which processes
Decides which processes are swapped out to disk Decides which process gets CPU
Responsibility are loaded into memory
or brought back into memory. time next.
for execution.
Manages the degree of
multiprogramming Balances the system's memory usage and Ensures efficient CPU utilization
Focus
(number of processes in performance. and responsiveness.
memory).
Process State Moves processes Moves processes Moves processes
Transition from New to Ready. between Ready/Suspend and Blocked/Suspend. between Ready and Running.
Affects the overall system
Impact on Affects the system's ability to handle memory Directly impacts CPU utilization
throughput and resource
Performance pressure. and response time.
utilization.
Notes : S10-CPU Scheduling Concepts and Criteria
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
CPU Scheduling:
CPU Scheduling is a process that allows one process to use the CPU while another process is delayed due to unavailability of
any resources such as I / O etc, thus making full use of the CPU. In short, CPU scheduling decides the order and priority of the
processes to run and allocates the CPU time based on various parameters such as CPU usage, throughput, turnaround, waiting
time, and response time. The purpose of CPU Scheduling is to make the system more efficient, faster, and fairer.
It is done by Short-term scheduler (CPU scheduler).
Dispatcher
The dispatcher is responsible for loading the process selected by the Short-term scheduler on the CPU (Ready to Running State).
Context switching is done by the dispatcher only.
Time taken by dispatcher is called dispatch latency or process context switch time.
The objective of multiprogramming is to have some process running at all times, to maximize CPU utilization.
Process execution = cycle of CPU + I/O wait.
▪ Virtually all modern operating systems including Windows, MacOS, Linux, and UNIX use preemptive scheduling algorithms
2. Throughput
A measure of the work done by the CPU is the number of processes being executed and completed per unit of time. This is
called throughput. The throughput may vary depending on the length or duration of the processes.
3. Turnaround Time
For a particular process, an important criterion is how long it takes to execute that process. The time elapsed from the time of
submission of a process to the time of completion is known as the turnaround time. Turn-around time is the sum of times spent
waiting to get into memory, waiting in the ready queue, executing in CPU, and waiting for I/O.
Turn Around Time = Completion Time – Arrival Time.
4. Waiting Time
A scheduling algorithm does not affect the time required to complete the process once it starts execution. It only affects the
waiting time of a process i.e. time spent by a process waiting in the ready queue.
Waiting Time = Turnaround Time – Burst Time.
5. Response Time
In an interactive system, turn-around time is not the best criterion. A process may produce some output fairly early and
continue computing new results while previous results are being output to the user. Thus another criterion is the time taken
from submission of the process of the request until the first response is produced. This measure is called response time.
Response Time = CPU Allocation Time(when the CPU was allocated for the first) – Arrival Time
2. Arrival Time
In CPU Scheduling, the arrival time refers to the moment in time when a process enters the ready queue and is awaiting execution
by the CPU. In other words, it is the point at which a process becomes eligible for scheduling.
3. Completion Time
Completion time is when a process finishes execution and is no longer being processed by the CPU. It is the summation of the
arrival, waiting, and burst times.
Notes : S11- Scheduling Algorithms – Preemptive
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
Example:
Make a gantt chart for following table and calculate average turn around time and waiting time by implementing Round Robin.
(Time slice=4ms and context switch time=1 ms)
Shortest Remaining Time First (SRTF):
The process with the shortest remaining burst time is selected for execution.
If a new process arrives with a shorter burst time, the current process is preempted.
Advantage: Minimizes average waiting time.
Disadvantage: Can lead to starvation of longer processes.
Example:
Make a gantt chart for following table and calculate average turn around time and waiting time by implementing Preemptive SJF
Scheduling(SRTF)
Priority Scheduling:
Processes are executed based on their priority. Higher priority processes are executed first. If a higher priority process arrives, the
current process is preempted. Advantage: Allows for prioritization of important processes. Disadvantage: Can lead to starvation of
lower priority processes.
Examples:
Make a gantt chart for following table and calculate average turn around time and waiting time by implementing Preemptive
Priority based algorithm.(Low number , High priority)
Question:
1. Make a gantt chart and calculate Completion Time (CT) for each process, Turnaround Time (TAT = CT - Arrival Time),
Waiting Time (WT = TAT - Burst Time), Average TAT and WT for all processes.for following table while implementing Round
Robin (RR) with time quantum = 4 ms .
Process Arrival Time (ms) Burst Time (ms)
P1 0 10
P2 1 4
P3 2 8
P4 3 5
2. Make a gantt chart and calculate Completion Time (CT) for each process ,Turnaround Time (TAT = CT - Arrival Time) ,
Waiting Time (WT = TAT - Burst Time) , Average TAT and WT for all processes.for following table while implementing Shortest
Remaining Time Next (SRTN - Preemptive SJF)
Process Arrival Time (ms) Burst Time (ms)
P1 0 10
P2 1 4
P3 2 8
P4 3 5
3. Make a gantt chart and calculate Completion Time (CT) for each process,Turnaround Time (TAT = CT - Arrival Time),
Waiting Time (WT = TAT - Burst Time) ,Average TAT and WT for all processes.
for following table while implementing Priority Preemptive Scheduling (Lower number = Higher priority)
Process Arrival Time (ms) Burst Time (ms) Priority
P1 0 10 3
P2 1 4 1
P3 2 8 2
P4 3 5 4
Notes : S12- CPU Scheduling Algorithms – Non-Preemptive
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
Non-Preemptive Scheduling
Non-preemptive scheduling is a type of CPU scheduling where a process runs to completion without being interrupted by the
operating system . Once a process is assigned to the CPU, it cannot be preempted until it finishes its execution or voluntarily gives
up the CPU (e.g., by waiting for an I/O operation). Non-preemptive scheduling is simpler to implement but may lead to longer
waiting times for some processes.
P0 P1 P2 P3
0 10 16 18 22
Process Arrival Time Burst Time Finish time TAT Waiting Time
(Finish time-Arrival time) (TAT-Burst time)
P0 0 10 10 10-0=10 10-10=0
P1 1 6 16 16-1=15 15-6=9
P2 3 2 18 18-3=15 15-2=13
P3 5 4 22 22-5=17 17-4=13
Result Avg TAT=(10+15+15+17)/4=14.25 Avg WT=(0+9+13+13)/4=8.75
Significant variation of FCFS:
Table is:
Process Arrival Time Burst Time
P0 5 10
P1 3 6
P2 1 2
P3 O 4
P3 P2 P1 P0
0 4 6 12 22
Process Arrival Time Burst Time Finish time TAT Waiting Time
(Finish time-Arrival time) (TAT-Burst time)
P0 5 10 22 22-5=17 17-10=7
P1 3 6 12 12-3=9 9-6=3
P2 1 2 6 6-1=5 5-2=3
P3 O 4 4 4-0=4 4-4=0
Result Avg TAT=(17+9+5+4)/4=8.75 Avg WT=(7+3+3+0)/4=3.25
Here you can see the average waiting time is decreased from 8.75 to 3.25. that is much better from the first solution.
So in this algorithm if we get short process behind long process it decreases the avg WT this particular effect is known as Convoy
Effect.
P0 P2 P3 P1
0 10 12 16 22
To Calculate Avg TAT and Avg WT:
Process Arrival Time Burst Time Finish time TAT Waiting Time
(Finish time-Arrival time) (TAT-Burst time)
P0 0 10 10 10-0=10 10-10=0
P1 1 6 22 22-1=21 21-6=15
P2 3 2 12 12-3=9 9-2=7
P3 5 4 16 16-5=11 11-4=7
Result Avg TAT=(10+21+9+11)/4=12.75 Avg WT=(0+15+7+7)/4=7.25
Priority Scheduling:
Processes are executed based on their priority. Higher priority processes are executed first. Advantage: Allows for prioritization of
important processes. Disadvantage: Can lead to starvation of lower priority processes.
Example:
Make a gantt chart for following table and calculate average turn around time and waiting time by implementing Non Preemptive
Priority based algorithm.
Process Arrival Time Burst Time Process priority
P0 0 10 5
P1 1 6 4
P2 3 2 2
P3 5 4 0
Gantt Chart:
P0 P3 P2 P1
0 10 14 16 22
Process Arrival Time Burst Time Finish time TAT Waiting Time
(Finish time-Arrival time) (TAT-Burst time)
P0 0 10 10 10-0=10 10-10=0
P1 1 6 22 22-1=21 21-6=15
P2 3 2 16 16-3=13 13-2=11
P3 5 4 14 14-5=9 9-4=5
Result Avg TAT=(10+21+13+9)/4=13.25 Avg WT=(0+15+11+5)/4=7.75
Multilevel feedback queue scheduling, however, allows a process to move between queues. Multilevel Feedback Queue
Scheduling (MLFQ) keeps analyzing the behavior (time of execution) of processes and according to which it changes its priority.
Now, look at the diagram and explanation below to understand it properly.
Question:
1. Make a gantt chart and calculate
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
Threads
A thread is the smallest unit of execution within a process. Threads are often referred to as lightweight processes because
they share the same address space and resources as the process that created them.
Threads allow for concurrent execution of tasks within a single process, improving application responsiveness and
performance.
All threads belonging to the same process share – code section, data section, and OS resources (e.g. open files and signals)
But each thread has its own (thread control block) – thread ID, program counter, register set, and a stack.
Any operating system process can execute a thread. we can say that single process can have multiple threads.
Threads are like workers in the same office (shared space, fast communication). Processes are separate offices (isolated, secure,
but slower to coordinate).
Threads can share common data so they do not need to use inter-process communication. Like the processes, threads also have
states like :
1. Born State: A thread that has just been created.
2. Ready State: The thread is waiting for the processor (CPU).
3. Running: The System assigns the processor to the thread means that the thread is being executed.
4. Blocked State: The thread is waiting for an event to occur or waiting for an I/O device.
5. Sleep: A sleeping thread becomes ready after the designated sleep time expires.
6. Dead: The execution of the thread is finished.
What is Multi-Threading?
Multithreading is a technique used in operating systems to improve the performance and responsiveness of computer systems.
Multithreading allows multiple threads (i.e., lightweight processes) to share the same resources of a single process, such as the
CPU, memory, and I/O devices.
All the threads must have a relationship between them (i.e., user threads and kernel threads). Here is a list of three common ways
of establishing this relationship.
Many-to-One Model: The many-to-one model plots several user-level threads to a single kernel thread.
One-to-One Model: The one-to-one model maps every user thread to a kernel thread and provides more concurrency
than the many-to-one model.
Many-to-Many Model: In the many-to-many model, many user-level threads get mapped to a smaller or equal quantity
of kernel threads. The number of kernel threads might be exact to a particular application or a specific machine
pthread_exit(NULL): Ensures the main thread waits for all child threads to finish.
# Example Explanation
Handles multiple client requests simultaneously using threads (e.g., Apache HTTP Server). Each client
1 Web Servers
connection is managed by a separate thread.
GUI Keeps the UI responsive by running background tasks (e.g., file loading, computations) in separate threads
2
Applications while the main thread handles user input.
Database
3 Uses multiple threads to process concurrent queries, improving performance (e.g., MySQL thread pool).
Systems
Runs physics, AI, rendering, and input handling in separate threads to improve performance and
4 Video Games
responsiveness.
Decodes audio/video in one thread while another handles UI updates and user interactions for smooth
5 Media Players
playback.
Scientific Parallelizes heavy computations (e.g., matrix operations, simulations) across multiple CPU cores using
6
Computing threads.
Real-Time
7 Uses threads to handle multiple sensors/actuators simultaneously (e.g., robotics, automotive systems).
Systems
File
8 Downloads multiple files concurrently by assigning each download to a separate thread.
Downloaders
Blockchain
9 Validates transactions and mines blocks in parallel using multiple threads for faster processing.
Networks
Notes : S14- Principles of Concurrency: Mutual Exclusion, Critical Section Problem
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
Multiple Processes:
Central to the design of modern Operating Systems is managing multiple processes–
Multiprogramming(multiple processes within a uniprocessor system)
Multiprocessing(multiple processes within a multiprocessor)
Distributed Processing(multiple processes executing on multiple ,distributed processing)
Big Issue is Concurrency – Managing the interaction of all of these processes
Principle of Concurrency:
Cooperating process is one that can affect or be affected by the other processes executing in system. Cooperating processes
may either share a logical address space or be allowed to share data only through files.
It is necessary because: information sharing, computation speed up, modularity.
When several process access and manipulate the same data at a time they are said to be concurrent processes.
Process synchronization is a mechanism to ensure a systematic sharing of resources amongst concurrent processes. So to
resolve concurrency we have to employ some protocols to ensure a systematic sharing of resources amongst concurrent
processes.
Concurrency affects a number of design issues : • Communication among processes • Sharing resources • Synchronization of
multiple processes • Allocation of processor time
Challenges in Concurrency
Challenge Description Real-World Impact Common Solutions
Race Occurs when multiple threads access shared data Crashes, corrupted data (e.g., bank Locks (mutexes), atomic
Conditions simultaneously, leading to unpredictable results. transactions updating wrong balances). operations, immutable data.
Frozen apps (e.g., database hangs
Two or more threads block forever, each waiting Timeouts, deadlock
Deadlocks when two queries lock each other’s
for the other to release a resource. detection, lock ordering.
tables).
Some threads get perpetually denied resources Slow performance (e.g., background Fair scheduling (round-
Starvation
due to unfair scheduling. tasks never completing). robin), priority adjustments.
Critical Section:
Critical section refers to the code segment of a process, whereby it accesses shared resources.
CS Problem:
Consider a set of concurrent processes {P0,P1,P2…,Pn-1} sharing a common resource R, through the execution of their critical
sections. These processes have to cooperate with each other to provide a solution to the critical section problem.
Requirements of a CS Solution:
An ideal critical section solution should meet the following three requirements:
Critical section solution: general structure
There are three main types of solutions to the critical section problem:
1. Software Solutions
These are algorithmic approaches that rely solely on programming logic—no special hardware instructions are needed.
Examples:
Peterson’s Algorithm (for two processes)
Dekker’s Algorithm
Lamport’s Bakery Algorithm (for multiple processes)
2. Hardware Solutions
These solutions use special hardware instructions that support atomic operations, which ensure mutual exclusion by preventing
interruption during key steps.
Examples:
Test-and-Set
Compare-and-Swap
Disable/Enable Interrupts (in uniprocessor systems)
Mutual exclusion
Mutual exclusion (often abbreviated to mutex) algorithms are used in concurrent programming to avoid the simultaneous
use of a common resource, such as a global variable, by pieces of computer code called critical sections.
A mutex is also a common name for a program object that negotiates mutual exclusion among threads, also called a lock.
Notes: S15-Software Solution to CS Problem: Dekker’s and Peterson’s Solutions
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
There are three main types of solutions to the critical section problem:
1. Software Solutions
These are algorithmic approaches that rely solely on programming logic—no special hardware instructions are needed.
Peterson’s Algorithm (for two processes)
Dekker’s Algorithm
2. Hardware Solutions
These solutions use special hardware instructions that support atomic operations, which ensure mutual exclusion by preventing
interruption during key steps.
Examples:
Test-and-Set
3. Operating System (OS) or Library-Based Solutions
Modern operating systems and languages offer high-level synchronization tools built on top of hardware or software primitives.
Examples:
Semaphores
Peterson’s Algorithm:
A Classic software-based solution to the critical-section problem known as Peterson’s solution.
May not work correctly on modern computer architecture. How ever, It provides a good algorithmic description of
solving the critical-section problem and illustrates some of the complexities involved in designing software that
addresses the requirements of mutual exclusion, progress, and bounded waiting.
Peterson’s solution is restricted to two processes that alternate execution between their critical sections and remainder
sections.
It ensures that if a process is in the critical section, no other process must be allowed to enter it. This property is termed
mutual exclusion.
If more than one process wants to enter the critical section, the process that should enter the critical region first must be
established. This is termed progress.
There is a limit to the number of requests that processors can make to enter the critical region, provided that a process has
already requested to enter and is waiting. This is termed bounding.
Pseudocode:
do {
flag[i] = true;
turn = j;
while (flag[j] && turn == j); // busy wait
// critical section
flag[i] = false;
// remainder section
} while (true);
Line of Code Explanation Purpose
do { Starts an infinite loop for process i. Ensures continuous execution of the process.
Indicates that process i wants to enter the critical section
flag[i] = true; Sets flag[i] to true.
(CS).
turn = j; Assigns the value j to turn. Gives priority to process j to ensure fairness.
Busy-waits while:
Ensures mutual exclusion by making process i wait if j is in
while (flag[j] && turn == j); 1. flag[j] is true (process j wants CS).
or wants CS.
2. turn == j (it's j's turn).
// critical section Placeholder for critical section code. Code here accesses shared resources safely.
flag[i] = false; Sets flag[i] to false. Signals that process i has exited the CS, allowing j to enter.
// remainder section Placeholder for non-critical code. Code here executes outside the CS.
} while (true); Closes the infinite loop. Ensures the process repeats indefinitely.
For example:
do { do {
flag[1] = true; flag[2] = true;
// Step 1: P₁ signals it wants to enter // Step 1: P₂ signals it wants to enter
turn = 2; turn = 1;
// Step 2: P₁ gives priority to P₂ // Step 2: P₂ gives priority to P₁
while (flag[2] && turn == 2); while (flag[1] && turn == 1);
// Step 3: Wait if P₂ is active & has priority // Step 3: Wait if P₁ is active & has priority
// CRITICAL SECTION (P₁ runs here) // CRITICAL SECTION (P₂ runs here)
flag[1] = false; flag[2] = false;
// Step 4: P₁ exits critical section // Step 4: P₂ exits critical section
// REMAINDER SECTION (Non-critical work) // REMAINDER SECTION (Non-critical
} while (TRUE); work)
} while (TRUE);
Dekker`s Algorithm:
If two processes attempt to enter a critical section at the same time, the algorithm will allow only one process in, based on whose
turn it is. If one process is already in the critical section, the other process will busy wait for the first process to exit. This is done
by the use of two flags, flag[0] and flag[1], which indicate an intention to enter the critical section and a turn variable which
indicates who has priority between the two processes.
Pseudocode
do {
flag[i] = true;
while (flag[j]) {
if (turn != i) {
flag[i] = false;
while (turn != i); // busy wait
flag[i] = true;
}
}
// critical section
turn = j;
flag[i] = false;
// remainder section
} while (true);
Line of Code Explanation Purpose
Ensures the process continuously attempts to enter
do { Starts an infinite loop for process i
critical section
Declares process i's intent to enter the critical section
flag[i] = true; Sets flag[i] to true
(CS)
Prevents both processes from entering CS
while (flag[j]) { Checks if process j also wants to enter CS
simultaneously
if (turn != i) { Verifies if it's not process i's turn Ensures fairness by yielding to the other process
flag[i] = false; Temporarily lowers process i's flag Allows process j to proceed if it's its turn
while (turn != i); Busy-waits until turn becomes i Ensures process i waits its turn
flag[i] = true; Re-raises process i's flag Re-attempts to enter CS after waiting
} Ends the yield condition block
} Exits the busy-wait loop when safe to proceed
// critical section Protected code section (accesses shared resources) Only one process executes this at a time
turn = j; Passes the turn to process j Ensures fairness by alternating turns
flag[i] = false; Lowers process i's flag Signals CS exit, allowing other processes to enter
// remainder section Non-critical code (doesn't access shared resources) Process performs other tasks
} while (true); Restarts the infinite loop Process continues operation
For example:
Process 1 (P₁) Process 2 (P₂)
do { do {
flag[1] = true; flag[2] = true;
// Step 1: P₁ signals it wants to enter // Step 1: P₂ signals it wants to enter
while (flag[2]) while (flag[1])
{ // Step 2: If P₂ is active, check turn { // Step 2: If P₁ is active, check turn
if (turn == 2) if (turn == 1)
{ // Step 3: If it's P₂'s turn, yield { // Step 3: If it's P₁'s turn, yield
flag[1] = false; flag[2] = false;
// Step 4: P₁ resets its flag // Step 4: P₂ resets its flag
while (turn == 2); while (turn == 1);
// Step 5: Wait until turn changes // Step 5: Wait until turn changes
flag[1] = true; flag[2] = true;
// Step 6: P₁ tries again // Step 6: P₂ tries again
} }
} }
// CRITICAL SECTION (P₁ runs here) // CRITICAL SECTION (P₂ runs here)
turn = 2; // Step 7: Pass turn to P₂ turn = 1; // Step 7: Pass turn to P₁
flag[1] = false; // Step 8: P₁ exits critical section flag[2] = false; // Step 8: P₂ exits critical section
// REMAINDER SECTION (Non-critical work) // REMAINDER SECTION (Non-critical work)
} while (TRUE); } while (TRUE);
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
There are three main types of solutions to the critical section problem:
1. Software Solutions
These are algorithmic approaches that rely solely on programming logic—no special hardware instructions are needed.
Examples:
Peterson’s Algorithm (for two processes)
Dekker’s Algorithm
2. Hardware Solutions
These solutions use special hardware instructions that support atomic operations, which ensure mutual exclusion by preventing
interruption during key steps.
Examples: Test-and-Set
Software solutions already covered in session 15. In this session we are going to explore Semaphores and Test & Set operations.
Semaphore Operations
Implementation of wait:
wait (S){
value--;
if (value < 0) {
add this process to waiting queue
block(); }
}
Implementation of signal:
Signal (S){
value++;
if (value <= 0) {
remove a process P from the waiting queue
wakeup(P); }
}
Counting Semaphore:
Semaphores which allow an arbitrary resource count are called counting semaphores(can have possible values more than two)
Example:
Semaphore S = 5; // 5 resources available
Process P1:
wait(S); // Decrements S (if S > 0)
// Uses resource
signal(S); // Releases resource (increments S)
Limitations of Semaphores
Deadlocks: If processes wait in a circular manner.
Starvation: Some processes may never get access.
Priority Inversion: A high-priority process may wait for a low-priority one.
Real-World Applications
Operating Systems: Process scheduling, memory management.
Database Systems: Transaction locking.
Embedded Systems: Resource allocation in RTOS.
Traffic Control: Managing shared lanes in automated systems.
Hardware Solutions:
There are three algorithms in the hardware approach of solving Process Synchronization problem: Test and Set ,Swap ,Unlock and
Lock
Introduction to Test-and-Set
The Test-and-Set (TAS) operation is a hardware-supported atomic instruction used to implement synchronization mechanisms
like semaphores. It ensures mutual exclusion in concurrent programming by allowing only one process/thread to access a critical
section at a time.
Key Properties:
Atomicity: The operation executes without interruption (CPU guarantees no interference).
Used for Mutual Exclusion: Helps prevent race conditions in multi-threaded environments.
Hardware Implementation: Typically provided by the CPU (e.g., xchg in x86, ldrex/strex in ARM).
while(1){
while (TestAndSet(lock));
critical section
lock = false;
remainder section
}
How Test-and-Set Works
The characteristics of this synchronization mechanism are-
It ensures mutual exclusion.
It is deadlock free.
It does not guarantee bounded waiting and may cause starvation.
It suffers from spin lock.
It is not architectural neutral since it requires the operating system to support test-and-set instruction.
It is a busy waiting solution which keeps the CPU busy when the process is actually waiting.
// Binary semaphore using mutex **C program that simulates Test-and-Set operations
typedef struct { #include <stdio.h>
pthread_mutex_t mutex; #include <pthread.h>
int value; #include <stdatomic.h>
} Semaphore;
// Atomic Test-and-Set lock
void init(Semaphore *s, int value) { typedef struct {
s->value = value; atomic_flag flag;
pthread_mutex_init(&s->mutex, NULL); } TASLock;
}
void init_lock(TASLock *lock) {
void wait(Semaphore *s) { atomic_flag_clear(&lock->flag);
pthread_mutex_lock(&s->mutex); }
while (s->value <= 0) {
pthread_mutex_unlock(&s->mutex); void lock(TASLock *lock) {
usleep(1000); // Prevent busy-waiting while (atomic_flag_test_and_set(&lock->flag)) {
pthread_mutex_lock(&s->mutex); usleep(1000); // Reduce CPU usage
} }
s->value--; }
pthread_mutex_unlock(&s->mutex);
} void unlock(TASLock *lock) {
atomic_flag_clear(&lock->flag);
void signal(Semaphore *s) { }
pthread_mutex_lock(&s->mutex);
s->value++; // Example usage
pthread_mutex_unlock(&s->mutex); TASLock tas_lock;
}
void* thread_func(void* arg) {
// Example usage lock(&tas_lock);
Semaphore sem; printf("Thread %ld entered critical section\n", (long)arg);
sleep(1); // Simulate work
void* thread_func(void* arg) { unlock(&tas_lock);
wait(&sem); return NULL;
printf("Thread %ld entered critical section\n", (long)arg); }
sleep(1); // Simulate work
signal(&sem); int main() {
return NULL; init_lock(&tas_lock);
} pthread_t t1, t2;
pthread_create(&t1, NULL, thread_func, (void*)1);
int main() { pthread_create(&t2, NULL, thread_func, (void*)2);
init(&sem, 1); // Binary semaphore (mutex) pthread_join(t1, NULL);
pthread_t t1, t2; pthread_join(t2, NULL);
pthread_create(&t1, NULL, thread_func, (void*)1); return 0;
pthread_create(&t2, NULL, thread_func, (void*)2); }
Notes: S17-Classical problems of concurrency : Dining Philosopher’s problem
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
Key Challenges:
1. Deadlock:
If all philosophers pick up their left chopstick simultaneously, none can pick up the right one.
Result: All philosophers starve (infinite waiting).
(Like threads holding partial resources and waiting indefinitely.)
2. Starvation
Some philosophers may never get both chopsticks due to unfair scheduling.
(Like low-priority threads never getting CPU time.)
3. Race Conditions
Uncontrolled access to chopsticks → data corruption (e.g., two philosophers grabbing the same
chopstick).
Step-by-Step Explanation
Code Explanation
while (true) { ... } Infinite loop where each philosopher continuously thinks and eats
#define NUM_PHILOSOPHERS 5
int main() {
pthread_t philosophers[NUM_PHILOSOPHERS];
int ids[NUM_PHILOSOPHERS];
// Initialize semaphores
sem_init(&mutex, 0, 1);
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
sem_init(&chopsticks[i], 0, 1); // Each chopstick starts as available (1)
}
// Clean up semaphores
sem_destroy(&mutex);
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
sem_destroy(&chopsticks[i]);
}
return 0;
}
void *philosopher(void *arg) {
int id = *(int *)arg;
while (1) {
printf("Philosopher %d is thinking...\n", id);
sleep(1); // Thinking time
// Use mutex to prevent deadlock (only one philosopher picks up chopsticks at a time)
sem_wait(&mutex);
// Eat
eat(id);
sem_post(&chopsticks[(id + 1) % NUM_PHILOSOPHERS]);
printf("Philosopher %d put down right chopstick (%d)\n", id, (id + 1) % NUM_PHILOSOPHERS);
}
}
void eat(int id) {
printf("Philosopher %d is eating...\n", id);
sleep(2); // Simulate eating time
}
Some other Classical problems of concurrency
2. Producer Consumer problem (Bounded buffer problem):
The Producer-Consumer problem is a classical multi-process synchronization problem, that is we are trying to achieve
synchronization between more than one process. It is also known as Bounded Buffer Problem in (multi-process synchronization
problem). Consider two processes Producer and Consumer , who share common, fixed size buffer. Producer puts information into
the buffer. Consumer consume this information from buffer. Trouble arises when the producer wants to put a new item in the buffer,
but it is already full. And consumer wants to remove a item from buffer, but it is already empty.
Problem statement:
1. A producer must not overwrite a full buffer.
2. A consumer must not consume an empty buffer.
3. Producer and consumer must access buffers in a mutually exclusive manner.
4. Information must be consumed in the same order in which it is put in to buffers(FIFO)
Sleeping barber problem:
Problem statement:
1. Hair cutting saloon has N chair in its waiting room and 1 chair in barber’s cabin for giving hair cut
2. If there is no customer in the saloon, barber goes to sleep.
3. When a customer arrives at the saloon, one of the following happens
(a). If there is no customer, customer walks in to cabin, wakes up the barber, and starts getting haircut.
(b)When customer is already getting hair cut in the cabin then the customer looks for an empty chair in the
waiting room. if yes then he occupy and wait if no he walks away without haircut.
4. Order will be FCFS.
Notes : S18- Deadlock System Models
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
Introduction to Deadlock
Deadlock is a situation in which two or more processes are unable to proceed because each is waiting for the other to release a
resource. Deadlock is a significant problem in operating systems because it can lead to system halts and resource wastage.
Understanding deadlock is essential for designing efficient and reliable systems.
Imagine four cars arriving at a four-way intersection at the same time, each coming from a different direction. Each driver politely
waits for the others to go first, but no one moves. As a result, all cars remain stuck indefinitely. This is called Deadlock situation.
The processes are the cars. The resources are the spaces occupied by the cars
In other words A deadlock consists of a set of blocked processes, each holding a resource and waiting to acquire a resource held
by another process in the set.
For Example: two processes each want to record a scanned document on a CD. process 1 requests for scanner & gets it. process 2
requests for CD writer & gets it. process 1 requests CD writer but is blocked. process 2 requests scanner but is blocked. At this
point both processes are blocked and will remain so forever, this situation is called a deadlock
For example, in the below diagram, Process 1 is holding Resource 1 and waiting for resource 2 which is acquired by process 2,
and process 2 is waiting for resource 1.
System model:
In the context of deadlocks, "System Models" refer to the formal representations of how processes, resources, and their interactions
are structured in an operating system. These models help in analyzing, detecting, preventing, and resolving deadlocks systematically.
Purpose of System Models in Deadlock
System models provide:
A theoretical framework to understand deadlock conditions.
Mathematical representations (like graphs) to detect deadlocks.
Algorithms (e.g., Banker’s Algorithm) to avoid deadlocks.
Strategies for prevention, avoidance, and recovery.
The bullet symbol within the resource is known as instances of that resource.
Instance of resources means identical resources of same type.
There exist 2 kinds of edges.
i) Request edge: Whenever a process request for resources then it is called a request edge. A request edge is drawn from the process
to resource.
ii) Allocation / assignment edge : Whenever a resource is allocated to a process the request edge is converted to an assignment
edge from the instance of the resource to the process.
The sets P, R, E:
P= {P1, P2, P3}
R= {R1, R2, R3, R4}
E= {P1 ->R1, P2 ->R3, R1 ->P2, R2 ->P2, R2 ->P1, R3 ->P3}
This graph consists of a set of vertices V and a set of edges E. the set of vertices V is partitioned into 2 different types of
nodes: P = {P1, P2….Pn}, the set consisting of all the active processes in the system. R= {R1, R2….Rm}, the set consisting
of all resource types in the system.
A directed edge from process Pi to resource type Rj is denoted by Pi ->Rj. It signifies that process Pi has requested an
instance of resource type Rj and is currently waiting for that resource.
A directed edge from resource type Rj to process Pi is denoted by Rj ->Pi, it signifies that an instance of resource type Rj
has been allocated to process Pi.
A directed edge Pi ->Rj is called a requested edge.
A directed edge Rj->Pi is called an assignment edge.
We represent each process Pi as a circle, each resource type Rj as a rectangle.
Since resource type Rj may have more than one instance. We represent each such instance as a dot within the rectangle. A
request edge points to only the rectangle Rj.
An assignment edge must also designate one of the dots in the rectangle.
When process Pi requests an instance of resource type Rj, a request edge is inserted in the resource allocation graph.
When this request can be fulfilled, the request edge is instantaneously transformed to an assignment edge.
When the process no longer needs access to the resource, it releases the resource, as a result, the assignment edge is deleted.
NOTE:-
If the RAG contains NO CYCLE , then there is NO DEADLOCK in the system.
If the RAG contains a CYCLE, then there MAY BE A DEADLOCK in the system.
If the resources have exactly one instance then a cycle indicates a deadlock.
If the resources have multiple instances per resource then a cycle indicates that “there may be deadlock”.
1.Mutual exclusion:
At least one resource type in the system which can be used in non-shareable mode (i.e. one at a time) for example
printer.
Because many resources can be shared by more than one process at a time (e.g., Memory location).
In the diagram below, there is a single instance of Resource 1 and it is held by Process 1 only.
3.No pre-emption:
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.
4. Circular wait:
Two or more processes must form a circular chain in which each process is waiting for a resource that is held by the next member
of the chain.
All four of these conditions must hold simultaneously in a system for a deadlock to occur.
1. Four-Way Traffic Four cars arrive at an intersection simultaneously, each waiting for another to move first. Since
Gridlock none yield, all remain stuck indefinitely (circular wait + mutual exclusion).
2. Dining Philosophers Five philosophers share forks, but each need two to eat. If all pick up one fork and wait, no one
Problem can proceed (hold & wait + circular dependency).
3. Office Printer- Employee A holds the printer and waits for the scanner, while Employee B holds the scanner
Scanner Deadlock and waits for the printer. Both are stuck (resource contention).
4. Construction Crane Two cranes need each other’s space to lift a beam but neither moves, halting progress (mutual
Standoff exclusion + no Preemption).
5. Bank Transaction Account X waits for a transfer from Account Y, which is itself waiting for X to release a lock
Deadlock (circular dependency).
6. Team Project Alice won’t submit code until Bob reviews it, but Bob won’t review until Alice submits (cyclic
Dependency wait).
7. Single-Track Train Two trains face each other on a single track, each waiting for the other to reverse (no
Deadlock Preemption).
8. Video Game Loot Player 1 holds a sword and waits for a shield (held by Player 2), who waits for the sword (hold
Standoff & wait).
9. Kitchen Knife & Chef A holds a knife and waits for a cutting board (held by Chef B), who waits for the knife (all
Cutting Board 4 conditions met).
Notes : S19-Deadlock Prevention and Avoidance
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
Deadlock Prevention
Deadlocks can be prevented from occurring by preventing one of the necessary four conditions. i.e. mutual exclusion, hold and wait,
no pre-emption and circular wait. If one of the conditions can be prevented from occurring then deadlock will not occur.
Eliminate Mutual Exclusion
It is not possible to dis-satisfy the mutual exclusion because some resources, such as the tape drive and printer, are inherently non-
shareable.
Eliminate Hold and wait
Allocate all required resources to the process before the start of its execution, this way hold and wait condition is eliminated but it
will lead to low device utilization. for example, if a process requires printer at a later time and we have allocated printer before the
start of its execution printer will remain blocked till it has completed its execution. The process will make a new request for resources
after releasing the current set of resources. This solution may lead to starvation.
Eliminate No Preemption
Preempt resources from the process when resources required by other high priority processes.
Eliminate Circular Wait
Each resource will be assigned with a numerical number. A process can request the resources increasing/decreasing. order of
numbering. For Example, if P1 process is allocated R5 resources, now next time if P1 ask for R4, R3 lesser than R5 such request
will not be granted, only request for resources more than R5 will be granted.
Deadlock Avoidance
To avoid deadlock it is required to keep the additional information of the process i.e. the operating System will be given prior
information about the process such that
- Which process will request for which resource.
- At what time and for how long time.
So that the operating System find out a sequence of execution. If all the process execute in the sequence then system will never
enter into a deadlock state.
SAFE STATE: A state is safe if the system can allocate the available resources to each process in same order and still avoid deadlock.
SAFE SEQUENCE: A sequence of processes (P1, P2, and P3----------Pn) is a safe sequence if the available resources can be allowed
to the processes in that sequence and avoid deadlock. If no safe sequence exists the system is said to be unsafe state. An unsafe
state may lead to a deadlock.
Banker’s algorithm consists of two parts. - Safety algorithm & - Resource request algorithm.
Safety algorithm
The safety algorithm is used to determine whether a system is in safe state or not.
The algorithm uses several data structures such as vector & matrixes. Ex. Let in a system there are ‘n’ processes and ‘m’ resources.
1) AVAILABLE: - It is an array/vector of length ‘m’ indicates the number of available resources of each type. If available [J] =k
means there are k instances of resource type Rj available.
2) MAX: - It is am matrix defines the maximum demand of each process. If max [i,j]=k, then the process Pi may request at most k
instances of resource type Rj.
3) ALLOCATION: - It is an n*m matrix defines the number of resources of each type currently allocated to each process. Ex. If
allocation [i,j]=k, then process Pi is currently allocated k instances of resource to Rj.
4) NEED: - It is an n*m matrix indicates the remaining resources need each process if need [i,j]=k, then the process Pi may need k
more instances of resource type Rj to complete its task. Need[i,j]=max[i,j] – allocation[i,j].
Algorithm: 1.Let Work and Finish be vectors of length m and n, respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1
2.Find an i such that both:
(a) Finish [i] = false
(b) Needi ≤ Work
If no such i exists, go to step 4
3.Work = Work + Allocationi
Finish[i] = true
go to step 2
4.If Finish [i] == true for all i, then the system is in a safe state
Resource request algorithm
The resource request algorithm is used determine whether or not a request generated by a process for a resource, would lead the
system to an unsafe state.
Resource-Request Algorithm for Process Pi
Requesti = request vector for process Pi. If Requesti [j] = k then process Pi wants k instances of resource type Rj
1.If Requesti ≤ Needi go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim
2.If Requesti ≤ Available, go to step 3. Otherwise Pi must wait, since resources are not available
3.Pretend to allocate requested resources to Pi by modifying the state as follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
If safe ⇒ the resources are allocated to Pi
If unsafe ⇒ Pi must wait, and the old resource-allocation state is restored
Problem:
Considering a system with 5 processes P0 to P4 and three resource types A,B,C. Resource type A has 10 instances, B has 5
instances ,C has 7 instances. Suppose at time t0 following snapshot of the system has been taken:
Process Allocation Max Available
ABC ABC ABC
P0 0,1,0 7,5,3 3,3,2
P1 2,0,0 3,2,2
P2 3,0,2 9,0,2
P3 2,1,1 2,2,2
P4 0,0,2 4,3,3
Solution:
(i) Matrix:
Need[i][j]
Process max[i][j]-allocation[i][j]
A B C
P0 7,5,3-0,1,0 7 4 3
P1 3,2,2-2,0,0 1 2 2
P2 9,0,2-3,0,2 6 0 0
P3 2,2,2-2,1,1 0 1 1
P4 4,3,3-0,0,2 4 3 1
(ii) Is the system in safe state? If yes, then what is the safe sequence?
Solution: Applying safety algorithm: Iteration -1: (R=Right, W=Wrong)
Process 1. 2. 3. 4.
(Pi) Initialize: Find an i such that Work=work+allocationi If finish[i]=true for all i
work=Available both Finish[i]=true Then the system is in
Finish[i] =false Finish[i]=false Go to step (2) safe state.
for i=1,2,3,…n. Needi<=work are
right then go to step
3.
If no such i exists,
go to step 4
After iteration-1, P0,P2 are waiting. Now for iteration two we have current available(745) in this case either P0 or P2 can
be kept in safe sequence. In this particular example we are taking P2 first for consideration:
Iteration -2:
Process 1. 2. 3. 4.
(Pi) Initialize: Find an i such that both Work=work+allocationi If finish[i]=true for all i
work=Available Finish[i]=false Finish[i]=true Then the system is in
Finish[i] =false Needi<=work Go to step (2) safe state.
for i=1,2,3,…n. If no such i exists, go to
step(4)
(iii)What will happen if process P1 requests one additional instance of resource type A and two instance of C.(1,0,2).
Solution:
Since P1 request some additional instance such that requesti=(1,0,2) We have available (3, 3, 2) now 1,0,2<=3,3,2 yes of course
request may be granted.
Now applying Resource request algorithm:
1. If requesti<=Needi 1,0,2<=1,2,2 Yes
Go to step 2; otherwise raise an error condition, since the process has exceeded its maximum claim.
2. If requesti<=available 1,0,2<=3,3,2 Yes
Go to step 3; otherwise, Pi must wait, since the resources are not available.
3. Have the system pretend to have allocated the requested resources to process Pi by modifying the state as follows:
Available=available-requesti Available=3,3,2-1,0,2=2,3,0
Allocationi= Allocationi+ requesti Allocation1=2,0,0+1,0,2=3,0,2
Needi= Needi- requesti Need1=1,2,2-1,0,2=0,2,0
If resulting resource allocation state is safe the transaction is completed and process P i is allocated its resources .if not
then Pi must wait for requesti and the old R-A state is restored.
Now new snapshot will be
Process Allocation need Available
ABC ABC ABC
P0 0,1,0 7,4,3 2,3,0
P1 3,0,2 0,2,0
P2 3,0,2 6,0,0
P3 2,1,1 0,1,1
P4 0,0,2 4,3,1
*Now we check again it`s safety sequence and got that it is safe to grant the request P1 immediately .Applying safety
algorithm we got the safe sequence<P1, P3, P4, P0, P2>.
(iv) If a request (3, 3, 0) by process P4 arrives in the state defined by (iii), can it be granted immediately?
Solution: Available=2,3,0, Request=3,3,0
3,3,0<=2,3,0 False so request of P4 can’t be granted as resources are unavailable.
(v)If a request (0, 2, 0) by process P0 arrives then check whether it is granted or not?
Solution: Available=2,3,0, Request=0,2,0
0,2,0<=2,3,0 True so request of P0 can be granted as resources are available. And new system will be like:
Available=available-requesti Available=2,3,0-0,2,0=2,1,0
Allocationi= Allocationi+ requesti Allocation1=0,1,0+0,2,0=0,3,0
Needi= Needi- requesti Need1=7,4,3-0,2,0=7,2,3
Process Allocation need Available
ABC ABC ABC
P0 0,3,0 7,2,3 2,1,0
P1 3,0,2 0,2,0
P2 3,0,2 6,0,0
P3 2,1,1 0,1,1
P4 0,0,2 4,3,1
Applying safety algorithm on this system found that system will be in unsafe state.
Question:
Consider the following snapshot for the banker's algorithm:
Question:
Notes : S20- Recovery from Deadlock
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
Process Termination
This method used to recover from deadlock. We use one of two methods for process termination. a. Abort all deadlock processes b.
Abort one by one process until the deadlock cycle is broken
Resource Preemption
There are three methods to eliminate deadlocks using resource pre-emption. They are- Selection of a victim. Roll back
.
Some Real-life scenarios related to deadlock recovery
# Scenario Deadlock Situation Possible Recovery Strategies OS Equivalent
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
IPC:
Processes in system can be independent or cooperating.
Independent processes cannot affect or be affected by the execution of another process.
Cooperating processes can affect or be affected by the execution of another process.
Inter-process communication (IPC) is a set of methods for the exchange of data among multiple threads in one or more processes.
Processes may be running on one or more computers connected by a network. IPC may also be referred to as inter-thread
communication and inter-application communication.
Why IPC
There are several reasons for providing an environment that allows process cooperation:
Information sharing: Since several users may be interested in the same piece of information (for instance, a shared file), we must
provide an environment to allow concurrent access to these types of resources.
Computation speedup: If we want a particular task to run faster, we must break it into subtasks, each of which will be executing
in parallel with the others. Such a speedup can be achieved only if the computer has multiple processing elements (such as CPUS
or I/O channels).
Modularity: We may want to construct the system in a modular fashion, dividing the system functions into separate processes or
threads.
Convenience: Even an individual user may have many tasks on which to work at one time. For instance, a user may be editing,
printing, and compiling in parallel.
Method of Communication:
The communication between these processes can be seen as a method of co-operation between them. Processes can communicate
with each other using these two ways:
a. Message Passing (Process A send the message to Kernel and then Kernel sends that message to Process B)
b. Shared Memory (Process A put the message into Shared Memory and then Process B read that message from Shared
Memory)
Notes : S22- Memory Management: Basic Bare Machine, Resident Monitor
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
All the programs are loaded in the main memory for execution. Sometimes the complete program is loaded into the memory, but
sometimes a certain part or routine of the program is loaded into the main memory only when it is called by the program, this
mechanism is called Dynamic Loading, which enhances the performance. Also, at times one program is dependent on some other
program. In such a case, rather than loading all the dependent programs, the CPU links the dependent programs to the main executing
program when required. This mechanism is known as Dynamic Linking.
Suppose the base is at 14000, then an attempt by the user to address location 0 is relocated dynamically to 14000; thus access to
location 356 is mapped to 14356.
It is important to note that the user program never sees the real physical addresses.
Swapping
Swapping is a memory management technique and is used to temporarily remove the inactive programs from the main memory of
the computer system. Any process must be in the memory for its execution, but can be swapped temporarily out of memory to a
backing store and then again brought back into the memory to complete its execution. Swapping is done so that other processes get
memory for their execution.
Due to the swapping technique performance usually gets affected, but it also helps in running multiple and big processes in
parallel. The swapping process is also known as a technique for memory compaction. Basically, low priority processes may be
swapped out so that processes with a higher priority may be loaded and executed.
The procedure by which any process gets removed from the hard disk and placed in the main memory or RAM commonly known
as Swap In.
On the other hand, Swap Out is the method of removing a process from the main memory or RAM and then adding it to the Hard
Disk.
Advantages of Swapping
The advantages/benefits of the Swapping technique are as follows:
1. The swapping technique mainly helps the CPU to manage multiple processes within a single main memory.
2. This technique helps to create and use virtual memory.
3. With the help of this technique, the CPU can perform several tasks simultaneously. Thus, processes need not wait too long
before their execution.
4. This technique is economical.
5. This technique can be easily applied to priority-based scheduling in order to improve its performance.
Disadvantages of Swapping
The drawbacks of the swapping technique are as follows:
1. There may occur inefficiency in the case if a resource or a variable is commonly used by those processes that are
participating in the swapping process.
2. If the algorithm used for swapping is not good then the overall method can increase the number of page faults and thus
decline the overall performance of processing.
3. If the computer system loses power at the time of high swapping activity then the user might lose all the information related
to the program.
In the early days of computing, before the development of modern operating systems, the execution of programs was done directly
on the hardware. This was made possible by two fundamental components: Bare Machine and Resident Monitor.
Bare Machine
A Bare Machine is a computer system that operates directly with its hardware. It executes programs in machine language without
the need for an operating system. Before the development of operating system, the only drawback was that it accepted instructions
in only machine language due to which only people with sufficient knowledge about Computer field are able to operate a computer.
Hence, after the development of the operating system, Bare machine is considered as inefficient.
Working of Bare Machine
The processor executes machine language instructions provided by the user.
There is no abstraction between the user and the machine. Hence, the system is highly dependent on the knowledge of
machine code.
Each instruction is processed by the hardware without the help of an operating system which results in a basic, raw
computing environment.
Advantages of Bare Machine
1. Direct Control: Users can directly control the hardware without any interference from an operating system which can be
beneficial in low-level tasks or specialized applications.
2. Efficiency in Simple Tasks: For basic, single-task operations, Bare Machines can be faster than using an operating system
as there is no overhead.
3. Low Resource Requirement: Bare Machines do not require resources or memory needed by an operating system which
makes them useful in environments with limited resources.
Disadvantages of Bare Machine
1. Lack of User-Friendliness: Bare Machines only accept machine language. Users must have a understanding of low-level
programming.
2. No Multitasking: Unlike systems with operating systems, Bare Machines cannot handle multiple tasks simultaneously
which makes them inefficient for most general-purpose use cases.
3. Limited Scope: Bare Machines are inefficient for complex applications as they lack the abstraction and multitasking
features provided by modern operating systems.
Resident Monitor
The Resident Monitor is program that runs on Bare Machines. The resident monitor works like a primitive operating system that
controls the instructions and performs all necessary functions such as job scheduling, memory management, and interrupt
processing.
A computer without any pre-installed A primitive OS that provides basic control and
Definition
software or OS. management.
Control Direct hardware access (no OS layer). Managed by a simple supervisor program (monitor).
Memory
No memory protection; user manages all. Basic memory allocation & protection.
Management
I/O Operations Direct hardware programming required. Simplified via monitor-provided routines.
Error Handling No safeguards; crashes are likely. Basic error detection & recovery.
Example Systems ENIAC, early mainframes. IBM 360, early batch-processing systems.
Use Case Research, low-level development. Early business & scientific computing.
Notes : S23- Multiprogramming with Fixed Partitions
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
The main memory must accommodate both operating system and various user processes. Generally, the main memory is divided
into 2 partitions. Operating system. & Application program/user processes. Multiprogramming is the ability of a system to run
multiple processes concurrently, improving CPU utilization and system throughput.
Generally, there are two methods used for partitioning the memory allocation.
1. Contiguous memory allocation
2. Non-Contiguous memory allocation
Fixed Partitioning:
This is the oldest and simplest technique used to put more than one process in the main memory. In this partitioning, the number of
partitions (non-overlapping) in RAM is fixed but the size of each partition may or may not be the same. As it is
a contiguous allocation, hence no spanning is allowed. Here partitions are made before execution or during system configure.
As illustrated in above figure, first process is only consuming 1MB out of 4MB in the main memory. Hence, Internal
Fragmentation in first block is (4-1) = 3MB. Sum of Internal Fragmentation in every block = (4-1)+(8-7)+(8-7)+(16-14)=
+1+1+2=7MB. Suppose process P5 of size 7MB comes. But this process cannot be accommodated in spite of available free
space because of contiguous allocation (as spanning is not allowed). Hence, 7MB becomes part of External Fragmentation.
Example: Consider the following memory blocks (in KB): Block sizes: 100 KB, 500 KB, 200 KB, 300 KB, 600 KB. Processes
arrive needing memory as follows: Process sizes: 212 KB, 417 KB, 112 KB, 426 KB. Apply First Fit, Best Fit, Worst Fit and
Next Fit algorithms to determine which process gets allocated to which block. Show the allocation and the remaining sizes of the
memory blocks after allocation.
Answer:
Memory Blocks (Initial):
Block 1: 100 KB
Block 2: 500 KB
Block 3: 200 KB
Block 4: 300 KB
Block 5: 600 KB
Processes (In Order):
P1: 212 KB
P2: 417 KB
P3: 112 KB
P4: 426 KB
1. First Fit
Approach: Allocate the first block that is large enough for the process.
Allocation Steps:
P1 (212 KB) → Block 2 (500 KB) → Allocated (500 - 212 = 288 KB left)
P2 (417 KB) → Block 5 (600 KB) → Allocated (600 - 417 = 183 KB left)
P3 (112 KB) → Block 3 (200 KB) → Allocated (200 - 112 = 88 KB left)
P4 (426 KB) → No suitable block remaining
Result:
Process Allocated Block Remaining Block Size
P1 Block 2 (500 KB) 288 KB
P2 Block 5 (600 KB) 183 KB
P3 Block 3 (200 KB) 88 KB
P4 Not allocated -
2. Best Fit
Approach: Allocate the smallest block that is large enough for the process.
Allocation Steps:
P1 (212 KB) → Best fit: Block 4 (300 KB) → Allocated (300 - 212 = 88 KB left)
P2 (417 KB) → Best fit: Block 2 (500 KB) → Allocated (500 - 417 = 83 KB left)
P3 (112 KB) → Best fit: Block 3 (200 KB) → Allocated (200 - 112 = 88 KB left)
P4 (426 KB) → Best fit: Block 5 (600 KB) → Allocated (600 - 426 = 174 KB left)
Result:
Process Allocated Block Remaining Block Size
P1 Block 4 (300 KB) 88 KB
P2 Block 2 (500 KB) 83 KB
P3 Block 3 (200 KB) 88 KB
P4 Block 5 (600 KB) 174 KB
3. Worst Fit
Approach: Allocate the largest available block that fits the process.
Allocation Steps:
P1 (212 KB) → Worst fit: Block 5 (600 KB) → Allocated (600 - 212 = 388 KB)
P2 (417 KB) → Worst fit: Block 5 (388 KB) no longer sufficient → next is Block 2 (500 KB) → Allocated (500 - 417 =
83 KB)
P3 (112 KB) → Worst fit: Block 5 (388 KB) → Allocated (388 - 112 = 276 KB)
P4 (426 KB) → No suitable block remaining
Result:
Process Allocated Block Remaining Block Size
P1 Block 5 (600 KB) 388 KB → 276 KB after P3
P2 Block 2 (500 KB) 83 KB
P3 Block 5 276 KB remaining
P4 Not allocated -
Notes : S24- Multiprogramming with Variable Partitions
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
Variable Partitioning –
It is a part of the Contiguous allocation technique. It is used to alleviate the problem faced by Fixed Partitioning. In contrast with
fixed partitioning, partitions are not made before the execution or during system configure. Various features associated with variable
Partitioning-
1. Initially, RAM is empty and partitions are made during the run-time according to the process’s need instead of partitioning
during system configure.
2. The size of the partition will be equal to the incoming process.
3. The partition size varies according to the need of the process so that the internal fragmentation can be avoided to ensure
efficient utilization of RAM.
4. The number of partitions in RAM is not fixed and depends on the number of incoming processes and Main Memory’s size.
Dynamic partitioning tries to overcome the problems caused by fixed partitioning. In this technique, the partition size is not declared
initially. It is declared at the time of process loading. The first partition is reserved for the operating system. The remaining space is
divided into parts. The size of each partition will be equal to the size of the process. The partition size varies according to the need
of the process so that the internal fragmentation can be avoided.
Memory is divided into fixed-sized partitions. Memory is allocated dynamically in varying sizes.
Only one process can be placed in each partition. A process is allocated a chunk of free memory as needed.
Inefficient memory utilization due to internal More efficient memory utilization with less internal
fragmentation. fragmentation.
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
Each process is mainly divided into parts where the size of each part is the same as the page size.
There is a possibility that the size of the last part may be less than the page size.
Pages of a process are brought into the main memory only when there is a requirement otherwise they reside in the
secondary storage.
One page of a process is mainly stored in one of the frames of the memory. Also, the pages can be stored at different
locations of the memory but always the main priority is to find contiguous frames.
Let us now cover the concept of translating a logical address into the physical address:
Before moving on further there are some important points to note:
The CPU always generates a logical address.
In order to access the main memory always a physical address is needed.
Every address generated by CPU mainly consists of two parts:
1. Page Number(p)
2. Page Offset (d)
where, Page Number is used as an index into the page table that generally contains the base address of each page in the physical
memory. Page offset is combined with base address in order to define the physical memory address which is then sent to the memory
unit. If the size of logical address space is 2 raised to the power m and page size is 2 raised to the power n addressing units then
the high order m-n bits of logical address designates the page number and the n low-order bits designate the page offset.
The logical address is as follows:
where p indicates the index into the page table, and d indicates the displacement within the page. The Page size is usually defined
by the hardware. The size of the page is typically the power of 2 that varies between 512 bytes and 16 MB per page.
Following steps are followed to translate logical address into physical address-
Step-01:CPU generates a logical address consisting of two parts-Page Number, Page Offset.
Page Number specifies the specific page of the process from which CPU wants to read the data.
Page Offset specifies the specific word on the page that CPU wants to read.
Step-02: For the page number generated by the CPU, Page Table provides the corresponding frame number (base address of the
frame) where that page is stored in the main memory.
Step-03: The frame number combined with the page offset forms the required physical address.
Frame number specifies the specific frame where the required page is stored.
Page Offset specifies the specific word that has to be read from that page.
Paging Example
In order to implement paging, one of the simplest methods is to implement the page table as a set of registers. As the size of registers
is limited but the size of the page table is usually large thus page table is kept in the main memory.
There is no External fragmentation caused due to this scheme; Any frame that is free can be allocated to any process that needs it.
But the internal fragmentation is still there.
If any process requires n pages then at least n frames are required.
The first page of the process is loaded into the first frame that is listed on the free-frame list, and then the frame
number is put into the page table.
The frame table is a data structure that keeps the information of which frames are allocated or which frames are available and many
more things. This table mainly has one entry for each physical page frame.
The Operating system maintains a copy of the page table for each process in the same way as it maintains the copy of the instruction
counter and register contents. Also, this copy is used to translate logical addresses to physical addresses whenever the operating
system maps a logical address to a physical address manually.
This copy is also used by the CPU dispatcher in order to define the hardware page table whenever a process is to be allocated to the
CPU.
Multilevel paging:
Multilevel paging is a paging scheme where there exists a hierarchy of page tables.
Need –The need for multilevel paging arises when- The size of page table is greater than the frame size. As a result, the page table
cannot be stored in a single frame in main memory.
Working-In multilevel paging, the page table having size greater than the frame size is divided into several parts. The size of each
part is same as frame size except possibly the last part. The pages of page table are then stored in different frames of the main
memory. To keep track of the frames storing the pages of the divided page table, another page table is maintained. As a result, the
hierarchy of page tables get generated. Multilevel paging is done till the level is reached where the entire page table can be stored
in a single frame.
There is the standard solution for the above problem that is to use a special, small, and fast-lookup hardware cache that is commonly
known as Translation of look-aside buffer(TLB).
TLB is associative and high-speed memory.
Each entry in the TLB mainly consists of two parts: a key(that is the tag) and a value.
When associative memory is presented with an item, then the item is compared with all keys simultaneously. In case if the
item is found then the corresponding value is returned.
The search with TLB is fast though the hardware is expensive.
The number of entries in the TLB is small and generally lies in between 64 and 1024.
Disadvantages of Paging
Internal Fragmentation: If the size of a process is not a perfect multiple of the page size, the unused space in the last
page results in internal fragmentation.
Increased Overhead: Maintaining the Page Table requires additional memory and processing. For large processes, the
page table can grow significantly, consuming valuable memory resources.
Page Table Lookup Time: Accessing memory requires translating logical addresses to physical addresses using the page
table. This additional step increases memory access time, although Translation Lookaside Buffers (TLBs) can help reduce
the impact.
I/O Overhead During Page Faults: When a required page is not in physical memory (page fault), it needs to be fetched
from secondary storage, causing delays and increased I/O operations.
Complexity in Implementation: Paging requires sophisticated hardware and software support, including the Memory
Management Unit (MMU) and algorithms for page replacement, which add complexity to the system.
Q1. Calculate the size of memory if its address consists of 22 bits and the memory is 2-byte addressable.
Solution-
• Number of locations possible with 22 bits = 2 22 locations
• t is given that the size of one location = 2 bytes
Thus, Size of memory= 222 x 2 bytes = 223 bytes== 8 MB
Q2. Calculate the number of bits required in the address for memory having size of 16 GB. Assume the memory is 4-byte
addressable.
Solution:
Let ‘n’ number of bits are required. Then, Size of memory = 2n x 4 bytes.
Since, the given memory has size of 16 GB, so we have-
2n x 4 bytes = 16 GB
2n x 4 = 16 G
2n x 22 = 234
2n = 232
∴ n = 32 bits
Problem: In a paging scheme, virtual address space is 4 KB and page table entry size is 8 bytes. What should be the optimal
page size?
Solution-
Given-
• Virtual address space = Process size = 4 KB
• Page table entry size = 8 bytes
We know-
Optimal page size
= (2 x Process size x Page table entry size)1/2
= (2 x 4 KB x 8 bytes)1/2
= (216 bytes x bytes)1/2
= 28 bytes
= 256 bytes
Thus, Optimal page size = 256 bytes.
Notes : S26- Demand Paging
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
The demand paging system is somehow similar to the paging system with swapping where processes mainly reside in the main
memory(usually in the hard disk). Thus demand paging is the process that solves the above problem only by swapping the pages on
Demand. This is also known as lazy swapper( It never swaps the page into the memory unless it is needed).
Swapper that deals with the individual pages of a process are referred to as Pager.
Performance of DP:
Effective access time for demand paging memory:
EAT=(1-P)*ma+P*page fault time
Where ma=memory access time, P=Probability of a page fault(0<=P<=1)
Basic A memory management technique to store and A technique for temporarily removing inactive
acquire data in the RAM. applications from the computer system's main
memory.
Purpose Allows the memory of the process address space to Allows multiple programs in the os to run
be non-contiguous. simultaneously.
Occurrence Occurs when only a part of the princess gets Occurs when the entire process is transferred to the
transferred to the disk. disk.
Processes Paging allows many processes in the main memory Swapping reduces the number of processes in the
to exist and is performed by active processes. memory and is performed by inactive processes.
Adaptability More adaptable as relocation of the process is Less adaptable as the operation goes back and forth.
allowed.
Workloads Appropriate for heavy workloads. Appropriate for light to medium workloads.
Memory Used in Non-contiguous Memory Management. Can be performed without any memory management.
Management
Usage Helps to implement virtual memory. Helps CPU to access processes faster.
Notes : S27- Page Replacement Algorithms
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
In this case, if a process requests a new page and supposes there are no free frames, then the Operating system needs to decide which
page to replace. The operating system must use any page replacement algorithm in order to select the victim frame. The Operating
system must then write the victim frame to the disk then read the desired page into the frame and then update the page tables. And
all these require double the disk access time.
Page replacement prevents the over-allocation of the memory by modifying the page-fault service routine.
To reduce the overhead of page replacement a modify bit (dirty bit) is used in order to indicate whether each page is
modified.
This technique provides complete separation between logical memory and physical memory.
Page Replacement in OS
In Virtual Memory Management, Page Replacement Algorithms play an important role. The main objective of all the Page
replacement policies is to decrease the maximum number of page faults.
Page Fault – It is basically a memory error, and it occurs when the current programs attempt to access the memory page for mapping
into virtual address space, but it is unable to load into the physical memory then this is referred to as Page fault.
Demand Paging – Page Fault
Common Page Replacement Techniques: First In First Out (FIFO), Optimal Page replacement, Least Recently Used (LRU)
Example 1: Consider page reference string 1, 3, 0, 3, 5, 6, 3 with 3-page frames. Find the number of page faults using FIFO Page
Replacement Algorithm.
Initially, all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots —> 3 Page Faults.
when 3 comes, it is already in memory so —> 0 Page Faults. T
hen 5 comes, it is not available in memory, so it replaces the oldest page slot i.e 1. —> 1 Page Fault.
6 comes, it is also not available in memory, so it replaces the oldest page slot i.e 3 —> 1 Page Fault.
Finally, when 3 come it is not available, so it replaces 0 ->1-page fault.
Question:Given reference string:7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1 .Implement FIFO on the string and calculate the number of
faults.(Frame =3,given)
Answer:
Reference 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
string
Head 7 7 7 2 2 2 4 4 4 0 0 0 7 7 7
0 0 0 3 3 3 2 2 2 1 1 1 0 0
Tail 1 1 1 0 0 0 3 3 3 2 2 2 1
Page Fault F F F F F F F F F F F F F F F TOTAL
PAGE
FAULT=15
Example to explain belady`s anamoly:
Q:Let the string is:1,2,3,4,1,2,5,1,2,3,4,5.Implement FIFO with 3 Frames and 4 Frames.
A:With frame =3.
Reference string 1 2 3 4 1 2 5 1 2 3 4 5
Head 1 1 1 4 4 4 5 3 3 3
2 2 2 1 1 1 1 4 4
Tail 3 3 3 2 2 2 2 5
Page Fault F F F F F F F F F F TOTAL PAGE FAULT=10
With frame=4
Reference string 1 2 3 4 1 2 5 1 2 3 4 5
Exercise for student:
Head
Tail
Page Fault F F F F F F F F F F TOTAL PAGE FAULT=9
**We notice that the number of faults for four frames(10) is greater than the number of faults for three frames(9)! This most
unexpected resut is known as belady`s anomaly.
Example: Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4-page frame. Find number of page fault using
Optimal Page Replacement Algorithm.
Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults
0 is already there so —> 0 Page fault.
when 3 came it will take the place of 7 because it is not used for the longest duration of time in the future—> 1 Page fault.
0 is already there so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault.
Now for the further page reference string —> 0 Page fault because they are already available in the memory.
Example:
Question:Given reference string:7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1 .Implement Optimal PRA on the string and calculate the
number of faults.(Frame =3,given)
Answer:
Reference 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
number
Reference 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
string
Head 7 7 7 2 2 2 2 2 7
0 0 0 0 4 0 0 1
Tail 1 1 3 3 3 1 1
Page Fault F F F F F F F F F TOTAL
PAGE
FAULT=9
Least Recently Used
In this algorithm, page will be replaced which is least recently used.
Example Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4-page frames. Find number of page faults
using LRU Page Replacement Algorithm.
Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults
0 is already there so —> 0 Page fault.
when 3 came it will take the place of 7 because it is least recently used —> 1 Page fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault
Now for the further page reference string —> 0 Page fault because they are already available in the memory.
Example:
Question:Given reference string:7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1 .Implement LRU PRA on the string and calculate the
number of faults.(Frame =3,given)
Answer:
Reference 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
number
Reference 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
string
Head 7 7 7 2 2 4 4 4 0 1 1 1
0 0 0 0 0 0 3 3 3 0 0
Tail 1 1 3 3 2 2 2 2 2 7
Page Fault F F F F F F F F F F F F TOTAL
PAGE
FAULT=12
Notes : S28- Segmentation
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
Each segment contains a logical unit of process. Whenever a process is to be executed, its segments are moved from secondary
storage to the main memory. Each segment is allocated a chunk of free memory of the size equal to that segment. OS maintains one
table known as segment table, for each process. It includes size of segment and location in memory where the segment has been
loaded.
Characteristics of Segmentation
Some characteristics of the segmentation technique are as follows:
The Segmentation partitioning scheme is variable-size.
Partitions of the secondary memory are commonly known as segments.
Partition size mainly depends upon the length of modules.
Thus with the help of this technique, secondary memory and main memory are divided into unequal-sized partitions.
Need of Segmentation
One of the important drawbacks of memory management in the Operating system is the separation of the user's view of memory
and the actual physical memory. and Paging is a technique that provides the separation of these two memories.
User' view is basically mapped onto physical storage. And this mapping allows differentiation between Physical and logical memory.
It is possible that the operating system divides the same function into different pages and those pages may or may not be loaded into
the memory at the same time also Operating system does not care about the User's view of the process. Due to this technique system's
efficiency decreases. Segmentation is a better technique because it divides the process into segments.
Segmentation Hardware
Given below figure shows the segmentation hardware :
Advantages of Segmentation
Reduced Internal Fragmentation : Segmentation can reduce internal fragmentation compared to fixed-size paging, as
segments can be sized according to the actual needs of a process. However, internal fragmentation can still occur if a
segment is allocated more space than it is actually used.
Flexibility: Segmentation provides a higher degree of flexibility than paging. Segments can be of variable size, and
processes can be designed to have multiple segments, allowing for more fine-grained memory allocation.
Sharing: Segmentation allows for sharing of memory segments between processes. This can be useful for inter-process
communication or for sharing code libraries.
Protection: Segmentation provides a level of protection between segments, preventing one process from accessing or
modifying another process’s memory segment. This can help increase the security and stability of the system.
Disadvantages of Segmentation
External Fragmentation : As processes are loaded and removed from memory, the free memory space is broken into little
pieces, causing external fragmentation. This is a notable difference from paging, where external fragmentation is
significantly lesser.
Overhead is associated with keeping a segment table for each activity.
Due to the need for two memory accesses, one for the segment table and the other for main memory, access time to retrieve
the instruction increases.
Fragmentation: As mentioned, segmentation can lead to external fragmentation as memory becomes divided into smaller
segments. This can lead to wasted memory and decreased performance.
Overhead: Using a segment table can increase overhead and reduce performance. Each segment table entry requires
additional memory, and accessing the table to retrieve memory locations can increase the time needed for memory
operations.
Complexity: Segmentation can be more complex to implement and manage than paging. In particular, managing multiple
segments per process can be challenging, and the potential for segmentation faults can increase as a result.
Paging is a memory management technique where Segmentation is also a memory management technique where
memory is partitioned into fixed-sized blocks that are memory is partitioned into variable-sized blocks that are commonly
commonly known as pages. known as segments.
With the help of Paging, the logical address is divided into With the help of Segmentation, the logical address is divided
a page number and page offset. into segment number and segment offset.
This technique may lead to Internal Fragmentation. Segmentation may lead to External Fragmentation.
The page table mainly contains the base address of each The segment table mainly contains the segment number and the
page. offset.
This technique is faster than segmentation. On the other hand, segmentation is slower than paging.
In Paging, a list of free frames is maintained by the In Segmentation, a list of holes is maintained by the Operating
Operating system. system.
In this technique, in order to calculate the absolute address In this technique, in order to calculate the absolute address segment
page number and the offset both are required. number and the offset both are required.
Question :A certain computer system has the segmented paging architecture for virtual memory. The memory is byte addressable.
Both virtual and physical address spaces contain 216 bytes each. The virtual address space is divided into 8 non-overlapping equal
size segments. The memory management unit (MMU) has a hardware segment table, each entry of which contains the physical
address of the page table for the segment. Page tables are stored in the main memory and consists of 2 byte page table entries. What
is the minimum page size in bytes so that the page table for a segment requires at most one page to store it?
Solution
Virtual Address Space = Process size = 216 bytes
Physical Address Space = Main Memory size =2 16 bytes
Process is divided into 8 equal size segments
Page table entry size = 2 bytes
Let page size = n bytes.
Now, since page table has to be stored into a single page, so we must have- Size of page table <= Page size
Size of each segment
= Process size / Number of segments
= 216 bytes / 8= 216 bytes / 23= 213 bytes= 8 KB
Number of pages each segment is divided
= Size of segment / Page size= 8 KB / n bytes = (8K / n) pages
Size of each page table
= Number of entries in page table x Page table entry size
= Number of pages the segment is divided x 2 bytes
= (8K / n) x 2 bytes= (16K / n) bytes
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
If you are experiencing any of these symptoms, it is possible that your system is thrashing. You can use a system monitoring
tool to check the CPU utilization, page fault rate, and disk activity to confirm this.
Causes of Thrashing in OS
The main causes of thrashing in an operating system are:
1. High degree of multiprogramming:
When too many processes are running on a system, the operating system may not have enough physical memory to accommodate
them all. This can lead to thrashing, as the operating system is constantly swapping pages of memory between physical memory and
disk.
2. Lack of frames:
Frames are the units of memory that are used to store pages of memory. If there are not enough frames available, the operating
system will have to swap pages of memory to disk, which can lead to thrashing.
3. Page replacement policy:
The page replacement policy is the algorithm that the operating system uses to decide which pages of memory to swap to disk. If
the page replacement policy is not effective, it can lead to thrashing.
4. Insufficient physical memory:
If the system does not have enough physical memory, it will have to swap pages of memory to disk more often, which can lead to
thrashing.
5. Inefficient memory management:
If the operating system is not managing memory efficiently, it can lead to fragmentation of physical memory, which can also lead
to thrashing.
6. Poorly designed applications:
Applications that use excessive memory or that have poor memory management practices can also contribute to thrashing.
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
Levels of Memory
Level 1 or Register: It is a type of memory in which data is stored and accepted that are immediately stored in the CPU.
The most commonly used register is Accumulator, Program counter , Address Register, etc.
Level 2 or Cache memory: It is the fastest memory that has faster access time where data is temporarily stored for faster
access.
Level 3 or Main Memory: It is the memory on which the computer works currently. It is small in size and once power is
off data no longer stays in this memory.
Level 4 or Secondary Memory: It is external memory that is not as fast as the main memory but data stays permanently
in this memory.
Cache Performance
When the processor needs to read or write a location in the main memory, it first checks for a corresponding entry in the cache.
If the processor finds that the memory location is in the cache, a Cache Hit has occurred and data is read from the cache.
If the processor does not find the memory location in the cache, a cache miss has occurred. For a cache miss, the cache
allocates a new entry and copies in data from the main memory, then the request is fulfilled from the contents of the
cache.
The performance of cache memory is frequently measured in terms of a quantity called Hit ratio.
We can improve Cache performance using higher cache block size, and higher associativity, reduce miss rate, reduce miss penalty,
and reduce the time to hit in the cache.
Locality of reference
Locality of reference refers to a phenomenon in which a computer program tends to access same set of memory locations for a
particular time period. In other words, Locality of Reference refers to the tendency of the computer program to access instructions
whose addresses are near one another. The property of locality of reference is mainly shown by loops and subroutine calls in a
program.
Locality of Reference and Cache Operation in Cache Memory
In the above figure, you can see that the CPU wants to read or fetch the data or instruction. First, it will access the cache memory
as it is near to it and provides very fast access. If the required data or instruction is found, it will be fetched. This situation is
known as a cache hit. But if the required data or instruction is not found in the cache memory then this situation is known as a
cache miss. Now the main memory will be searched for the required data or instruction that was being searched and if found will
go through one of the two ways:
1. First way is that the CPU should fetch the required data or instruction and use it and that’s it but what, when the same
data or instruction is required again. CPU again has to access the same main memory location for it and we already know
that main memory is the slowest to access.
2. The second way is to store the data or instruction in the cache memory so that if it is needed soon again in the near future
it could be fetched in a much faster way.
Cache Operation:
Temporal Locality – Temporal locality means current data or instruction that is being fetched may be needed soon. So we should
store that data or instruction in the cache memory so that we can avoid again searching in main memory for the same data.
Spatial locality means instruction or data near to the current memory location that is being fetched, may be needed soon in the
near future. This is slightly different from the temporal locality. Here we are talking about nearly located memory locations while
in temporal locality we were talking about the actual memory location that was being fetched.
Advantages
Cache Memory is faster in comparison to main memory and secondary memory.
Programs stored by Cache Memory can be executed in less time.
The data access time of Cache Memory is less than that of the main memory.
Cache Memory stored data and instructions that are regularly used by the CPU, therefore it increases the performance of
the CPU.
Disadvantages
Cache Memory is costlier than primary memory and secondary memory .
Data is stored on a temporary basis in Cache Memory.
Whenever the system is turned off, data and instructions stored in cache memory get destroyed.
The high cost of cache memory increases the price of the Computer System.
Notes : S31- I/O Devices and I/O Subsystems
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
I/O Hardware
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 kernel I/O services are: I/O scheduling, Caching, Buffering, Spooling, Device Reservation, I/O Protection, and Error
1. Scheduling
It means to determine a good order in which to execute input/output requests called disk scheduling.
Scheduling I/O requests can greatly improve overall efficiency. Priorities can also play a vital role in request scheduling and can
reduce the average waiting time for I/O to complete.
By this device access, permissions will be fair to all jobs. When an application issues a blocking I/O system call, the request is
placed on the job queue for that device. The order of the queue rearranges by the Kernel I/O scheduler to improve the overall
system efficiency.
2. Caching
It involves keeping a copy of data in a faster access location than where the data is normally stored.
For example, when you request a file by looking at a Web page are stored on your hard disk in a cache subdirectory under the
directory for your browser. When you get back to a page you have recently visited, the browser can get those files from the cache
rather than the original server, saving you time and saving the network the burden of additional traffic.
There is a difference between cache and buffer is cache is a duplicate copy of some other data stored elsewhere whereas buffer
stores copy of an existing data item.
3. Buffering
Buffer is a memory area maintains by Kernel I/O Subsystem that stores data while they are transferred between two devices or
between a device with an application.
Buffering is done for three reasons.
1. One reason is to adapt to devices that have different data transfer sizes.
2. A second use of buffering is to support copy semantics for an application I/O. An application wants to write data on disk
which is stored in its buffer, it is called “copy semantic ”. it calls the write() system call, providing a pointer to the buffer
and the integer specify the number of bytes to write.
3. A third one is to cope with the speed mismatch between the producer and consumer of the data stream.
4. Spooling
A spool is a type of buffer that holds output for a device. It is the process in which jobs from the cards are read directly onto the
disk and the location of that card in the disk is recorded in a table by the operating system. When that job is required for execution,
it is read from the disk.
For example, several applications may wish to print their output concurrently, so spooling solves this problem by maintaining a
queue for the output. A printer does not accept interleaved data. The output of all application is spooled in a separate disk file.
When an application finishes printing then the spooling system queues the corresponding spool file for output to the printer.
5. Device Reservation
It provides exclusive device access. Allocation of devices when required by the processes and that device when it is no longer
needed. It prevents deadlock. Many operating systems provide features that enable processes to coordinate and give exclusive
access to them.
6. I/O Protection
I/O must be performed via system calls. User processes may accidentally or purposefully attempt to disrupt normal operation via
illegal I/O instructions. To prevent a user from do all I/O instructions defined to be privileged.
7. Error Handling
An operating system that uses protected memory that can guard against many kinds of hardware and application errors. Devices
and I/O transfers may fail in some manner, either for transient reasons, as when a network becomes overloaded or for permanent
reasons, as when a disk controller becomes defective.
Notes : S32- I/O Buffering in Operating Systems
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
1. Single Buffer
Using one buffer to store data temporarily. A buffer is provided by the operating system to the system portion of the main memory.
Block Oriented Device
The system buffer takes the input.
After taking the input, the block gets transferred to the user space by the process and then the process requests for another
block.
Two blocks work simultaneously, when one block of data is processed by the user process, the next block is being read
in.
OS can swap the processes.
OS can record the data of the system buffer to user processes.
Stream Oriented Device
Line- at a time operation is used for scroll-made terminals. The user inputs one line at a time, with a carriage return
signalling at the end of a line.
Byte-at-a-time operation is used on forms mode, terminals when each keystroke is significant.
2. Double Buffer
In this technique the operating system Uses two buffers to allow continuous data transfer between two process or two devices.
Block Oriented
There are two buffers in the system.
One buffer is used by the driver or controller to store data while waiting for it to be taken by higher level of the hierarchy.
Other buffer is used to store data from the lower level module.
Double buffering is also known as buffer swapping.
A major disadvantage of double buffering is that the complexity of the process get increased.
If the process performs rapid bursts of I/O, then using double buffering may be deficient.
Stream Oriented
Line- at a time I/O, the user process need not be suspended for input or output, unless process runs ahead of the double
buffer.
Byte- at a time operations, double buffer offers no advantage over a single buffer of twice the length.
3. Circular Buffer
In this technique the operating system Uses a circular buffer to manage continuous data streams efficiently.
When more than two buffers are used, the collection of buffers is itself referred to as a circular buffer.
In this, the data do not directly pass from the producer to the consumer because the data would change due to overwriting
of buffers before they had been consumed.
The producer can only fill up to buffer i-1 while data in buffer i is waiting to be consumed.
It overlap the input/output of one job with the It overlaps the input/output of one job with the
Basic Difference execution of another job. execution of the same job.
Efficiency Spooling is more efficient than buffering buffering is less efficient than spooling.
Consider Size It consider disk as a huge spool or buffer. Buffer is limited area in main memory.
remote processing It can process data at remote places. It does not support remote processing.
Can handle large amounts of data since spooled Limited by the size of memory available for
Capacity data is stored on disk or other external storage buffering.
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
Disk storage (also sometimes called drive storage) is a data storage mechanism based on a rotating disk. The recording employs
various electronic, magnetic, optical, or mechanical changes to the disk's surface layer. A disk drive is a device implementing such
a storage mechanism. Notable types are hard disk drives (HDD), containing one or more non-removable rigid platters; the floppy
disk drive (FDD) and its removable floppy disk; and various optical disc drives (ODD) and associated optical disc media.
Disk scheduling algorithms are crucial in managing how data is read from and written to a computer’s hard disk. These algorithms
help determine the order in which disk read and write requests are processed, significantly impacting the speed and efficiency of
data access. Common disk scheduling methods include First-Come, First-Served (FCFS), Shortest Seek Time First (SSTF), SCAN,
C-SCAN, LOOK, and C-LOOK. By understanding and implementing these algorithms, we can optimize system performance and
ensure faster data retrieval.
Disk scheduling is a technique operating systems use to manage the order in which disk I/O (input/output) requests are
processed.
Disk scheduling is also known as I/O Scheduling.
The main goals of disk scheduling are to optimize the performance of disk operations; reduce the time it takes to access
data and improve overall system efficiency.
Example:
Suppose the order of request is- (82,170,43,140,24,16,190)
And current position of Read/Write head is: 50
So, total overhead movement (total distance covered by the disk arm) =
(82-50)+(170-82)+(170-43)+(140-43)+(140-24)+(24-16)+(190-16) =642
Disadvantages of FCFS: Does not try to optimize seek time, May not provide the best possible service
3. SCAN
In the SCAN algorithm the disk arm moves in a particular direction and services the requests coming in its path and after reaching
the end of the disk, it reverses its direction and again services the request arriving in its path. So, this algorithm works as an
elevator and is hence also known as an elevator algorithm. As a result, the requests at the midrange are serviced more and those
arriving behind the disk arm will have to wait.
Example:
Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at 50, and it is also given that the
disk arm should move “towards the larger value”.
Therefore, the total overhead movement (total distance covered by the disk arm) is calculated as
= (199-50) + (199-16) = 332
Circular SCAN
Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at 50, and it is also given that the
disk arm should move “towards the larger value”.
So, the total overhead movement (total distance covered by the disk arm) is calculated as:
=(199-50) + (199-0) + (43-0) = 391
5. LOOK
LOOK Algorithm is similar to the SCAN disk scheduling algorithm except for the difference that the disk arm in spite of going to
the end of the disk goes only to the last request to be serviced in front of the head and then reverses its direction from there only.
Thus it prevents the extra delay which occurred due to unnecessary traversal to the end of the disk.
Example:
LOOK Algorithm
Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at 50, and it is also given that the
disk arm should move “towards the larger value”.
So, the total overhead movement (total distance covered by the disk arm) is calculated as:
= (190-50) + (190-16) = 314
6. C-LOOK
As LOOK is similar to the SCAN algorithm, in a similar way, C-LOOK is similar to the CSCAN disk scheduling algorithm. In
CLOOK, the disk arm in spite of going to the end goes only to the last request to be serviced in front of the head and then from
there goes to the other end’s last request. Thus, it also prevents the extra delay which occurred due to unnecessary traversal to the
end of the disk.
Example:
1. Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at 50, and it is also given
that the disk arm should move “towards the larger value”
So, the total overhead movement (total distance covered by the disk arm) is calculated as
= (190-50) + (190-16) + (43-16) = 341
Notes : S34- RAID
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
What is RAID?
RAID (Redundant Array of Independent Disks) is like having backup copies of your important files stored in different places on
several hard drives or solid-state drives (SSDs). If one drive stops working, your data is still safe because you have other copies
stored on the other drives. It’s like having a safety net to protect your files from being lost if one of your drives breaks down.
RAID (Redundant Array of Independent Disks) in a Database Management System (DBMS) is a technology that combines multiple
physical disk drives into a single logical unit for data storage. The main purpose of RAID is to improve data reliability, availability,
and performance. There are different levels of RAID, each offering a balance of these benefits.
Hardware Based: In hardware-based RAID, there’s a physical controller that manages the whole array. This controller can handle
the whole group of hard drives together. It’s designed to work with different types of hard drives, like SATA (Serial Advanced
Technology Attachment) or SCSI (Small Computer System Interface). Sometimes, this controller is built right into the computer’s
main board, making it easier to set up and manage your RAID system. It’s like having a captain for your team of hard drives, making
sure they work together smoothly.
Software Based: In software-based RAID, the controller doesn’t have its own special hardware. So it use computer’s main processor
and memory to do its job. It perform the same function as a hardware-based RAID controller, like managing the hard drives and
keeping your data safe. But because it’s sharing resources with other programs on your computer, it might not make things run as
fast. So, while it’s still helpful, it might not give you as big of a speed boost as a hardware-based RAID system
Firmware Based: Firmware-based RAID controllers are like helpers built into the computer’s main board. They work with the
main processor, just like software-based RAID. But they only implement when the computer starts up. Once the operating system
is running, a special driver takes over the RAID job. These controllers aren’t as expensive as hardware ones, but they make the
computer’s main processor work harder. People also call them hardware-assisted software RAID, hybrid model RAID, or fake
RAID.
Pros:
Greatly improves read/write performance
Inexpensive and easy to implement
Has high storage efficiency
Cons:
Has no data redundancy at all
Failure probability increases with each additional disk
2. In RAID 1 (mirroring without parity or striping), data is written identically to multiple disks (a "mirrored set"). Although
many implementations create sets of 2 disks, sets may contain 3 or more disks. Array provides fault tolerance from disk
errors or failures and continues to operate as long as at least one drive in the mirrored set is functioning. Increased read
performance occurs when using a multi-threaded operating system that supports split seeks, as well as a very small
performance reduction when writing. Using RAID 1 with a separate controller for each disk is sometimes called duplexing.
Pros:
Greatly improves fault tolerance
Greatly improves read performance
Inexpensive and easy to implement
Easier to rebuild data after failure
Can sometimes sustain multiple failures
Cons:
Has no write performance increase
Has low redundancy/storage efficiency
3. In RAID 2 (bit-level striping with dedicated Hamming-code parity), all disk spindle rotation is synchronized, and data
is striped such that each sequential bit is on a different disk. Hamming-code parity is calculated across corresponding bits
on disks and stored on one or more parity disks. Extremely high data transfer rates are possible.
4. In RAID 3 (byte-level striping with dedicated parity), all disk spindle rotation is synchronized, and data is striped such
that each sequential byte is on a different disk. Parity is calculated across corresponding bytes on disks and stored on a
dedicated parity disk. Very high data transfer rates are possible.
5. RAID 4 (block-level striping with dedicated parity) is identical to RAID 5 (see below), but confines all parity data to a
single disk, which can create a performance bottleneck. In this setup, files can be distributed between multiple disks. Each
disk operates independently which allows I/O requests to be performed in parallel, though data transfer speeds can suffer
due to the type of parity. The error detection is achieved through dedicated parity and is stored in a separate, single disk
unit.
6. RAID 5 (block-level striping with distributed parity) distributes parity along with the data and requires all drives but
one to be present to operate; drive failure requires replacement, but the array is not destroyed by a single drive failure.
Upon drive failure, any subsequent reads can be calculated from the distributed parity such that the drive failure is masked
from the end user. The array will have data loss in the event of a second drive failure and is vulnerable until the data that
was on the failed drive is rebuilt onto a replacement drive. A single drive failure in the set will result in reduced performance
of the entire set until the failed drive has been replaced and rebuilt.
Pros:
Greatly improves fault tolerance
Greatly improves read performance
Has high redundancy/storage efficiency
Cons:
Expensive and difficult to implement
More difficult to rebuild data after failure
7. RAID 6 (block-level striping with double distributed parity) provides fault tolerance from two drive failures; array
continues to operate with up to two failed drives. This makes larger RAID groups more practical, especially for high-
availability systems. This becomes increasingly important as large-capacity drives lengthen the time needed to recover
from the failure of a single drive. Single-parity RAID levels are as vulnerable to data loss as a RAID 0 array until the failed
drive is replaced and its data rebuilt; the larger the drive, the longer the rebuild will take. Double parity gives time to rebuild
the array without the data being at risk if a single additional drive fails before the rebuild is complete.
Notes : S35- File Concept and Organization
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
A file can be defined as a data structure which stores the sequence of records. Files are stored in a file system, which may
exist on a disk or in the main memory. Files can be simple (plain text) or complex (specially-formatted).
The collection of files is known as Directory.
The collection of directories at the different levels, is known as File System. File system is the part of the operating system
which is responsible for file management. It provides a mechanism to store the data and access to the file contents including
data and programs. Some Operating systems treats everything as a file for example Ubuntu.
The major concern about the file is deciding where to store the files on the hard disk. There are various disks scheduling
algorithm which will be covered later in this tutorial. A File may or may not be stored within only one block. It can be
stored in the noncontiguous blocks on the disk. We need to keep track of all the blocks on which the part of the files resides.
A file is a collection of related information that is recorded on secondary storage. Or file is a collection of logically related entities.
From the user’s perspective, a file is the smallest allotment of logical secondary storage.
The name of the file is divided into two parts as shown below:
Name
Extension, separated by a period.
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
File Directory
Directory can be defined as the listing of the related files on the disk. The directory may store some or the entire file
attributes.
To get the benefit of different file systems on the different operating systems, A hard disk can be divided into the number
of partitions of different sizes. The partitions are also called volumes or mini disks.
Each partition must have at least one directory in which, all the files of the partition can be listed. A directory entry is
maintained for each file in the directory which stores all the information related to that file.
File Directories
The collection of files is a file directory. The directory contains information about the files, including attributes, location, and
ownership. Much of this information, especially that is concerned with storage, is managed by the operating system. The directory
is itself a file, accessible by various file management routines.
Below are information contained in a device directory.
Name
Type
Address
Current length
Maximum length
Date last accessed
Date last updated
Owner id
Protection information
1) Single-Level Directory
The single-level directory is the simplest directory structure. In it, all files are contained in the same directory which makes it
easy to support and understand.
A single level directory has a significant limitation, however, when the number of files increases or when the system has more than
one user. Since all the files are in the same directory, they must have a unique name. If two users call their dataset test, then the
unique name rule violated.
Advantages
Since it is a single directory, so its implementation is very easy.
If the files are smaller in size, searching will become faster.
The operations like file creation, searching, deletion, updating are very easy in such a directory structure.
Logical Organization : Directory structures help to logically organize files and directories in a hierarchical structure. This
provides an easy way to navigate and manage files, making it easier for users to access the data they need.
Increased Efficiency: Directory structures can increase the efficiency of the file system by reducing the time required to
search for files. This is because directory structures are optimized for fast file access, allowing users to quickly locate the
file they need.
Improved Security : Directory structures can provide better security for files by allowing access to be restricted at the
directory level. This helps to prevent unauthorized access to sensitive data and ensures that important files are protected.
Facilitates Backup and Recovery : Directory structures make it easier to backup and recover files in the event of a system
failure or data loss. By storing related files in the same directory, it is easier to locate and backup all the files that need to
be protected.
Scalability: Directory structures are scalable, making it easy to add new directories and files as needed. This helps to
accommodate growth in the system and makes it easier to manage large amounts of data.
Disadvantages
There may chance of name collision because two files can have the same name.
Searching will become time taking if the directory is large.
This cannot group the same type of files together.
2) Two-Level Directory
As we have seen, a single level directory often leads to confusion of files names among different users. The solution to this problem
is to create a separate directory for each user.
In the two-level directory structure, each user has their own user files directory (UFD). The UFDs have similar structures, but each
list only the files of a single user. System’s master file directory (MFD) is searched whenever a new user id is created.
Advantages
The main advantage is there can be more than two files with same name, and would be very helpful if there are multiple
users.
A security would be there which would prevent user to access other user’s files.
Searching of the files becomes very easy in this directory structure.
Disadvantages
As there is advantage of security, there is also disadvantage that the user cannot share the file with the other users.
Unlike the advantage users can create their own files, users don’t have the ability to create subdirectories.
Scalability is not possible because one user can’t group the same types of files together.
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
Direct Access: It is an alternative method for accessing a file, which is based on the disk model of a file, since disk allows random
access to any block or record of a file. For this method, a file is viewed as a numbered sequence of blocks or records which are
read/written in an arbitrary manner, i.e. there is no restriction on the order of reading or writing. It is well suited for Database
management System.
Index Access: In this method an index is created which contains a key field and pointers to the various block. To find an entry in
the file for a key value , we first search the index and then use the pointer to directly access a file and find the desired entry.
Advantages
• There is no external fragmentation with linked allocation.
• Any free block can be utilized in order to satisfy the file block requests.
• File can continue to grow as long as the free blocks are available.
• Directory entry will only contain the starting block address.
Disadvantages
• Random Access is not provided.
• Pointers require some space in the disk blocks.
• Any of the pointers in the linked list must not be broken otherwise the file will get corrupted.
• Need to traverse each block.
Advantages
• Uses the whole disk block for data.
• A bad disk block doesn't cause all successive blocks lost.
• Random access is provided although its not too fast.
• Only FAT needs to be traversed in each file operation.
Disadvantages
• Each Disk block needs a FAT entry.
• FAT size may be very big depending upon the number of FAT entries.
• Number of FAT entries can be reduced by increasing the block size but it will also increase Internal Fragmentation.
Notes : S38- File Sharing and Implementation Issues
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]
File Sharing in OS
File Sharing in an Operating System(OS) denotes how information and files are shared between different users, computers, or
devices on a network; and files are units of data that are stored in a computer in the form of documents/images/videos or any others
types of information needed.
For Example: Suppose letting your computer talk to another computer and exchange pictures, documents, or any useful data. This
is generally useful when one wants to work on a project with others, send files to friends, or simply shift stuff to another device.
Our OS provides ways to do this like email attachments, cloud services, etc. to make the sharing process easier and more secure.
Now, file sharing is nothing like a magical bridge between Computer A to Computer B allowing them to swap some files with each
other.
Folder/Directory: It is basically like a container for all of our files on a computer. The folder can contain files and even
other folders maintaining like hierarchical structure for organizing data.
Networking: It is involved in connecting computers or devices where we need to share the resources. Networks can be
local (LAN) or global (Internet).
IP Address: It is numerical data given to every connected device on the network
Protocol: It is given as the set of rules which drives the communication between devices on a network. In the context of
file sharing, protocols define how files are transferred between computers.
File Transfer Protocol (FTP): FTP is a standard network protocol used to transfer files between a client and a server on a
computer network.
These all file sharing methods serves different purpose and needs according to the requirements and flexibility of the users based
on the operating system.
Application Programs: This is the topmost layer where users interact with files through applications. It provides the user
interface for file operations like creating, deleting, reading, writing, and modifying files. Examples include text editors, file
browsers, and command-line interfaces.
Logical File system – It manages metadata information about a file i.e includes all details about a file except the actual
contents of the file. It also maintains via file control blocks. File control block (FCB) has information about a file – owner,
size, permissions, and location of file contents.
File Organization Module – It has information about files, the location of files and their logical and physical blocks.
Physical blocks do not match with logical numbers of logical blocks numbered from 0 to N. It also has a free space that
tracks unallocated blocks.
Basic File system – It Issues general commands to the device driver to read and write physical blocks on disk. It manages
the memory buffers and caches. A block in the buffer can hold the contents of the disk block and the cache stores frequently
used file system metadata.
I/O Control level – Device drivers act as an interface between devices and OS, they help to transfer data between disk and
main memory. It takes block number as input and as output, it gives low-level hardware-specific instruction.
Devices Layer: The bottommost layer, consisting of the actual hardware devices. It performs the actual reading and writing
of data to the physical storage medium. This includes hard drives, SSDs, optical disks, and other storage devices.
Implementation Issues
Management of Disc pace: To prevent space wastage and to guarantee that files can always be stored in contiguous blocks,
file systems must manage disc space effectively. Free space management, fragmentation prevention, and garbage collection
are methods for managing disc space.
Checking for Consistency and Repairing Errors: The consistency and error-free operation of files and directories must
be guaranteed by file systems. Journaling, check summing, and redundancy are methods for consistency checking and error
recovery. File systems may need to perform recovery operations if errors happen in order to restore lost or damaged data.
Locking Files and Managing Concurrency: To prevent conflicts and guarantee data integrity, file systems must control
how many processes or users can access a file at once. File locking, semaphore, and other concurrency-controlling methods
are available.
Performance Optimization: File systems need to optimize performance by reducing file access times,
increasing throughput, and minimizing system overhead. Caching, buffering, prefetching, and parallel processing are
methods for improving performance.
They are:-
1-Type of access
2 Access control
3-Other protection approaches (such as password).
Type of access:-
We can easily provide protection by prohibiting access. Controlled access is provided by protection mechanism. These
mechanisms can accept or reject an access depending on the type of access. The various operations that can be controlled are:-
Read:- helps to read from the file
Write:- helps to write or rewrite the file
Execute:- helps to execute a stored file
Append:- helps to write new information at the end of file.
Delete:- helps to delete the file
List:- helps to list the name and attribute of the file
Various operations such as renaming, copying, editing of a file can be controlled.
Access control:-
In this approach of protection, access depends on the identity of the user .Every user uses a different type to access a file or directory.
The most common method to make a list with identity of each user and their access control. When a user requests an access for file,
then first it checks the access list related to that file. If that particular user is listed, then the operating system allows to access that
user. If not, then it leads to protection violation and operating system denies the request.
[Source:https://bcs.wiley.com/he-bcs/Books?action=resource&itemId=0471250600&bcsId=1743&resourceId=2437,
https://www.geeksforgeeks.org/,https://codex.cs.yale.edu/avi/os-book/OS9/practice-exer-dir/index.html , Other Online Resources]