0% found this document useful (0 votes)
17 views54 pages

unit-4

The document discusses deadlocks in multiprogramming environments, outlining their causes, necessary conditions, and various methods for handling them, including deadlock prevention, detection, and recovery. It explains the resource allocation graph and introduces the Ostrich Algorithm as a method of ignoring deadlocks. Additionally, it covers file systems, their attributes, operations, access methods, and directory structures.

Uploaded by

heenatabassum569
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views54 pages

unit-4

The document discusses deadlocks in multiprogramming environments, outlining their causes, necessary conditions, and various methods for handling them, including deadlock prevention, detection, and recovery. It explains the resource allocation graph and introduces the Ostrich Algorithm as a method of ignoring deadlocks. Additionally, it covers file systems, their attributes, operations, access methods, and directory structures.

Uploaded by

heenatabassum569
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

UNIT-4

DEADLOCKS
DEADLOCKS:

In a multiprogramming environment, several processes may compete for a finite number of resources. A process
requests resources; if the resources are not available at that time, the process enters a waiting state. Sometimes, a
waiting process is never again able to change state, because the resources it has requested are held by other waiting
processes. This situation is called a deadlock.
SYSTEM MODEL:
A system consists of a finite number of resources to be distributed among a number of competing processes.
The resources may be either physical resources (for example, printers, tape drives, memory space, and CPU cycles) or
logical resources (for example, semaphores, mutex locks, and files)A process must request a resource before using it
and must release the resource after using it.
• A process may request as many resources as it requires carrying out its designated task.
• The number of resources requested should not exceed the total number of resources available in the system.
• In other words, a process cannot request three printers if the system has only two.
Under the normal mode of operation, a process may utilize a resource in only the following sequence:

1. Request. The process requests the resource. If the request cannot be granted immediately (for example, if the
resource is being used by another process), then the requesting process must wait until it can acquire the resource.
2. Use. The process can operate on the resource (for example, if the resource is a printer, the process can print on the
printer).
3. Release. The process releases the resource.
system calls Examples:
request ()
release () device,
open () and close () file,
allocate () and free() memory system calls.
A system table records whether each resource is free or allocated. For each resource that is
allocated, the table also records the process to which it is allocated. If a process requests a
resource that is currently allocated to another process, it can be added to a queue of processes
waiting for this resource.

The four necessary conditions for a deadlock to arise are as follows.


A deadlock situation can arise if the following four conditions hold simultaneously in a system:
Mutual exclusion: only one process at a time can use the resource.

Mutual exclusion
Hold and wait:
A process must be holding at least one resource and waiting to acquire additional resources that are currently being
held by other processes.

Hold and wait

No preemption: Resources cannot be preempted; that is, a resource can be released only voluntarily by the process
holding it, after that process has completed its task.

No preemption
Circular wait: A process is waiting for the resource held by the second process, which is waiting for the
resource held by the third process and so on, till the last process is waiting for a resource held by the first
process. This forms a circular chain.

Circular wait
Resource-Allocation Graph:

P = {PI, P2, …, Pn},

R = {R1, R2, …, Rm}

A directed edge Pi → Rj is called a request edge


A directed edge Rj → Pi is called an assignment edge.

Resource- Allocation Graph


1. The sets P, R, and E:
a. P = { P1 , P2 , P3 }
b. R = { R1 , R2 , R3 , R4 }
c. E = { P1  R1 , P2  R3 , R1  P2 , R2  P2 , R2  P1 , R3  P3 }
2. Resource Instances:
• One instance of resource type R1
• Two instances of resource type R2
• One instance of resource type R3
• Three instances of resource type R4

3. Process states:

• Process PI is holding an instance of resource type R2, and is waiting for an instance of resource type R1.

• Process P2 is holding an instance of R1 and R2 and is waiting for an instance of resource type R3.

• Process P3 is holding an instance of R3.


NOTE:If a graph contains no cycle, then no process in the system is in deadlock
state. If the graph does contain a cycle, then a deadlock may exist.

Resource allocation graph with a deadlock.


Now consider the resource-allocation graph ,In this example, we also have
a cycle.

