Notes_3
Notes_3
1
● Deadlock …………………………………………………………………….…………. 59
● Deadlock Prevention and avoidance ...……….……………..…………… 59
● Deadlock Detection ……………..…………………………………..……………. 62
● Deadlock Recovery ……………..…………………………………..…….………. 63
2
Operating system
3
● Time-Sharing (Multitasking) Operating System: This type of operating
system allows multiple users to access a computer system
simultaneously, with the system rapidly switching between users to give
each user the illusion of having their own dedicated system. An example
of a time-sharing operating system is Unix.
● Distributed Operating System: This type of operating system allows
multiple machines to work together as a single system, sharing
resources and processing power. An example of a distributed operating
system is the Amoeba operating system.
● Network Operating System: This type of operating system is designed to
manage network resources, such as servers, printers, and other devices,
and to provide file and print sharing services to multiple users. An
example of a network operating system is Windows Server.
● Real-Time Operating System: This type of operating system is designed
to respond to events in real-time, such as in control systems, robotics, or
military applications. An example of a real-time operating system is QNX.
Example of NOS
4
● Supports priority based algorithms
● No user interface
How can we implement the compiler to work as the time shared program?
● The time shared system the CPU time is shared among the number of
processes by means of the time slice.
● Now the compiler can be implemented as the time shared program by
breaking the process into the smaller discrete tasks
● This can executed in time slice
● When CPU returns the compiler can work again
5
Network Operating System and Distributed Operating System
Can you explain the difference between a kernel and an operating system ?
BIOS
➔ First the POST (Power on Self Test) is called and it Checks for the BIOS
➔ Then the BIOS check for the CMOS-RAM(it saves the BIOS settings in it)
➔ After this the hardware components are checked
6
➔ BIOS then check for MBR(it stores the information in first sector of any
hard disk and helps in finding the location of OS storage)(First sector is
always of 512 Bytes)
➔ Finally BOOTLOADER is retrieved and the RAM is assigned to the OS
➔ OS is booted
WHAT IS MBR?
UEFI
7
● It maintains a list of valid boot volumes called EFI service partition
instead of using MBR it uses GPT (GUILD PARTITION TABLE)
What is GPT ?
8
What happens when you press the power button ?
9
Can you explain the difference between monolithic and microkernel
architectures ?
Monolithic Architecture:
Microkernel Architecture:
10
● However, this approach can lead to lower performance since system
calls must be executed through the message-passing interface, which
introduces additional overhead.
● Eg - MacOS
Is windows microkernel ?
Microkernel includes : graphics subsystem and some device drivers that are
implemented in user-mode processes that communicate with the kernel
through system calls and other interfaces, which is similar to a microkernel
design.
Modules
11
● Then dynamically based on the usage the other are loaded
● Efficient no use of message passing
● Used in Linux, Unix, MacOs
● MacOS is hybrid has the BSD kernel as well as the Mash Kernel
● The dynamically loaded kernel is known as the kernel extension in
MacOs X
★ The device driver loads the appropriate register into the device
controller
★ The device controller performs further action based on the register
★ Once the operation completes the device driver notifies the operating
system and the os starts working. But it may cost if the device is a
keyboard which triggers the many actions
★ Hence most of the system uses the DMA (Direct Memory Access)
★ In the CPU grant the I/O to read and write without involvement.
★ Hence it notifies at the start and at the end to the CPU and side by side
CPU does its work
★ It needs the special device call the DMA controller
12
○ Kernel mode (0) : Which has the process and threads of the
operating system level
[TIP] The mode bit define the mode of the operating system
When the user requests any system call the OS jumps to the kernel mode and
executes the services needed and again jumps back to the user mode.
Timer:
13
➔ Also the internal register such as the index register provide the high
speed cache to the main memory
➔ Many register allocation and deallocation algorithms are used for the
register management
➔ Since cache has the limited size cache management is important
➔ Single data has the multiple copies in the multiple level
➔ In case of the multiprocess system different devices cache may contain
the same data
➔ IF data updated in one cache then the data need to be updated in all
others
➔ This is known as the cache coherency
● Sometimes user need to access the resources beyond its privileges that
can be given using the escalated privileges
● Example : In linux the sudo used to give the escalade privileges
14
Process Management
Process
[TIP] If the process exists without its parent it is known as the orphan process. For
such a process init becomes the parent and once execution is completed it will be
terminated.
Process Termination
15
1. Process ID or PID : Unique Integer Id for each process in any stage of
execution.
2. Process Stage : The state any process currently is in, like Ready, wait, exit
etc
3. Process Privileges : The special access to different resources to the
memory or devices the process has.
4. Pointer : Pointer location to the parent process.
5. Program Counter : It will always have the address of the next instruction
in line of the processes
6. CPU Registers : Before the execution of the program the CPU registered
where the process needs to be stored at.
7. Scheduling Information : There are different scheduling algorithms for a
process based on which they will be selected in priority. This section
contains all the information about the scheduling.
8. Memory Management Information : The operating system will use a lot
of memory and it needs to know information like – page table, memory
limits, Segment table to execute different programs MIM has all the
information about this.
16
9. Accounting Information : As the name suggests it will contain all the
information about the time process took, Execution ID, Limits etc.
10.I/O Status : The list of all the information of I/O the process can use.
Well, it's not that simple even for a process to run in a modern OS there are
many other stages in the life-cycle of a process, let’s discuss those in detail.
17
System calls
18
● fork() - Creates a new child process by duplicating the calling
process.
● exec() - Replaces the current process with a new process.
● wait() - Suspends execution of the calling process until a child
process exits.
● exit() - Terminates the calling process and returns a status code.
● Load the system call number into a register. The system call number
identifies which system call you want to execute.
● Load the arguments for the system call into the appropriate registers or
memory locations, according to the calling convention of the operating
system and architecture.
● Trigger a software interrupt, which will transfer control to the operating
system kernel.
● The kernel will use the system call number to dispatch the appropriate
system call handler, which will execute the requested operation on
behalf of the calling process.
● The kernel will return the result of the system call to the calling process.
● The calling process can then use the result of the system call as needed
System programs
Why do we need system programs when we already have the system calls?
19
● Abstract the way the system is implemented
● Used for the hardware device management
Implementation
What is a thread?
Thread is the smallest executable unit of a process. For example, when you run
a notepad program, the operating system creates a process and starts the
execution of the main thread of that process.
A process can have multiple threads. Each thread will have their own task and
own path of execution in a process. For example, in a notepad program, one
thread will be taking user inputs and another thread will be printing a
document.
All threads of the same process share memory of that process. As threads of
the same process share the same memory, communication between the
threads is fast.
20
What is the difference between User Level and Kernel Level Threads ?
21
Explain Thread Vs. Process
22
★ If one process is blocked then it will not affect the execution of other
processes. If a user-level thread is blocked, then all other user-level
threads are blocked :
23
Now, suppose the web browser process gets blocked because it's waiting for a
response from a slow website. In this case, the other two processes (word
processing and media player) will continue to execute normally, as they are not
dependent on the web browser process.
if the media player process has two user-level threads, one for playing the
music and another for displaying the playlist, then both threads will get
blocked if the file read operation blocks the media player thread.
24
Parallelism
Concurrency
Concurrency supports more than one task to make progress. Hence we can
achieve the concurrency without parallelism
Multithreading model
To run the user level thread on the CPU-core the kernel level threads are
important. This distributes the user level threads to the appropriate CPU core.
The kernel thread executes the instructions of the user-level thread until a
blocking operation or context switch occurs.
25
2) One-to-One model : Maps each user thread to the kernel thread. It
provides more concurrency than a many-to-one model. The only
drawback of the system is for each user thread the corresponding kernel
thread is needed which creates the overhead to the system
Thread Libraries
26
Java threads are also used but since the java runs on operating system,
ultimately uses the OS’s thread library
Pthreads and Windows use the global variables to share the between the
threads but java is pure object oriented hence it uses the shared objects
Implicit threading
Thread pool
OpenMP
27
Threading issues
Signal Handling
In multithreading the signal can be handled in two ways-
1) Asynchronous : Signal or interrupt is reported to any other thread such
as ctrl + c
2) Synchronous : Signal reported to only process which caused the signal.
Thread Cancellation
Types of cancellation
● Asynchronous : One thread immediately terminates the target thread
● Synchronous : The target thread periodically checks whether it should
terminate.
● The difficulty with the cancellation is that a thread may not release all
the system resources and it's difficult for the OS to do that.
Same as static storage but it is unique for each thread. Persistent across the
function invocation.
Scheduler Activation
28
● A thread requires the LWP (Light weight process) data structure to
interact with the kernel threads.
● In scheduler activation the kernel first allocates the LWP to the thread, if
the thread is blocked it revokes the LWP and assigns it to any other
process. IF the thread is again enabled the kernel assigns the new LWP
to the thread or revokes existing one.
Interprocess communication
29
What are the different ways processes can communicate with each other ?
Shared-Memory Systems
Types of buffer
30
Message passing system
31
How does IPC work in windows ?
32
● A pair of processes communicating over a network employs a pair
of sockets ID one for each process.
● A socket is identified by an IP address concatenated with a port
number. In general, sockets use a client–server architecture.
● Winsock is a programming interface that allows processes to
communicate over network protocols, such as TCP/IP or UDP. It
provides functions for creating sockets, establishing connections,
and sending/receiving data between processes.
3. RPC
● You know
pipes for one-to-one communication, less coding and letting the OS handle
things, shared memory for many-to-many, more manual control over things
but at the cost of more work and harder debugging.
33
Process Scheduling
34
Imagine a company that has a large number of tasks to complete, such as
processing customer orders, generating invoices, and managing inventory. To
manage these tasks efficiently, the company uses a job scheduler queue, which
operates as follows:
35
Once the task is completed, it is moved to the terminated state, and its
resources are released back to the system.
CPU scheduling is the system used to schedule processes that wants to use
CPU time. It allows one process (say P1) to use the CPU time by possibly putting
other process (say P2) on hold or in waiting queue since P2 may be waiting for
an additional resource like input or locked file etc.
● CPU Burst Time : In simple terms, the duration for which a process gets
control of the CPU is the CPU burst time.
● CPU Utilization : CPU utilization can be defined as the the percentage of
time CPU was handling process execution to total time
A computer with 75% CPU utilization is better than 50%, since the CPU time
was better utilized to handle process execution in 75% and lesser time was
wasted in idle time.
● Waiting Time : Total amount of time spent in the ready queue to gain the
access of the CPU for execution.
● TurnAround Time : From the time the process is submitted to the time
the process is completed, is defined as Turn Around Time.
OR
36
Turnaround Time = Exit Time + Arrival Time
37
FCFS :
This is the most basic algorithm in Operating System, as the name suggests,
first come first serve (FCFS) the process which enters the system first gets the
CPU time first.
FCFS may suffer from the convoy effect if the burst time of the first job is the
highest among all. As in real life, if a convoy is passing through the road then
the other persons may get blocked until it passes completely. This can be
simulated in the Operating System also.
SJF :
SJF (Shortest Job First) is a scheduling algorithm in which the scheduler selects
the process with the smallest execution time to execute next.
One liner: SJF is a scheduling algorithm that selects the process with the
shortest execution time.
Characteristics :
SRTF :
SRTF (Shortest Remaining Time First) is a CPU scheduling algorithm where the
process with the smallest amount of time remaining until completion is
scheduled next.
One liner: SRTF schedules the process with the shortest remaining burst time
first.
38
Characteristics :
● SRTF provides optimal average waiting time and turnaround time for
processes.
● It can result in starvation for processes with long burst times.
● It requires preemption of currently executing processes.
RR :
RR is a scheduling algorithm that allocates CPU time to each process for a fixed
time quantum, and then moves on to the next process in a circular fashion.
Characteristics :
Advantages :
Disadvantages :
● Keeping the time quantum big can lead to longer response times for
interactive processes, as they may have to wait longer for their turn to
use the CPU.
39
● Keeping the time quantum small can result in higher overhead due to
frequent context switches, and can lead to lower CPU utilization.
Priority scheduling :
Characteristics :
Advantages :
Disadvantages :
40
● May not be fair to low-priority processes, which may have to wait a long
time to be executed.
● Requires careful management of priorities to prevent priority inversion,
where a low-priority process holds a resource that a high-priority
process needs to continue executing.
Multilevel scheduling :
Characteristics :
Advantages :
Disadvantages :
Suppose we have three queues, namely Q1, Q2, and Q3, with Q1 being the
highest priority queue and Q3 being the lowest priority queue. Each queue has
a different scheduling algorithm, as follows:
41
● Q1: First-Come, First-Served (FCFS) scheduling algorithm
● Q2: Round-Robin (RR) scheduling algorithm with a time quantum of 10
milliseconds
● Q3: Priority scheduling algorithm, where processes with higher priority
are executed first
Now let's assume that a system receives three processes, P1, P2, and P3, with
the following attributes:
When these processes arrive in the system, they are placed in their respective
queues based on their priority and other attributes. So, P1 is placed in Q1, P2 is
placed in Q2, and P3 is placed in Q3.
The scheduling algorithm now executes the processes in the queues according
to their respective algorithms. In Q1, since we are using the FCFS algorithm, P1
is executed first. In Q2, since we are using the RR algorithm, P2 is executed for
10 milliseconds, and then the CPU is switched to the next process in the queue,
which could be P1 again if it is still in the queue. In Q3, since we are using the
priority algorithm, P3 is executed first.
As the processes are executed, they may change their attributes, such as their
priority or CPU burst time, which can cause them to be moved to a different
queue. For example, if P2 completes its I/O operation, it may be moved to Q1 if
it now has a high priority due to its CPU burst time.
Characteristics :
42
● Dynamic priority adjustment: If a process uses up its allotted time-slice,
it is moved to a lower-priority queue, while processes that complete
quickly are moved to higher-priority queues.
● Time-slice adjustment: The length of time-slices can be adjusted based
on the current system load.
Advantages :
Disadvantages :
43
Process Synchronization
Critical sections are sections of code in a process where shared resources are
accessed. To ensure that only one process can access a shared resource at a
time, critical sections must be protected with synchronization primitives
➔ Preemptive Kernel
◆ It allows a process to be preempted while it is running in kernel
mode
◆ More responsive
◆ More suitable for real-time programming
➔ Non preemptive Kernel
◆ It does not allow a process running in kernel mode to be
preempted
44
◆ A kernel mode process will run until it exists kernel mode blocks.
◆ It is free from the race condition
◆ Only one process is active at a kernel at time
When several threads (or processes) share data, running in parallel on different
cores , then changes made by one process may override changes made by
another process running parallel. Resulting in inconsistent data. So, this
requires processes to be synchronized, handling system resources and
processes to avoid such situations is known as Process Synchronization.
Now, due to concurrent access and processing time that computer takes in
both ways you were able to withdraw 3000$ more than your balance. In total
8000$ were taken out and the balance was just 5000$.
45
To avoid such situations process synchronization is used, so another
concurrent process P2 is notified of existing concurrent process P1 and not
allowed to go through as there is P1 process which is running and P2 execution
is only allowed once P1 completes.
46
● Consumer takes that data and process it
● Eg - When we give command to printer, if the prints are less frequent
the consumer will sit idle, or it can exceed the memory if produced in
more number
● This is called as printer spooler problem
47
What is a Bounded Buffer problem ?
1. Mutual Exclusion
48
2. Progress
3. Bounded Waiting
A bound must exist on the number of times that other processes are
allowed to enter the critical section after a process has made a request
to enter the critical section and before that request is granted.
Process1:
49
Process 2:
1. The process that wants to access the shared resource first reads the
value at the memory location.
2. If the value is false, the process sets the value to true and enters the
critical section (i.e., accesses the shared resource). If the value is true,
the process spins in a loop, repeatedly trying to read the value until it
becomes false.
3. When the process is finished using the shared resource, it sets the value
back to false to indicate that it has finished.
4. Any other process that wants to access the shared resource must first
perform the Test and Set operation. If the value is true, it must spin in a
loop until it becomes false.
5. Code:
50
Then what is the difference between both ?
Semaphores
1. wait() : The wait() operation, also known as the P() operation, decrements
the value of the semaphore by 1. If the result is negative, it indicates that
the resource is currently unavailable and the process must wait for it to
become available.
2. signal() : The signal() operation, also known as the V() operation,
increments the value of the semaphore by 1. If any processes are waiting
on the semaphore, it wakes up one of them to access the shared
resource.
Mutexes
51
➔ Mutexes are typically used to protect data structures such as critical
sections, shared variables, and other shared resources.
➔ Mutexes can be implemented in different ways, such as spinlocks, sleep
locks, or hybrid locks.
Implementing Mutexes :
52
the section at a time. shared resource.
Spin Loop
In the spin loop, the thread/process repeatedly checks the lock status to see if
it has been released. This constant checking is called "spinning." Instead of
yielding the CPU or putting the thread/process to sleep, it actively waits for the
lock to be released. This spinning loop can consume CPU resources, so
spinlocks are most effective when the waiting time is expected to be very short.
53
Example : Let's consider a scenario where two threads, Thread A and Thread B,
need to access a shared resource protected by a spinlock.
Advantages:
Disadvantages:
➔ less suitable when waiting times are longer as they can waste CPU
resources.
54
What if more than one process is there in spinLoop , which process will get
the access to the lock when the process holding the lock releases?
➔ The order in which processes acquire the lock may vary each time,
depending on the scheduling algorithm and the dynamic conditions of
the system.
➔ The objective is to provide fairness and ensure that all waiting processes
eventually get an opportunity to access the critical section, preventing
any particular process from monopolizing the lock indefinitely.
Monitors
Purpose
Encapsulation
Monitors encapsulate the shared data and the procedures (also known as
methods or functions) that operate on that data. The shared data and the
procedures are combined into a single construct called a monitor. The monitor
defines the boundaries of the critical section, ensuring that only one thread or
process can be inside the monitor executing its procedures at any given time.
55
Entry and Exit
Condition Variables
● Monitors often provide condition variables, which are mechanisms for
threads or processes to wait for specific conditions to be met before
proceeding.
● Condition variables are associated with the monitor and allow threads or
processes to suspend their execution, freeing the monitor's lock, until a
certain condition becomes true.
● For example, a producer-consumer problem can be solved using a
monitor with condition variables, where the consumer waits until the
buffer is not empty, and the producer signals the consumer when it adds
an item to the buffer.
Example of monitor
Let's consider an example where multiple threads need to access a shared
counter variable in a program. We can use a monitor to ensure that only one
thread can access the counter at a time to prevent inconsistencies.
● The monitor encapsulates the counter variable and the procedures that
operate on it.
● Threads wishing to access the counter first acquire the monitor's lock.
● Once a thread acquires the lock, it can increment or read the counter
safely within the critical section of the monitor.
● Other threads that try to access the counter will be blocked until the lock
is released.
● When a thread finishes its work on the counter, it releases the lock,
allowing other waiting threads to acquire it and access the counter.
56
Advantages
● Monitors provide a higher-level abstraction for synchronization,
● They encapsulate the shared data and synchronization mechanisms
within a single construct,
● Promot’s modular and structured programming.
● Help to prevent race conditions and ensure the integrity and consistency
of shared data.
[ Syntax ]
monitor monitor name
{
/* shared variable declarations */
function P1 ( . . . ) {
...
}
function P2 ( . . . ) {
...
}
.
.
.
function Pn ( . . . ) {
...
}
}
initialization code ( . . . ) {
...
}
57
Priority Inversion
● Suppose there are three tasks: Task A (high priority), Task B (medium
priority), and Task C (low priority).
● Task A is currently executing and needs to access a shared resource that
is currently held by Task B.
● However, Task B gets preempted by Task C, which has a higher priority
than Task B.
● Task A cannot proceed until Task B releases the shared resource, but it is
unable to do so because it is preempted by Task C.
● As a result, Task A, which has a higher priority than both Task B and Task
C, remains blocked or delayed, causing priority inversion.
Synchronization in linux
Synchronization in windows
58
Deadlock
59
resources are requested and released in a specific order, it is possible to
prevent the "hold and wait" condition.
2. Resource allocation graph : In this method, a directed graph is used to
represent the resources and processes involved in a system. If a cycle is
detected in the graph, it indicates the presence of a Deadlock.
3. Banker's algorithm : This is a resource allocation algorithm that is used
to avoid Deadlock. It involves predicting the maximum amount of
resources a process may need and then only allocating them if they are
available.
Deadlock avoidance uses dynamic information, such as the current state of the
system and the resource allocation, to ensure that a deadlock never occurs.
Deadlock prevention uses static information, such as the maximum number of
resources a process can request, to prevent a deadlock from occurring.
1. Resource allocation
2. Timeout
3. Priority based resource allocation
4. Wait/Die : In this method, a process that requests a resource that is not
available will wait until the resource becomes available. If a process that
holds the resource is also requesting a resource that is not available, the
process that has been waiting the longest will be allowed to proceed.
5. Wound/Wait : In this method, a process that requests a resource that is
not available will be killed (wounded) if a process that holds the resource
is also requesting a resource that is not available. The process that was
killed will have to request the resource again later.
60
2. Resource allocation graph : By representing the resources and the
processes as nodes in a graph, the operating system can use algorithms
to check for safe states before allocating resources.
Yes, this code has the potential to cause an infinite loop and lead to a livelock
situation.
61
What is Deadlock detection ?
62
What is deadlock recovery ? [same as avoidance]
Once a deadlock has been detected, it must be resolved. There are several
methods for deadlock recovery, including:
Can you describe a situation where deadlock may occur even in the
presence of a deadlock prevention algorithm?
Suppose there are two resources A and B, and three processes P1, P2, and P3.
The system has one instance of each resource. The processes have the
following sequence of requests:
If we use the Banker's algorithm for deadlock prevention, then the system will
be in a safe state if we start with any of the following sequences:
● P1, P2, P3
● P3, P2, P1
● P3, P1, P2
63
Memory management
When Operating System receives an API request, how does it handle it with
the job queue, secondary storage ready queue ?
➔ When you receive an API request, the OS does not directly handle it. The
request first goes to the application that is responsible for handling the
request. However, when the application requires additional resources
such as memory, the OS comes into play.
➔ The application may request memory for its data and code from the OS.
The OS then checks if the requested memory is available in the main
memory. If it is not available, the OS creates a new process for the
application and assigns it to the job queue.
➔ The job queue contains a list of processes that are waiting for memory
allocation. When the required memory is available, the process is moved
to the ready queue, which is a list of processes that are waiting to be
executed by the CPU.
➔ If the CPU is available, the OS assigns the process from the ready queue
to the CPU, and the process starts executing. As the process executes, it
may request more memory from the OS, which repeats the same
process of checking memory availability and allocating it to the process.
➔ When the process is done with its task, it is removed from the memory,
and the allocated memory is released back to the OS for further
allocation to other processes
64
Memory Management Techniques
65
Types of Contiguous Memory Allocation :
66
Figure 8.1 A base and a limit register define a logical address space.
The base and limit registers can be loaded only by the operating system, which
uses a special privileged instruction. Since privileged instructions can be
executed only in kernel mode, and since only the operating system executes in
kernel mode, only the operating system can load the base and limit registers.
67
Address Binding
68
program requests memory from the operating system at runtime,
and the operating system assigns the memory block's address.
Internal fragmentation
69
● For example, consider a process that requests 100 KB of memory, but
the only available block of memory is 120 KB. The process is allocated
the entire block, but 20 KB of memory is wasted as internal
fragmentation
External fragmentation
● The memory space in the system can comfortably satisfy the processes,
but the available memory space is non-contiguous, thus it further can’t
be utilized.
70
Fixed partitioning :
● In fixed size partitioning, the memory is divided into fixed size partitions,
and each partition can be allocated to a process.
● The size of the partition is determined at system initialization and cannot
be changed dynamically.
● Each process is allocated a single partition, and no other process can use
that partition.
● Examples include IBM 360/370 OS and the early versions of Microsoft
Windows.
● Advantages:
○ Simple and easy to implement.
○ No external fragmentation.
● Disadvantages:
○ Internal fragmentation.
○ Inefficient memory usage.
○ Limited number of processes can be executed.
71
Variable Size Partitioning :
1. First Fit:
● It allocates the first available partition in the memory that is
large enough to accommodate the process.
● Advantages: It is easy to implement and has a relatively low
overhead.
● Disadvantages: It may result in inefficient use of memory
due to fragmentation.
● Example: Suppose the memory is divided into partitions of
size 50KB, 100KB, 200KB, and 300KB. If a process requests
150KB of memory, it will be allocated the 200KB partition.
2. Best Fit :
● It allocates the smallest partition in the memory that is large
enough to accommodate the process.
● Advantages: It reduces fragmentation and makes efficient
use of memory.
● Disadvantages: It requires more time to search for the
best-fit partition, leading to higher overhead.
● Example: Suppose the memory is divided into partitions of
size 50KB, 100KB, 200KB, and 300KB. If a process requests
150KB of memory, it will be allocated the 200KB partition.
72
3. Worst Fit :
● It allocates the largest partition in the memory that is large
enough to accommodate the process.
● Advantages: It reduces the probability of larger processes
being left out of the memory.
● Disadvantages: It leads to higher fragmentation of memory.
● Example: Suppose the memory is divided into partitions of
size 50KB, 100KB, 200KB, and 300KB. If a process requests
150KB of memory, it will be allocated the 300KB partition.
● Advantages:
○ Efficient memory usage.
○ Can accommodate processes of varying sizes.
● Disadvantages:
○ External fragmentation.
○ Overhead in searching for available partitions.
a. Paging
b. Segmentation
Paging
73
1. 1 byte = 2^3 bits
2. 1KB = 2^10 bytes
3. 1MB = 2^20 bytes
4. 1GB = 2^30 bytes
Logical View
Physical View
Frames :
➔ Frames are basically the sliced up physical memory blocks of equal size.
Example : 512kb of memory can be divided into 4 parts for 128kb each
➔ FrameSize === PageSize
➔ Frames == Main Memory divided into parts,
➔ Pages == Process divided in parts
74
Tip : byte addressability refers to the ability of a CPU or memory system to
individually address and access bytes of data. This means that the CPU can access
any byte of memory directly by specifying its address. In a byte-addressable system,
each byte is assigned a unique memory address, and the CPU can access or
manipulate individual bytes of data in memory using their respective addresses
75
In simple terms,
A bit can be 0 || 1
Log2(4096) = 12;
That means To calculate the page number, we need to remove the 12 least
significant bits, which represent the offset. Therefore, the page number is
0x7FFF1
1 char = 4 bits
Remaining is offset
The translation from the logical to the physical address is done by special
equipment in the CPU that is called the Memory Management Unit (MMU)
76
Full Example
1. Process Request :
2. Address Translation:
The CPU receives the logical/virtual address and sends it to the MMU for
address translation. The MMU splits the logical address into two parts:
the page number and the offset.
● Page Number: The MMU extracts the page number from the
logical address, in this case, it's 0x12345.
● Offset: The MMU retrieves the offset within the page, which is
0x678.
3. Page Table Lookup:
The MMU consults the page table, which is maintained by the operating
system, to find the corresponding physical frame for the given page
number.
● Page Table Lookup: The MMU looks up the page number, 0x12345,
in the page table.
● Physical Frame: The page table provides the MMU with the
corresponding physical frame number, let's say it's 0x9876.
4. Physical Address Generation :
With the physical frame number and the offset, the MMU generates the
physical address.
The CPU uses the generated physical address, 0x9876678, to access the
main memory and read the data from that location.
77
Normal paging, also known as eager paging or pre-paging, refers to a paging
system where all the required pages of a process are loaded into physical
memory before the process begins its execution. In normal paging, the entire
address space of the process is loaded into memory, including both the pages
that are immediately needed and those that may not be accessed for a while.
78
8. The TLB contains only a few of the page-table entries. When a logical
address is generated by the CPU, its page number is presented to the
TLB.
9. if the page number is found (known as TLB hit), its frame number is
immediately available and is used to access memory.
10.If the page number is not found in the TLB (known as TLB miss) a
memory reference to the page table must be made.
11.when the frame number is obtained, we can use it to access memory.
12.In addition, we add the page number and frame number to the TLB, so
that they will be found quickly on the next reference.
13.If the TLB is already full of entries, the operating system must select one
for replacement.
14.Replacement policies range from least recently used (LRU) to random.
79
Shared pages
Demand Paging
80
How is page fault handled?
When a page fault occurs, the operating system's page fault handler takes
control to resolve the fault and bring the required page into physical memory.
81
◆ Begin the transfer of the page to a free frame.
➔ While waiting, allocate the CPU to some other user (CPU scheduling,
optional).
➔ Receive an interrupt from the disk I/O subsystem (I/O completed).
➔ Save the registers and process state for the other user (if step 6 is
executed).
➔ Determine that the interrupt was from the disk.
➔ Correct the page table and other tables to show that the desired page is
now in memory.
➔ Wait for the CPU to be allocated to this process again.
➔ Restore the user registers, process state, and new page table, and then
resume the interrupted instruction.
What difficulties arise when restarting the instruction after page fault?
82
● By performing these access checks, the system can determine whether
all the relevant pages are in memory and no page fault will occur during
the move operation.
● If no page faults are detected, the move operation can proceed with the
assurance that all necessary pages are already in memory.
COPY-ON-WRITE
Segmentation
83
Segment Descriptor Table :
Seg Descriptor :
Memory protection :
84
EXAMPLE
Segment
85
2 (Stack) 0x4000 0x1000 Read/Write
3. Address Translation :
4. Memory Access :
86
translation. Unauthorized memory accesses would result in exceptions
or errors.
Error Code: 11
Error Code: 10
Error Code: 4
87
4. Floating-Point Exception (SIGFPE) :
Error Code: 8
5. Abort (SIGABRT) :
Error Code: 6
7. Stack Overflow :
88
Page replacement algorithms
Page Fault – In simple terms, if an incoming stream item (page) is not available
in any of the frames. Then page fault occurs.
89
What is a reference string in PRA ?
EX - 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1
1. FIFO algorithm
90
Belady’s Anomaly
Think – Now, one should think that increasing the page size(frame size as page
size = frame size) will lead to less page fault, right!! since there are more
chances of elements to be present in the queue.
But, that’s not always the case. Sometimes by increasing the page size page
fault rather increases, this type of anomaly is called belady’s Anomaly.(asked in
AMCAT, CoCubes, Oracle)
● 3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4
○ Using 3 slots gives 9-page faults
○ Using 4 slots gives 10-page faults
91
Optimal page replacement algorithm or [MIN Algorithm]
One result of the discovery of Belady’s anomaly was the search for an optimal
page-replacement algorithm—the algorithm that has the lowest page-fault
rate of all algorithms and will never suffer from Belady’s anomaly. Such an
algorithm does exist and has been called OPT or MIN. It is simply this:
Replace the page that will not be used for the longest period of time.
LRU
92
● The key distinction between the FIFO and OPT algorithms (other than
looking backward versus forward in time) is that the FIFO algorithm uses
the time when a page was brought into memory, whereas the OPT
algorithm uses the time when a page is to be used.
● If we use the recent past as an approximation of the near future, then
we can replace the page that has not been used for the longest period of
time.
● This approach is the least recently used (LRU) algorithm.
Basically in the current stack at any iteration we choose that element for
replacement which has the smallest count in the incoming page stream.
Basically in the current stack at any iteration we choose that element for
replacement which has the highest count in the incoming page stream.
93
Which one out of FIFO,LRU,LFU,MFU is best ?
Why ?
● The LRU algorithm selects the page for replacement that has not been
accessed for the longest time.
● It assumes that the pages that have not been used recently are less
likely to be used in the near future.
● LRU takes advantage of temporal locality, which suggests that recently
accessed pages are more likely to be accessed again soon.
Thrashing
94
Cause of Thrashing:
95
Static and Dynamic Loading
Static :
Dynamic :
Static loading is commonly used for standalone executable files, while dynamic
loading is often used for libraries and modular systems.
96
[EXTra Ahead]
● .so files are used for shared libraries in Unix-like systems, including Linux
and other Unix variants.
● Shared libraries provide a mechanism for code and data reuse among
multiple programs. They contain compiled code, functions, and data that
can be dynamically linked and loaded into a program's memory at
runtime.
● .so files are dynamically linked during runtime, allowing multiple
programs to share a single copy of the library in memory.
● The dynamic linker/loader (ld.so or ld-linux.so) is responsible for
resolving symbols and dependencies of .so files when they are loaded
into a program's memory.
● Examples of .so files include libm.so (the math library), libpthread.so (the
POSIX threads library), and user-defined shared libraries like libimage.so.
● .dll files are used for shared libraries in Windows operating systems.
● Similar to .so files, .dll files contain compiled code, functions, and data
that can be dynamically linked and loaded into a program's memory at
runtime.
● .dll files are dynamically linked during runtime, allowing multiple
programs to share a single copy of the library in memory, just like .so
files in Unix-like systems.
● Windows uses the dynamic link library manager (kernel32.dll) to handle
the loading, linking, and resolving of symbols and dependencies of .dll
files.
● Examples of .dll files include kernel32.dll (the Windows API library),
user32.dll (the user interface library), and third-party libraries
distributed as .dll files.
97
What is linking in Operating System ?
There are typically two types of linking involved in the software development
process:
Compile-time Linking:
Run-time Linking:
98
What is the difference between linking and loading ?
Linking is the process of combining code and data from multiple object files to
create an executable or shared library, while loading is the process of bringing
the executable or shared library into memory for execution.
➔
➔ Linux has there own swap area which extends the memory by adding
virtual memory
➔ Although most operating systems for PC s and servers support some
modified version of swapping, mobile systems typically do not support
swapping in any form.
◆ They generally uses flash memory which has limited number of
writes
99
Memory Mapped Files
100
File systems
Why is it used ?
101
Magnetic Disk Structure in Operating System
[TIP] - The length of the tracks near the center is less than the length of the tracks
farther from the center.
102
HDD Vs SDD
SSD’s are A solid-state drive (SSD) is a solid-state storage device that uses
integrated circuit assemblies as memory to store data persistently.
They don’t have disks, thus at times also known as shock resistant storage
system as they don’t break is hard disk falls.(Generally, as everything breaks if
you know how to throw :D
Block :
103
● A block is a fixed-size unit of data storage used by file systems to
manage and organize data on storage devices
● Each block has one or more sector
Sector :
Device driver :
104
★ UNIX uses the UNIX file system (UFS)
★ Windows supports disk file-system formats of FAT , FAT 32, and NTFS (or
Windows NT File System)
★ Linux supports over forty different file systems, the standard Linux file
system is known as the extended file system, with the most common
versions being ext3 and ext4.
Swapping in that setting occurs when the amount of physical memory reaches
a critically low point and processes are moved from memory to swap space to
free available memory.
➔ RAID 0
◆ Simple
◆ No redundancy
➔ RAID 1
◆ Mirroring
◆ Easy
◆ Costly
➔ RAID 2
◆ Memory style error correcting codes
◆ Contains parity of the bits record(set bit count and unset)
◆ Hence can check bit wise error by memory system
◆ First bit of each byte stored on disk1 and second disk2 and so on
◆ Error correcting are stored in other extra disks
➔ RAID 3
◆ bit-interleaved parity organization
◆ Improve 2 by using sector based parity checking
◆ Easy
◆ Less Storage
➔ RAID 4
◆ block-interleaved parity organization
◆ USes block level parity than the bit
◆ Separate blocks parity in separate N disks
➔ RAID 5
105
◆ Block-interleaved distributed parity organization
◆ Avoid single block parity on same block
◆ Ith block parity sorted on (i mod N)+1
➔ RAID 6
◆ P + Q redundancy
◆ Same as RAID 5 but with extra information
◆ Instead of parity, error correction codes Reed Solomon code is
used
RAID problems
➔ RAID does not always assure that data is available for the operating
system and its users.
➔ A pointer to a file could be wrong, for example, or pointers within the file
structure could be wrong. Incomplete writes, if not properly recovered,
could result in corrupted data.
➔ Some other process could accidentally write over a file system’s
structures, too.
➔ RAID protects against physical media errors, but not other hardware and
software errors.
➔ As large as is the landscape of software and hardware bugs, that is how
numerous are the potential perils for data on a system.
Protection
106
File system architectures
107
● Free Space Management : This component tracks the available free
space on the storage device. It maintains information about unused
blocks or clusters and handles the allocation and deallocation of storage
space for files.
● File Allocation Table (FAT) or Inode Table : The file allocation table or
inode table is a data structure that maintains a mapping between file
names, file control blocks (FCBs), and their corresponding physical
storage locations. It helps in locating and accessing files efficiently.
Inode table and fat both use the same data structure which maps logical
address to physical location of file ?
➔ No, the inode table and the File Allocation Table (FAT) do not use the
same data structure to map logical addresses to physical locations of
files
108
Example inode table:
In this example, the inode table contains information about file1.txt and
file2.txt. Each file has an inode number, file name, size, and pointers to
the corresponding data blocks where the file's content is stored.
Now let's consider a FAT file system where the file allocation table is
used to track the allocation status and location of data blocks. The FAT
stores a sequence of entries, with each entry corresponding to a cluster
(a group of contiguous data blocks) on the storage device. The FAT
entries maintain the information about which clusters are allocated and
the next cluster in the file's chain.
109
Example FAT:
In this example, the FAT contains entries that map the cluster numbers
to the next cluster in the file's chain. Each file in the file system will have
a starting cluster number, and by following the chain of entries in the
FAT, the file system can determine the sequence of clusters that make
up the file's content.
Key Differences:
110
File System Interface
Allows to perform File operations: create, open, read, write, close, All features
111
Low-level disk organization refers to the physical organization of data on a
disk. It involves dividing the disk into sectors and tracks, which are the smallest
units of storage. Sectors are typically 512 bytes in size and tracks are
concentric circles on the disk. This organization allows the disk to read and
write data efficiently.
➔ Data structures :
● Inodes : Inodes are data structures used by file systems to store
metadata about files, such as file permissions, ownership,
timestamps, and pointers to data blocks. Each file has an
associated inode that contains this information.
● File Control Blocks (FCBs) : FCBs are data structures used by the
operating system to manage files. They contain information about
a file's location, size, and attributes. FCBs are used to perform file
operations like opening, reading, and writing files.
● Directory structures : Directory structures are used to organize
files into a hierarchical structure. Directories contain entries that
associate file names with their corresponding inodes or FCBs. This
allows for efficient file lookup and management.
The file system layout consists of three main components: boot block,
superblock, and data blocks.
● Boot block : The boot block is the first sector on the disk and
contains the bootloader program. It is responsible for loading the
operating system into memory during the boot process.
● Superblock : The superblock is a data structure that stores
important information about the file system, such as the total
number of blocks, the size of each block, and the location of the
root directory.
112
● Data blocks : Data blocks are the actual units of storage where file
data is stored. They are allocated to files and directories as
needed and are typically of fixed size.
➔ FCFS (First-Come-First-Serve) :
● FCFS is a simple disk scheduling algorithm that handles requests
in the order they arrive.
● It works by servicing the requests in the order they are submitted
to the disk.
● This algorithm suffers from the "elevator effect" where the head
moves back and forth on the disk, resulting in poor performance.
➔ SSTF (Shortest Seek Time First) :
● SSTF is a disk scheduling algorithm that selects the request with
the shortest seek time from the current head position.
● It aims to minimize the seek time by selecting the closest request
to the current head position.
● SSTF is more efficient than FCFS in terms of reducing the total
distance traveled by the disk head.
➔ SCAN :
● SCAN, also known as Elevator algorithm, moves the disk arm from
one end of the disk to the other, serving requests along the way.
● It starts servicing requests in one direction until it reaches the
end, and then reverses direction.
● SCAN provides a better response time compared to FCFS and
reduces the average seek time.
113
➔ C-LOOK (Circular LOOK) :
● C-LOOK is an improvement over the SCAN algorithm.
● Instead of going all the way to the end of the disk, C-LOOK only
goes to the last request in the current direction and then reverses
the direction.
● This reduces unnecessary head movement and improves the
disk's throughput and response time.
Let's consider a directory named "documents" with the following ACL entries:
➔ User: John
◆ Permissions: Read, Write
➔ User: Mary
◆ Permissions: Read, Write, Execute
➔ Group: Employees
◆ Permissions: Read
➔ Other (Everyone else)
◆ Permissions: No access
114
Performance optimization techniques :
➔ Buffering :
● Buffering involves using a region of memory to temporarily store
data during input/output (I/O) operations.
● It helps reduce the number of direct disk accesses by buffering
data in memory before reading or writing it to the disk.
● Buffering can improve performance by reducing disk I/O latency
and increasing the efficiency of data transfers.
➔ Caching :
● Caching is a technique that stores frequently accessed data in a
cache, which is a faster and smaller memory closer to the
processor.
● When a program requests data, the operating system checks the
cache first. If the data is found in the cache, it is retrieved quickly,
avoiding the need to access slower storage devices.
● Caching can significantly speed up data retrieval and improve
overall system performance by reducing the reliance on slower
storage devices.
➔ Prefetching :
● Prefetching involves predicting the future data or instructions that
a program is likely to access and retrieving them in advance.
● By fetching data or instructions proactively, prefetching reduces
the latency associated with accessing data from slower storage
devices.
● Prefetching can be performed at various levels, such as hardware
prefetching in processors or software-based prefetching in the
operating system.
115
Virtual File system
116