0% found this document useful (0 votes)
10 views38 pages

Lesson 5 Process Managment 2

The document covers process management in operating systems, focusing on interrupts, deadlocks, and starvation. It explains the causes and conditions of deadlocks, methods for preventing and recovering from them, and strategies for handling starvation. Additionally, it discusses the importance of process synchronization and various algorithms like the Banker's Algorithm for resource allocation and deadlock avoidance.

Uploaded by

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

Lesson 5 Process Managment 2

The document covers process management in operating systems, focusing on interrupts, deadlocks, and starvation. It explains the causes and conditions of deadlocks, methods for preventing and recovering from them, and strategies for handling starvation. Additionally, it discusses the importance of process synchronization and various algorithms like the Banker's Algorithm for resource allocation and deadlock avoidance.

Uploaded by

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

Process Management

Learning objectives
• Explain Interrupts
• Describe the differences among deadlock and starvation
• Explain Causes of system deadlock
• Explain Cases of Deadlock
• Preventing and avoiding deadlocks
• Detecting and recovering from deadlocks
• Detecting and recovering from starvation
Interrupts
• Interrupts function in OS that enables the CPU to respond to
events requiring immediate attention (algorithms(priority, order,
conditions))
• Signals sent to the CPU by hardware or software warning it of an
event that requires immediate attention.
• Interrupt occurrences causes the CPU to halt temporarily to
address the event requiring attention.
Interrupts
Hardware Interrupts:
• Generated by hardware devices (e.g., keyboard, mouse, disk drives)
• E.g., pressing ctr, alt, del keys on a keyboard to check the task manager.
Software Interrupts:
Cause by software instructions to signal an error or request a service
e.G (printer out of papers).
Timer Interrupts:
Caused by a timer within the CPU.
Enables the OS to perform regular tasks like process scheduling.
Interrupts
Advantages of Interrupts

Efficiency:
CPU responds quicker to events.
Enable multitasking

Responsiveness:
Real-time processing of events.
Deadlocks
Deadlock
• System issues that occurs when many processes compete for few
resources.
• Causes the system to stop responding.
• Causes system unavailability.

Process synchronization:
• A state that ensures multiple processes operates coordinated when
accessing shared resources.
• It avoids race conditions.
Deadlock
Deadlock
• A situation in which two or more processes are halted.
• Waiting for each other to release resources.
• Deadlock occurs when the resources needed by a job is the same resource held by other jobs
• Each process holds a resource that the other process needs.
• Both processes are waiting for the other to release the resource first.
• None of process can continue executing.
• All these jobs come to standstill.
• Causes processes to hang, leading to system failure.
• Deadlocks are critical as they affect more than one job.
• Users' intervention is usually required if the situation cannot be resolved by the OS.
Starvation:
• Happens when process waits for an indefinite period to access a resource that that other processes
are continuously given preference.
• Happens to individual processes.
• Happens usually to processes with low priorities.
Deadlock
• A process in an operating system uses requestion resources in the
following way.
• Requests a resource
• Use the resource
• Releases the resource
Deadlock
• A deadlock usually occurs in a non-sharable and non-preemptable environment.
• When files, printers, or scanners, are allocated to jobs.
• These jobs also require other non-sharable no preemptive resource resources that
have been locked by other jobs.
• Non-sharable: Cannot be shared between multiple processes or users. Only one
entity can access the resource at any given time.
• Printers: Once a process starts printing, no other process can use the printer until the job is
completed.
• File Locks: When a process locks a file, other processes are prevented from modifying it until
the lock is released.
• I/O Devices: Certain I/O devices (e.g., disk drives) can only be controlled by one process at a
time.

• Non-preemptable: processes that cannot be interrupted or taken away once


allocated until the process using it releases when the job is completed.
Deadlock in OS
Conditions that causes Deadlock
Mutual Exclusion: A resource is non-shareable.
e.g. Printer is being used by one process, no other process can use the printer until it is released.

Hold and Wait: A process is holding on to a resource and yet it is waiting to acquire additional resources currently held by other
processes.
e.g. A process has locked File A but is waiting for File B, which is held by another process that is waiting for File A.

No Preemption: The processor release the resource voluntarily and cannot be forcibly taken away from it.
e.g., a locked a file, the OS or other processors cannot force the process to release that file.

Circular Wait: Several processes in a circular chain and each process is waiting for a resource that the next process in the chain holds.
e.g.
• Process A holds resource 1 and waits for resource 2.
• Process B holds Resource 2 and waits for Resource 3.
• Process C holds Resource 3 and waits for Resource 1.
• This loop forms a circular dependency.
Deadlock-Modelling
• Processes are represented by circles.
• Resources represented by squares.
• A solid line with an arrow that runs from a resource means that resource has already
been allocated to that process ( the process is holding that resource).
Figure a
• A dashed line with an arrow going from a process to a resource, means that the process
is waiting for that resource.
• Resource 1 is being held by Process 1
• Resource 2 is held by Process 2.
• The directions of the arrows the indicate flow.
• If cyclic flow ,then there’s a deadlock involving the processes and the resources that are Figure b
shown in the cycle.
• Process 1 requests Resource 2 but
doesn’t release Resource 1.
• Process 2 request resource 1 and
does not release resource 2.
• This creates a deadlock
• If processes release the resource, the
deadlock would be resolved.
Deadlock
• A system with three processes (P1, P2, and P3)
• The system also has three resources R1, R2, and R3 ( printer, disk drive, and plotter)
• Let's look at three different possible scenarios using graphs to help us detect any deadlocks.
Scenario 1: No Deadlock
Deadlock

