Lesson 5 Process Managment 2
Lesson 5 Process Managment 2
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.
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.
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
• 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
I/O Devices: hardware devices such as printers, keyboard, mouse that processes
need access to.
Files:
User or file systems required during execution.