OS - Unit 3 - Digital Content - 2024
OS - Unit 3 - Digital Content - 2024
Systems
Vidyalankar School of
Information Technology
Wadala (E), Mumbai
www.vsit.edu.in
Certificate
This is to certify that the e-book titled “Operating Systems” comprises all elementary learning tools
for a better understating of the relevant concepts. This e-book is comprehensively compiled as per
the predefined eight parameters and guidelines.
Signature
Date: 03-09-2024
Ms. Maitreyi Joglekar
Assistant Professor
Department of IT & DS
DISCLAIMER: The information contained in this e-book is compiled and distributed for
educational purposes only. This e-book has been designed to help learners understand
relevant concepts with a more dynamic interface. The compiler of this e-book and
Vidyalankar Institute of Technology give full and due credit to the authors of the
contents, developers and all websites from wherever information has been sourced. We
acknowledge our gratitude towards the websites YouTube, Wikipedia, and Google
search engine. No commercial benefits are being drawn from this project.
Unit 3 Concurrency- Deadlock and Memory Management
Contents:
Concurrency:
Principles of Deadlock, Deadlock Detection, Deadlock Prevention, Deadlock Avoidance, Safe
and Unsafe states, Bankers Algorithm
Memory Management
Virtual Memory, Swapping, Paging, Segmentation, Page replacement algorithms, Issues with
paging
Recommended Books:
1. Modern Operating Systems, Edition 4 by Andrew Tanenbaum, Pearson Publications
2. Operating Systems-Internals and Design Principles, Edition 8 by William Stallings,
Pearson publishing House
3. Operating Systems Concept, Edition 8 by Silberschatz, Wiley Publications
4. Operating Systems: By Godbole and Kahate, Tata McGraw-Hill
Deadlock
Q.1 List two types of resources. Explain with suitable example
1. A preemptable resource - is one that can be taken away from the process
owning it with no ill effects.
E.g.
• Memory and CPU cycles is an example of a preemptable resource.
• Consider, a system with 1 GB of memory, one printer, and two 1-GB
processes that each want to print something.
• Currently process A is in main memory. Process A requests and gets the
printer, then starts to compute the values to print. Before it has finished the
computation, its quantum is up and is swapped out to disk.
• Process B now is swapped in main memory and tries to acquire the printer.
• Now we have a deadlock situation, because A has the printer and B has the
memory, and neither one can proceed without the resource held by the
other.
• It is possible to preempt (take away) the memory from B by swapping it out
and swapping A in. Now A can run, do its printing, and then release the
printer. No deadlock occurs.
2. A non-preemptable resource- is one that cannot be taken away from its current
owner without potentially causing failure.
E.g.
• DVD/CD recorder is non-preemptable.
• If a process has begun to burn a CD, suddenly taking the CD recorder away
from it and giving it to another process will result in a garbled/distorted CD.
Q. Define Deadlock and explain the same with an example.
2
Deadlock is a set of blocked processes each holding a resource and waiting to
acquire a resource held by another process.
The cause of deadlocks: Each process needing what another process has.
Source- https://www.youtube.com/shorts/DGU8LT6fSSg
Example-
• Two processes A and B, each want to record a scanned document on a Blu-
ray disc.
• So resources required are scanner and Blu-ray recorder.
• Process A requests to use the scanner and acquires it.
• Process B is programmed differently and requests the Blu-ray recorder first
and is also granted it.
• Now A asks for the Blu-ray recorder, but the request is suspended until B
releases it. Unfortunately, instead of releasing the Blu-ray recorder, B asks
for the scanner.
• At this point both processes are blocked and will remain so forever.
• This situation is called a deadlock.
Q.3 What is deadlock? What are necessary conditions for occurrence of deadlock?
Deadlock is a set of blocked processes each holding a resource and waiting to
acquire a resource held by another process.
The cause of deadlocks: Each process needing what another process has.
Conditions for deadlock-
1) Mutual exclusion‐ Resources are non-sharable. Resource should be used by
a single process at a time.
Q.4 Write a short note on deadlock detection with one resource of each type.
Consider a system with one resource of each type.
E.g. System with 1 printer, 1 scanner, 1 plotter and 1 CD recorder.
Conclusion-
In above grapg, there exists a cycle as shown above.
Processes B, C and D are deadlocked, as they are in circular wait.
Process A can is not in deadlocked state.
Q.5 Write a short note on deadlock detection with multiple resources of each type.
• It is a matrix‐based algorithm for detecting deadlock.
• Let total P processes want to run and m resource classes are available.
• E is the existing resource vector with E1 resourcesof class 1, E2 resources of
class 2, Em resources of class m.
E= (E1, E2, E3, ....., Em)
E.g. Consider system with 3 processes and 4 resource classes with 4 tape
drives, 2 plotters, 3 scanners and 1 Blu‐ray.
P= 3 processes (P1,P2,P3)
m=4 resource classes
• Let C be current allocation matrix, Cij is
the number of instances of resource j that
are held by process i.
• Let A be the available resource vector,
with A1 unallocated resources of class 1,
upto Am unallocated resources of class m.
A= (A1, A2, A3, ...., Am)
• Let R be the request matrix. Rij is the number of
instances of resource j that Process i
wants.
• Temporarily take away a resource from its current owner and give it to
another process.
• Take a resource away from a process, give it to another process and then give
it back without the process noticing it.
• The process which has acquired the resources but currently doing some other
tasks are chosen in this case.
Recovery through Rollback‐
• Check which resources are needed to recover a deadlock.
• A process that has a needed resource is rolled back in time to the point
before it has taken that resource.
• In effect, the process is reset to an earlier moment when it did not have the
resource.
• Periodic check pointing is used here. Checkpointing a process means that its
state is written to a file periodically so that it can be restarted later.
Recovery through Killing Processes‐
• The crudest but simplest way to break a deadlock is to kill one or more
processes.
• Kill one process in the cycle.
• In this approach, the process to be killed is carefully chosen because it is
holding resources that some process in the cycle needs.
• If this does not help, it can be repeated until the cycle is broken.
Q.7 Explain deadlock prevention in detail.
Attacking The Mutual Exclusion Condition‐
• Make resources sharable.
• E.g read only file can be accessed by many users at a time.
• But printer cannot be shared, because its dedicated device.
• By spooling printer, several processes can generate output at the same time.
Attacking The Hold and Wait Condition‐
• To avoid hold and wait, allocate all the resources needed for a process before
the start of execution.
• If all resources are available, process will start its execution.
• If one or more resources are busy, then process will go into waiting state.
• Problem ‐ many processes do not know how many resources they will need
until they have started running.
• Resources will not be used optimally with this approach.
Attacking The No Preemption Condition‐
• Resources assigned to the process can be taken away to prevent deadlock
situation.
• If a process is holding some resources and requests another resource that
cannot be immediately allocated to it (that is, the process must wait), then
all resources the process is currently holding are preempted.
• Some resources can be virtualized to avoid this situation.
Attacking The Circular Wait Condition‐
• One way is to have a rule that a process can have only one resource at any
moment. If it needs a second one, it must release the first one.
• Second way is assign a unique number to each resource type. Each process
request the resources in increasing order of assigned numbers.
• It should release any resources with number less than the requested value.
• The state of (a) is safe because there exists a sequence of allocations that
allows all processes to complete.
• Namely, the scheduler can simply run B exclusively, until it asks for and gets
two more instances of the resource, leading to the state of (b). When B
completes, the state moves of (c). Then the scheduler can run C, leading to
(d).
• When C completes the state moves (e).
• Now A can get the six instances of the resource it needs and also complete.
• Thus, the state of (a) is safe because the system, by careful scheduling, can
avoid deadlock.
Source- https://www.youtube.com/watch?v=lMNrmDUJ3GY
Q.11Explain livelock.
• Imagine 2 processes A and B, both require 2 resources- resource 1 and resource
2 for execution.
• Processes are using polling method in order to acquire lock.
• Polling- Request for resource, if not available wait for few seconds and then
request again. The process continues till the resource is acquired.
• Suppose A first acquires resource 1 and B first acquires resource 2.
• Suppose process 1 wants to lock resource 2 and process 2 wants to lock
resource 1 at the same time, which is not possible.
• So both the processes will release the resources they already has, wait for some
time and follow the same process.
• This cycle will be repeated forever and no process can run, but neither of the
processes will be blocked.
• Each process will use time gap over and over without making progress.
• Here- no deadlock as no process is blocked.
• Livelock- Both processes are live but no process can run further.
•
• Example- Suppose multiple processes have requested to access the Printer.
Algorithm used- print smallest file first
• If one process has a huge file, every time when printer if free it will choose a
process with shortest file. The process with huge file will never get printer
and will starve to death. (it won’t be blocked but postponed every time)
• The first come first serve algorithm is a good option to avoid starvation, but
it has some other disadvantages.
Memory Management
Example-
a. Only A in memory
b. Process B is swapped in from hard disk to memory
c. Process C is swapped in from hard disk to memory.
d. A goes in waiting state. So swapped out to hard disk.
e. D swapped in.
f. B goes in waiting state. So swapped out to hard disk.
g. A has come out of waiting state and ready to execute again. So swapped in
again to RAM
Characteristics-
1. Variable partitions
2. Number, location and size of partitions vary dynamically.
Memory Compaction-
Swapping creates multiple holes in memory. Combine all holes together to form big
one by moving all the processes downwards.
1. Each process has its own address space called as Logical/ virtual address space.
2. In other words, processes are stored in Virtual address space.
3. Virtual Address space is divided into fixed size small units called pages.
4. Physical address space(RAM) is divided into small fix sized units call frames.
5. Size of Page= Size of frame
i.e. each frame can accommodate 1 page
6. Page table- In order run execute pages, they must be brought into physical
memory. Page table Maps pages from virtual memory into frames in RAM.
It is a table maintained by OS to indicate which page is mapped into which frame.
Page Fault-
No space in physical memory for new page. In order to add new page, one page
must be removed from RAM. Which page must be removed to make room for new
page is decided by Page replacement Algorithms.
Source- https://www.youtube.com/watch?v=c-BRg5wPceU
6. Pages 2,3,4 are initially brought into main memory causing page fault. Next 2
page is to be added, which is already there in main memory, so page hit.
7. In order to add page 1, replace the page 2 as it is not needed for long time in
future.
8. In above case, total page faults= 6, total hits=4
Q. 5 Explain FIFO page replacement algorithm with example.
1. The simplest page replacement algorithm and works on basis of first in first out.
2. It removes the pages out in the order in which they are brought in.
3. This algorithm chooses oldest page for the replacement.
4. A queue is maintained in order to hold pages.
5. Example- Consider sequence given below-
6. Pages 4,7,3 are initially brought into main memory causing page fault.
7. In order to add page0, replace the page 4 as it has come first into the RAM.
8. Next page 7 will be replaced as it is the oldest page in RAM.
9. Total page Faults=11 , total page hits= 1
Source- https://www.youtube.com/watch?v=RSOmQV0Wk3Q
5. Initially bring page 2 in main memory causing page fault. R=0 for page 2 as it is
referenced single time.
6. Then bring page 3 in main memory causing page fault. R=0 as it is referenced
single time.
7. Now page 2 is already in RAM and is referenced again, so make R=1 for page 2.
8. Bring in page 1 with R=0.
9. In order to add 5, page 2 is supposed to be replaced as per FIFO policy. But page
2 has second chance (R=0), so it cannot be replaced. So page 2 will remain in RAM
with R=0. Then page 3 will be removed as per FIFO policy.
10. Total page faults=7 , total page hits= 5
Q.7 Write a short note on LRU page replacement algorithm.
1. Removes the page that has not been used for longest time.
2. In order to add new page, this algorithm replaces the page that was used far
back in past.
3. This algorithm looks backward in time, while optimal algorithm looks forward in
time.
4. Example- Consider following sequence-
5. Pages 4,7,3 are initially brought into 3 frames of main memory causing page
fault.
6. Next, page 2 is already in memory so it will lead to page hit.
7. In order to add page 1, algorithm checks 3 distinct pages in the past and removes
the page that is far away (i.e. 3rd page). Out of pages 2,4,3 , page 3 is far away in
past. So algorithm will remove page 3 and will add page 1 at the place of page 3.
8. Total page faults-8, total page hits- 2
5. Removes the page that has not been used recently. Class 0 is of lowest priority
while class 3 is of highest priority.
6. In case of page fault, first replace pages of class 0, then class 1 or class 2.
7. R=1 and M=1 indicates recently used pages.
• Main function
• User defined functions
• Symbol table- variables, arrays, pointers, other data structures
• Stack
• Subroutines
4. Virtual memory is divided into variable sized partitions, each partition serve a
specific function known as segment.
5. Segment Table- Entity which maps segments from Virtual memory to physical
memory.
6. Two registers are used for mapping-
Base- starting physical address of a segment in main memory
Limit- Length of a segment
Q 13 What is Dining Philosophers problem in IPC?
1. Five silent philosophers sit at a round table with bowls of spaghetti. Forks are placed
between each pair of adjacent philosophers.
2. Each philosopher must alternately think and eat.
3. However, a philosopher can only eat spaghetti when he has both left and right forks.
Each fork can be held by only one philosopher and so a philosopher can use the
fork only if it is not being used by another philosopher.
4. After he finishes eating, he needs to put down both forks so they become available
to others.
5. A philosopher can take the fork on his right or the one on his left as they become
available, but cannot start eating before getting both of them.
6. The problem is how to design a discipline of behaviour (a concurrent algorithm)
such that no philosopher will starve.
7. Mutual exclusion is the basic idea of the problem; the dining philosophers create a
generic and abstract scenario useful for explaining issues of this type.
8. The failures these philosophers may experience are analogous to the difficulties that
arise in real computer programming when multiple programs need exclusive access
to shared resources
Source- https://www.youtube.com/watch?v=XjlFoND00oY
MCQs-
Deadlock
a) Memory
b) CPU cycles
c) DVD/CD recorder
d) Printer
a) Mutual Exclusion
c) Preemption
d) Circular Wait
Answer: c) Preemption
d) To avoid deadlocks
4. In a system with single resources of each type, which of the following methods can
be used for deadlock detection?
b) Wait-for Graph
c) Banker's Algorithm
d) FIFO Algorithm
a) Preemption
b) Rollback
c) Killing Processes
d) Page Replacement
Answer: a) Preemption
2. Which page replacement algorithm replaces the page that will not be needed for the
longest time in the future?
a) FIFO
b) LRU
c) Optimal
d) Clock
Answer: c) Optimal
6. Which memory management technique involves dividing virtual address space into
fixed-size units?
a) Segmentation
b) Paging
c) Swapping
d) Compaction
Answer: b) Paging
b) Page Size
c) Swapping
d) Load Control
Answer: c) Swapping