• The system has no deadlocks if each resource is released before it is requested by the next process.

• We can safely conclude that a deadlock can’t occur with this system at this time.
• Even if each process requests each of the other resources, every resource is released
before the next process requests it.
Deadlock in OS
• Scenario 2
This sequence of events is
shown in the two
directed graphs
Deadlock
• The system becomes deadlocked when P3
requests R1.
• A deadlock occurs because every process
is waiting for a resource that is being held
by another process.
• None will be released without intervention.

• A deadlock occurs because every process is waiting


for a resource that is being held by another process.
• None will be released without intervention.
Deadlock
• The resources are released before deadlock can occur (Deadlock
broken)
Deadlock in OS
Common cases of deadlocks
File Deadlocks
• Multiple processes trying to access files and lock the file once
they are holding it. Both waiting for each other to release the file
• Process A locks File 1 and requests File 2,
• Process B locks File 2 and requests File 1.
Deadlock
• Two programs, Purchasing and Sales
• Both need to access two separate files, called Inventory and
Suppliers, to read and write transactions.
• The Purchasing process accesses the Suppliers file to place an
order for more lumber.
• The Sales process accesses the Inventory file to reserve the
parts that will be required to build the home ordered that day.
• The Purchasing process doesn’t release the Suppliers file, but it
requests the Inventory file so it can verify the quantity of lumber
on hand before placing its order for more.
• Purchasing is blocked because the Inventory file is being held
by Sales.
• Meanwhile, the Sales process doesn’t release the Inventory file
(because it needs it).
• At this point, the Sales process is also blocked because the
Suppliers file is already being held by the Purchasing process.
Deadlock
Database Deadlocks: Happens when two or more transactions block
each other from accessing databases objects/resources
(like rows, tables) that the other transactions need.
e.g.
• Transaction A locks Table 1 and waits for Table 2.
• Transaction B locks Table 2 and waits for Table 1.
• Both are waiting for each other to release the locked tables.
Deadlock
Dedicated device allocation:
• Processes compete for exclusive access to hardware devices that are
not sharable, such as printers, disk drives.
• Devices are allocated to a single process at a time.
• If a processes request access to the same device while they hold on to
other resources while waiting, deadlocks may arise.
• The system must allocate these devices carefully to avoid conflicts.
Deadlock in OS
• Consider the case of an engineering design firm with three
programs (P1, P2, and P3)
• They have three dedicated devices: scanner, printer, and
plotter.
• The following sequence of events will result in deadlock:
• Program 1 (P1) requests and gets the only scanner.
• Program 2 requests and gets the only printer.
• Program 3 requests and gets the only plotter.
• Now, Program 1 requests the printer but is blocked.
• Program 2 requests the plotter but is blocked
• Program 3 requests the scanner but is blocked.
• The deadlock begins.
• None of these programs can continue because, each is
waiting for a necessary resource that’s already being held by
another
Deadlock in OS
Deadlocks in Spooling
• Spooling : sending data to a temporary storage area (usually buffer) where it is
accessed by a printer or a process.
• Spooling takes place in such a printing where data is kept in a spool file
waiting to be printed
e.g.
1.Process A is writing its job to the printer spooler, before completing, it needs
the printer to clear some of its data for more space.
2.Process B: Process B poses exclusive access to the printer and is waiting for
more space in the spooler (which is being used by Process A) to write its data to
the spool.
3. A circular wait condition arises—Process A is waiting for the printer (held by
Process B), and Process B is waiting for disk space (held by Process A).
4.Both processes are stuck waiting for resources held by the other, creating a
deadlock.
Deadlock in OS
• Deadlocks In a Network
• Deadlock in network occurs in distributed systems.
• Two or more processes communicate over the network.
• Processes depend on each other for resources.
• Server 1 locks File X saved on its local system.
• Server 2 locks File Y saved on its local system.
• Now, Server 1 needs to access File Y, which is locked by Server 2.
• Server 2 needs to access File X, which is locked by Server 1.
Deadlock In OS
Deadlocks in Disk Sharing

Occurs when multiple processes or computers are accessing the disk or file system.
Server 1 and Server 2 sharing access to two disk partitions, Disk A and Disk B, in a
shared network storage environment.
Server 1 locks Disk A for writing some data.
Server 2 locks Disk B for a backup operation.
Now, Server 1 needs to access Disk 2 to continue its operation (perhaps to store some
related logs or backup).
Server 2, on the other hand, needs access to Disk 1 to finish its backup process (it may
require data from Disk A to complete the operation).
Server 1 is waiting for Server 2 to release Disk B.
Server 2 is waiting for Server 1 to release Disk A.
Strategies for Handling Deadlocks

