Safe State in operating systems

What is the state?

The state of the system informs that if resources are allocated to different processes then the system undergoes deadlock or not.

What is a safe state? Describe how a safe state helps to avoid deadlock

If the system can allocate resources to the process in such a way that it can avoid deadlock. Then the system is in a safe state.

What is an unsafe state?

If the system can’t allocate resources to the process safely, then the system is in an unsafe state.

Note: Unsafe state not always cause deadlock.

If a system is in a safe state, are we guaranteed it will never deadlock?

It’s true that “All safe states are deadlock free”, but don’t forgot that “all unsafe states not always lead to deadlocks”.

are safe state always absolutely deadlock free?

Yes, safe state are absolutely deadlock free.

Difference between safe and unsafe state in OS

Some common difference between  safe and unsafe state are mentioned below;

safe state unsafe state
system safely allocate all resources system is unable to safely allocate all resources
There is no deadlock

Safe state system will never deadlock.

unsafe state may lead to deadlock but not always.

Unsafe state system may lead to deadlock but not always.

 

Example of safe and unsafe state

Let’s see Deadlock avoidance safe and unsafe state example.

Consider the following set of processes to answer deadlock question. Is the system in a safe state

Free resources : 3

PROCESS Allocated R Needed resources
P1 4 10
P2 2 4
P3 2 7

Process P1 have 4 resources and 10 resources required for completion.

Process P2 have 2 resources and 4 resources required for completion.

Process P3 have 2 resources and 7 resources required for completion.

Total free resources for P1, P2 and P3 are 3.

Now, let’s move to other calculations.

Free resources: 1

PROCESS Allocated R Needed resources
P1 4 10
P2 4 4
P3 2 7

Free resources: 5

PROCESS Allocated R Needed resources
P1 4 10
P2 0 0
P3 2 7

Free resources : 0

PROCESS Allocated R Needed resources
P1 4 10
P2 0 0
P3 7 7

Free resources: 7

PROCESS Allocated R Needed resources
P1 4 10
P2 0 0
P3 0 0

Free resources: 1

PROCESS Allocated R Needed resources
P1 10 10
P2 0 0
P3 0 0

Free resources: 11

PROCESS Allocated R Needed resources
P1 0 0
P2 0 0
P3 0 0

Now, after the termination of processes, we can see following results;

  • P1 have 0 resources and 0 resources required because P1 is no more in system.
  • P2 have 0 resources and 0 resources required because P2 is no more in system.
  • P3 have 0 resources and 0 resources required because P3 is no more in system.

Result: All processes execute successfully, so there is no deadlock and the system is in a safe state.

How is a safe state in OS achieved?

Safe state in Operating system can be achieved if the system can safely allocate all resources requested by all the processes in the system without entering a deadlock state.

Download Slides Presentations and PDF file of this tutorial

  1. Download safe state and unsafe state in OS deadlocks Slides Presentation
  2. Download safe state and unsafe state in OS deadlocks PDF File

Video Lecture