But there is no deadlock. Process P4 may release its instance of resource type R2. That
resource can then be allocated to P3, breaking the cycle.
Ostrich Algorithm:
• This algorithm is vastly used to solve Deadlock issues in computer
operating systems. Deadlocks can occur when processes have been
granted exclusive access to devices, files, and so on.
METHODS FOR HANDLING DEADLOCKS:

1. Deadlock Ignorance (Ostrich Algorithm).


2. Deadlock Prevention
3. Deadlock Detection and Recovery.
4. Deadlock Avoidance.
1.Deadlock Ignorance (Ostrich Algorithm):
This algorithm is a well-used method to ignore problems. It gets its name from The
Ostrich and the way it sticks its head in the sand and ignores everything that’s
happening around just as the picture below shows.

Pros of ignorance in deadlocks?


Cons of ignorance in deadlocks?

Which OS uses the Ostrich Algorithm?


When to use the Ostrich Algorithm?

Ostrich Algorithm can be used when:

1. Deadlocks occur very rarely.

2. Deadlock is difficult to detect.

3. The cost of prevention of deadlock is high.

2.Deadlock Prevention:

1. Mutual Exclusion:
• non-sharable resources(printer)

• sharable resources(Read-only files )


2.Hold and Wait: Hold and wait condition occurs when a process
holds a resource and is also waiting for some other resource in order to
complete its execution. Thus, if we did not want the occurrence of this
condition then we must guarantee that when a process requests a
resource, it does not hold any other resource.

There are some protocols that can be used in order to ensure that the Hold and Wait condition never
occurs:
• According to the first protocol; Each process must request and gets all its resources before the
beginning of its execution.
• The second protocol allows a process to request resources only when it does not occupy any
resource.
Disadvantages of Both Protocols
• Utilization of resources may be low, since resources may be allocated but unused for a
long period.
• There is a possibility of starvation.
3.No Preemption

• The third necessary condition for deadlocks is that there should be no pre-emption of
resources that have already been allocated.
According to the First Protocol: "If a process that is already holding some resources requests another
resource and if the requested resources cannot be allocated to it, then it must release all the resources
currently allocated to it.“
According to the Second Protocol: "When a process requests some resources, if they are available, then
allocate them. If in case the requested resource is not available then we will check whether it is being used
or is allocated to some other process waiting for other resources. If that resource is not being used, then the
operating system pre-empts it from the waiting process and allocate it to the requesting process. And if that
resource is being used, then the requesting process must wait".
4.Circular Wait

• The Fourth necessary condition to cause deadlock is circular wait, In order to ensure violate this
condition we can do the following:

• Assign a priority number to each resource. There will be a condition that any process cannot request
for a lesser priority resource.

Example: Assume that R5 resource is allocated to P1, if next time P1 asks for R4, R3 that are lesser than
R5; then such request will not be granted. Only the request for resources that are more than R5 will be
granted.
3. Deadlock Detection and Recovery:
RECOVERY FROM DEADLOCK

When a detection algorithm determines that a deadlock exists, several alternatives are available.
• One possibility is to inform the operator that a deadlock has occurred .
• Another possibility is to let the system recover from the deadlock automatically.
There are two options for breaking a deadlock.
• One is simply to abort one or more processes to break the circular wait.
• The other is to preempt some resources from one or more of the deadlocked processes.

Process Termination:
• Abort all deadlocked processes.
• Abort one process at a time until the deadlock cycle is eliminated.
Many factors may affect which process is chosen, including:

1. What the priority of the process is

2. How long the process has computed and how much longer the process will compute before completing its
designated task.
3. How many and what types of resources the process has used.
4. How many more resources the process needs in order to complete.
5. How many processes will need to be terminated?
6. Whether the process is interactive or batch
Resource Preemption:
To eliminate deadlocks using resource preemption, we successively preempt some resources from processes and give
these resources to other processes until the deadlock cycle is broken.
• Selecting a victim:Which resources and which processes are to be preempted?
• Rollback:If we preempt a resource from a process, it cannot continue with its normal execution; it is missing some
needed resource. So roll back the process to some safe state and restart it from that state.
• Starvation:we must ensure that a process can be picked as a victim only a (small) finite number of times.
Wait-Die and Wound-Wait Schemes:
These schemes can also be used for recovery. Older processes can preempt
resources from younger processes (Wound-Wait), or younger processes
can be terminated if they try to access resources held by older
processes (Wait-Die).
Kill the Deadlock:
In some cases, it might be possible to identify a specific process that is
causing the deadlock and terminate it. This is typically a last resort option,
as it directly terminates a process without any complex recovery
mechanisms.
DEADLOCK DETECTION:

