unit-2
unit-2
Semester–IV
Subject: CS-22 Operating System
Concepts with Unix/Linux
UNIT-2
What is Deadlocks?
1
Examples Of Deadlock
1. The system has 2 tape drives. P1 and P2 each hold one tape drive and
each needs another one.
2. Semaphores A and B, initialized to 1, P0, and P1 are in deadlock as
follows:
P0 executes wait(A) and preempts.
P1 executes wait(B).
Now P0 and P1 enter in deadlock.
P0 P1
wait(A); wait(B)
wait(B); wait(A)
2
3. Assume the space is available for allocation of 200K bytes, and the
following sequence of events occurs.
P0 P1
Hold and Wait: A process is holding at least one resource and waiting for
resources.
Circular Wait: A set of processes waiting for each other in circular form.
3
Deadlock Prevention
If we simulate deadlock with a table which is standing on its four legs then
we can also simulate four legs with the four conditions which when occurs
simultaneously, cause the deadlock.
However, if we break one of the legs of the table then the table will fall
definitely. The same happens with deadlock, if we can be able to violate one
of the four necessary conditions and don't let them occur together then we
can prevent the deadlock.
1. Mutual Exclusion
Mutual section from the resource point of view is the fact that a resource
can never be used by more than one process simultaneously which is fair
enough but that is the main reason behind the deadlock. If a resource could
have been used by more than one process at the same time then the
process would have never been waiting for any resource.
Spooling
For a device like printer, spooling can work. There is a memory associated
with the printer which stores jobs from each of the process into it. Later,
Printer collects all the jobs and print each one of them according to FCFS.
By using this mechanism, the process doesn't have to wait for the printer
and it can continue whatever it was doing. Later, it collects the output when
it is produced.
4
Although, Spooling can be an effective approach to violate mutual exclusion
but it suffers from two kinds of problems.
Hold and wait condition lies when a process holds a resource and waiting
for some other resource to complete its task. Deadlock occurs because
there can be more than one process which are holding one resource and
waiting for other in the cyclic order.
!(Hold and wait) = !hold or !wait (negation of hold and wait is, either
you don't hold or you don't wait)
5
This can be implemented practically if a process declares all the resources
initially. However, this sounds very practical but can't be done in the
computer system because a process can't determine necessary resources
initially.
Process is the set of instructions which are executed by the CPU. Each of the
instruction may demand multiple resources at the multiple times. The need
cannot be fixed by the OS.
3. No Preemption
Deadlock arises due to the fact that a process can't be stopped once it
starts. However, if we take the resource away from the process which is
causing deadlock then we can prevent deadlock.
This is not a good approach at all since if we take a resource away which is
being used by the process then all the work which it has done till now can
become inconsistent.
Consider a printer is being used by any process. If we take the printer away
from that process and assign it to some other process then all the data
which has been printed can become inconsistent and ineffective and also
the fact that the process can't start printing again from where it has left
which causes performance inefficiency.
4. Circular Wait
6
Among all the methods, violating Circular wait is the only approach that
can be implemented practically.
Deadlock Avoidance
In deadlock avoidance, the request for any resource will be granted if the
resulting state of the system doesn't cause deadlock in the system. The
state of the system will continuously be checked for safe and unsafe states.
In order to avoid deadlocks, the process must tell OS, the maximum
number of resources a process can request to complete its execution.
7
The simplest and most useful approach states that the process should
declare the maximum number of resources of each type it may ever need.
The Deadlock avoidance algorithm examines the resource allocations so
that there can never be a circular wait condition.
If the system cannot fulfill the request of all processes then the state of the
system is called unsafe.
The key of Deadlock avoidance approach is when the request is made for
resources then the request must only be approved in the case if the
resulting state is also a safe state.
Deadlock Detection
8
Deadlock Detection:
9
Deadlock Recovery:
10
Physical Memory
11
Virtual Memory
12
Motivations behind this technique:
13
Advantages:
Implementation:
1. Demand paging
2. Demand Segmentation
3. Segmentation with Paging
14
Memory Allocation
2. High Concurrency:
15
Contiguous Memory Allocation
16
Advantages:
Easy to implement
Excellent read performance due to sequential data.
Disadvantages:
17
Advantages:
Disadvantages:
18
Limiting the size of the process: The size of the process cannot be
larger than the size of the partition.
Degree of multiprogramming is less: number of processes depends
on number of partitions.
19
Advantages:
Disadvantages:
External Fragmentation:
Total unused space of RAM cannot be used to load the
processes because available space is not in the contiguous manner.
Difficult Implementation:
Allocation and De-allocation are done very frequently and
partition size will be changed at each time so it will be difficult for the
OS to manage everything.
20
Fragmentation
Processes are loaded and deleted from memory, which creates free
memory space (hole), which are too small to assign other processes and
the memory blocks stay unused. It is called fragmentation.
Types of Fragmentation:
1. Internal Fragmentation
It occurs only in fixed size partitioning.
Size of the process is smaller than the size of the partition in that case
some space of the partition gets wasted and remains unused. This
wastage inside the memory partition is called as Internal
fragmentation.
This space can not be allocated to any other process.
Because static partitioning allows to store only one process in each
partition.
2. External Fragmentation
Total unused space by various partitions cannot be used to load the
processes because the available space is not in the contiguous
manner is called as External fragmentation.
21
Sr. Key Internal Fragmentation External Fragmentation
No.
Definition When there is a difference When there are small and non-
between required memory contiguous memory blocks
1 space vs allotted memory which cannot be assigned to
space, problem is termed as any process, the problem is
Internal Fragmentation. termed as External
Fragmentation.
Memory Internal Fragmentation External Fragmentation occurs
2 Block occurs when allotted when allotted memory blocks
Size memory blocks are of fixed are of varying size.
size.
Occurren Internal Fragmentation External Fragmentation occurs
3 ce occurs when a process needs when a process is removed
more space than the size of from the main memory.
allotted memory block or
use less space.
Solution Best Fit Block Search is Compaction is the solution for
4 thesolution for internal externalfragmentation.
fragmentation.
Process Internal Fragmentation External Fragmentation occurs
5 occurs whenPaging is whenSegmentation is employed.
employed.
22
Non-Contiguous Memory Allocation
b.) Segmentation
Advantages:
23
Disadvantages:
a.) Paging
Advantages:
No External Fragmentation
Swapping is easy (Frame size = Page size)
Disadvantages:
Internal Fragmentation
Page table occupies more memory.
24
b.) Segmentation
Virtual memory is divided into the variable (different) size parts that
are called as segment.
Program is a collection of segments.
Segment assigns related data of process for faster processing.
Types of segmentation: 1. Simple
2. Virtual
Process:
The pages are then stored in different frames of the main memory.
25
Note: CPU generates the logical address for user programs and
the user thinks that the program is running in this logical address
but the program needs physical address because they are in RAM
for its execution. So logical address must be mapped to the
physical address.
Note:
Address = 31 bit, then Address Space = 231 words = 2 G words (1 G =
230)
26
Mapping: Converting logical address(pages) into Physical
address(frames) is called mapping.
Memory-Management Unit: A hardware that maps logical address to
physical address.
27
Logical Address:
Physical Address:
Extra note:
Page number(p): Number of bits required to represent the pages in
Logical Address Space or Page number
Page offset(d): Number of bits required to represent particular
word in a page or page size of Logical Address Space or word
number of a page or page offset.
Frame number(f): Number of bits required to represent the frame
of Physical Address Space or Frame number.
Frame offset(d): Number of bits required to represent particular
word in a frame or frame size of Physical Address Space or word
number of a frame or frame offset.
Structure: Suppose CPU wants to read 5th word of 3rd page, then logical
address is So 3 5 Physical Address for that 1 5
28
Advantages:
No External Fragmentation
Swapping is easy (Frame size = Page size)
Disadvantages:
Internal Fragmentation
Page table occupies more memory.
Demand Paging:
The process of loading the page in main memory from secondary memory
on demand is known as demand paging.
Page fault:
When a page referenced by the CPU is not found in the main memory, it is
called as a page fault.
29
Virtual Memory using Segmentation
Virtual memory is divided into the variable (different) size parts that
are called as segment.
Program is divided into variable size segments and loaded to main
memory.
Program is collection of segments.
OS doesn't care about the User's view of the process. It may divide
the same function into different pages and those pages may or may
not be loaded at the same time into the memory. It decreases the
efficiency of the system.
It is better to have segmentation which divides the process into the
segments. Segment contains related data into segments for faster
processing.
main program
function
method
object
local variables
global variables
symbol table
stack
arrays
Process:
Logical address:
30
Segment table:
Advantages
No Internal fragmentation.
31
Segment Table consumes less space in comparison to Page table in
paging.
Average Segment Size is larger than the actual page size.
Disadvantages:
External fragmentation.
Structure:
32
Contiguous memory allocation Non- Contiguous memory
allocation
Faster Slower
(sequential block ) (different location)
Types: Types:
1. Fixed size partition 1. Paging
2. Variable size partition 2. Segmentation
Paging Segmentation
33