• Deadlock Prevention (mutual exclusion, preemption, wait and


hold, cycle wait)
• Deadlock avoidance (bank, algorithm)
• Deadlock detection (Resource Allocation Graph)
• Deadlock Recovery
Deadlock Prevention
• Designing the system that does not allow deadlocks to occur by eliminating the conditions causing deadlock.
• Design the systems that have the possibility to completely exclude deadlocks.
• The system should prevent the occurrence of the following conditions of deadlock:

• Exclusion: Systems should ensure that no resource is held exclusively by a process.


• Hold and Wait: System should be designed to allow processes to request all the needed resources at once,
without having to hold on to some, while waiting for others.

• No Preemption: Create systems that enable resources pre-emption.

• Circular Wait: Ensure that processes can only request resources in a certain order to avoid circular wait.

• Note: System kernel does not predict processes and resource requirement, hence, it cannot accurately predict
deadlocks.
deadlock avoidance
• Algorithms that function by looking out for for potential deadlock occurrence and avoid them before
they happen.
• The Deadlock Avoidance algorithm minimize the potential of a deadlock occurring.
• It does not completely avoid a deadlock from occurring.
• Deadlock avoidance algorithms allocates resources only upon establishing that the request does not
lead to a deadlock.
• It tracks each resource usage and identify potential conflicts that could lead to a deadlock.
• If a potential deadlock is identified, the algorithm will resolve the conflict by allocating resources pre-
emptively to other processes.
Deadlock Avoidance
• Each process declares the maximum resource units of each class that it may require.
• A request is served on condition of no possibility of deadlocks occurring.
• key algorithms/technique used:
Banker’s Algorithm:
• Resource allocation and deadlock avoidance algorithm which test all the request made by processes for
resources.
• The algorithm checks for safe state and remain in safe state after granting the resource.
• If there is no safe state it doesn’t allow the request made by the process.
• Safe state: A state where the system confirms that there is at least one sequence of resource allocations
available to allows every process in the system to complete executions with no deadlock occurrence.
• This state guarantees that all processes in a system can be executed fully with no deadlock.
Deadlock avoidance
The Banker's Algorithm determines if a system is in a safe state by looking
at the following:
Available resources: record of the number of available resources type.
Max resources needed: record of the maximum number of resources that
each process needs to fully execute.
Current allocated: record of resources types currently allocated to each
process.
Resources still needed: The resource needed by the process to be fully
execute
Need=maximum – current allocated.
Deadlock avoidance
Banker's Algorithm resource type
CPU Cycles: CPU time required to execute a task

Memory: RAM where data is held while under processing.

I/O Devices: hardware devices such as printers, keyboard, mouse that processes
need access to.
Files:
User or file systems required during execution.

NOTE: resource type can have multiple instances.


RAM available 5 instances in a system, or 20 GB of memory
Strategies for Handling Deadlocks
• Inputs to Banker’s Algorithm
• Max needs of resources by each process.
• Currently, allocated resources by each process.
• Max free available resources in the system.
• The request will only be granted under the below condition
• If the request made by the process is less than equal to the max
needed for that process.
• If the request made by the process is less than equal to the freely
available resource in the system.
Deadlock Detection
• Detection:
• The system keeps checking the resource allocation graph to detect a
deadlock.
• Resource Allocation Graph (RAG) is the algorithm used to detect deadlock.
• (RAG): shows the allocation of resources to processes and uses cycles in
the graph.
• It show which resources are held by which processes and the processes that
are waiting for a resource.
• The presence of a cycle in the graph shows potential for deadlock
occurrence.
• Though this graph the algorithm detects potential deadlocks.
Deadlock Detection

• In the above diagram, resource 1 and resource 2 have single


instances. There is a cycle R1 → P1 → R2 → P2. So, Deadlock is
Confirmed.
Deadlock recovery
• Deadlock recovery techniques used OS to resolve a deadlock
after detection.
• The OS needs to recover by breaking the condition that cause the
deadlock for the system to return to its normal as state.
• There are two main approaches to recover from a deadlock:
• Process termination
• Resource preemption.
• Once a deadlock has been detected, it must be untangled and the
Starvation
• Starvation happens when process i keep being denied the
resources it needs to complete with its execution over a long time.
Starvation:
Handle Starvation in OS
• Apply aging criteria to starving processes
• Aging method used to prevent starvation
• It increases the priority of processes have been waiting in the queue a long
time gradually
• The longer the process waits longer, the more its priority increases.
• It eventually gain a high priority to access the needed resources.
• User Priory Scheduling algorithm with timers.
• Use Round Robin Algorithm.
References
• Understanding Operating Systems Seventh Edition(prescribed book)
• https://www.andrew.cmu.edu/course/14-712 s20/applications/ln/14712-l11.pdf
• https://egyankosh.ac.in/bitstream/123456789/72506/3/Unit-4.pdf

You might also like