If a system does not employ either a deadlock-prevention or a deadlock avoidance algorithm, then a deadlock
situation may occur. In this environment, the system may provide:

 An algorithm that examines the state of the system to determine whether a deadlock has occurred
 An algorithm to recover from the deadlock
Single Instance of Each Resource Type:

Fig 4.11: a) Resource Allocation Graph b) Corresponding wait-for graph


Note: A deadlock exists in the system if and only if the wait-for graph contains a cycle.
Several Instances of a Resource Type:
1. Available: A vector of length “m” indicates the number of available resources of each type.

2. Allocation. An n × m matrix defines the number of resources of each type currently allocated to each
process.
3. Request. An n × m matrix indicates the current request of each process. If Request[i][j] equals k,
• then process Pi is requesting “k” more instances of resource type Rj .

Example: Consider a system with five processes

P0 through

P4 and three resource types A, B, and C.

Resource type A has seven instances, resource type B has two instances, and resource type C has six
instances. Suppose that, at time T0, we have the following resource-allocation state:
• DEADLOCK AVOIDANCE:
The alternative method for avoiding deadlock is to require additional information about how the resources are to
be requested. In order to implement this each process should declare the maximum number of resources of each
type that it may need. Given this prior information, it is possible to construct an algorithm that ensures that the
system will never enter a deadlocked state.

• "The simplest and most useful model requires that "each process" declares " max no. of resources" that it may
need.

• Deadlock avoidance algorithm can dynamically examines the resources will never be in circular wait
condition.
• Safe State:

•A state is safe if the system can allocate resources to each process (up to its maximum) in some order and still
avoid a deadlock. More formally, a system is in a safe state only if there exists a safe sequence.
• NOTE: Deadlock Avoidance Will ensure that system will never enters into "unsafe state"

• So, the deadlock avoidance can achieved with the help of "BANKERS ALGORITHM"
File Systems
A File System is a data structure that stores data and information on storage devices
(hard drives, floppy disc, etc.), making them easily retrievable. Different OS's use
different file systems, but all have similar features.

A file has a certain defined structure, which depends on its type, such as

1. Text File: A text file is a sequence of characters organized into lines (and possibly
pages).

2. Source File: A source file is a sequence of functions, each of which is further


organized as declarations followed by executable statements.

3. Executable File: An executable file is a series of code sections that the loader can
bring into memory and execute.
File Attributes:
• Name
• Identifier
• Type
• Location
• Size
• Protection
• Time, date, and user identification
File Operations:A file is an abstract data type. To define a file properly, the operations that can be performed
on files need to be considered. The operating system can provide system calls to create, write, read, reposition, delete,
and truncate files. Some of the typical operations are
• Creating a file
• Writing a file
• Reading a file
• Repositioning within a file
• Deleting a file
• Truncating a file
open-file table
• containing information about all open files.
• When a file operation is requested, the file is specified via an index into this table, so
no searching is required.
The implementation of the open () and close () operations is more complicated in an
environment where several processes may open the file simultaneously.
per-process table
 tracks all files that a process has open.
 Stored in this table is the information regarding the process’s use of the file.
 The open-file table also has an open count associated with each file to indicate
how many processes have the file open.
 Each close () decreases this open count, when reaches zero-the file’s entry is
removed from the open-file table.
system-wide table
 Maintains information regarding location of the file on disk, access dates, and file
size
File Types:
The name is split into two parts—a name and an extension
Examples include resume.docx, server. c, and ReaderThread.cpp.

File types
Access Methods:
• Files store information. When it is used, this information must be accessed and read into computer memory.
Access methods are generally classified as:
1. Sequential Access Methods
2. Direct Access Methods
 Read: Read from the file.
 Write: Write or rewrite the file.
 Execute: Load the file into memory and execute it.
 Append: Write new information at the end of the file.
 Delete: Delete the file and free its space for possible reuse.
 List: List the name and attributes of the file.
Access Control:
The most common approach to the protection problem is to make access dependent on
the identity of the user. Different users may need different types of access to a file or
directory. The most general scheme to implement identity dependent access is to
associate with each file and directory an access- control list (ACL) specifying user
names and the types of access allowed for each user.
•This technique has two undesirable consequences:

 Constructing such a list may be a tedious and unrewarding task, especially if we do not know in advance the list of
users in the system.
 The directory entry, previously of fixed size, now must be of variable size, resulting in more complicated space
management.

•To condense the length of the access-control list, many systems recognize three classifications of users in connection
with each file:
 Owner. The user who created the file is the owner.

 Group. A set of users who are sharing the file and need similar access is a group, or work group.
Directory Structure:
• The directory can be viewed as a symbol table that translates file names into their directory entries.
1. Search for a file
2. Create a file
3. Delete a file
4. List a directory
5. Rename a file
6. Traverse the file system
Single Level Directory:

Single-level Directory
Two Level Directories:
Tree Structured Directories:

Path names can be of two types: absolute and relative.

 An absolute path name begins at the root and follows a path down to the specified file, giving the
directory names on the path.
 A relative path name defines a path from the current directory.
• absolute path name root/spell/mail/prt/first.
• relative path name defines a path from the current directory.(prt/first)
• Deletion of files in tree structure:
Acyclic Graph Directories:
• An acyclic graph is a graph with no cycles—allows directories to share subdirectories and files.
• The same file or subdirectory may be in two different directories.

Acyclic Graph directory structure


A link is effectively a pointer to another file or subdirectory.
For example, a link may be implemented as an absolute or a
relative path name.
General Graph Directory:
File System Implementation:
• A file is a collection of related information. The file system resides on secondary storage
and provides efficient and convenient access to the disk by allowing data to be stored,
located, and retrieved.
• File system implementation in an operating system refers to how the file system
manages the storage and retrieval of data on a physical storage device such as a hard
drive, solid-state drive, or flash drive.
The file system is organized into many
layers:
• On disk structure
• In memory structure
File control block(FCB)
Directory implementation:
1.Linear list
2.Hash table
File Allocation Methods:
The allocation methods define how the files are stored in
the disk blocks. There are three main disk space or file
allocation methods.
• Contiguous Allocation
• Linked Allocation
• Indexed Allocation
Disadvantages of contiguous file allocation
• It suffers from internal fragmentation. Suppose the size of a
block is 2KB, but the file that has to be stored is just 1KB. In
that case, an extra 1KB remains unutilized and the memory
is wasted.

• It suffers from external fragmentation. If there are sufficient
blocks available to store a file, but if they are not
contiguous, the file cannot be stored.

• The size of the file can be increased only if free contiguous
disk blocks are available.

• Although it allows fast access, memory management is poor.
2. Linked File Allocation
• The linked allocation works just like the linked list. The problem with
contiguous allocation was that memory remained unutilized due to
external fragmentation. The solution to the problem was to allocate
the disk block in the form of a linked list where every block was a
node.

• The blocks are allocated in such a way that every block contains a
pointer to the next block that is allocated to the file.
Disadvantages of linked file allocation
• Random access is not allowed since the memory allocation is not
contiguous. Therefore with the help of the starting block address, we
cannot directly jump to some other block, just as we cannot jump to
any node in the linked list with just the head pointer.

• It is relatively slow because of more seek time.

• Every block needs to contain extra information about the pointer to the
next block.
3. Indexed File Allocation

• Although linked allocation used the memory in an


efficient way, it was slower than contiguous allocation.
There was a need for an allocation method such that
the disk blocks are used properly and also the access
time is less.
Disadvantages of Indexed allocation
• It is very complex.
• Extra memory for index blocks.
• Large pointer overhead.
• For files that are very large, single index block may not be able to hold
all the pointers.
Free Space Management
The operating system manages the free space in the hard disk. This is known as free space
management in operating systems. The OS maintains a free space list to keep track of the free disk
space. The free space list consists of all free disk blocks that are not allocated to any file or directory.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy