FOR FREE MATERIALS

Software Solutions

 

Lock Variable

  • # Software mechanism implemented in user mode. 
  • # Busy waiting solution.
  • # More than two processes are allowed for execution.
  •  

*CS = Critical Section 

 

The concept of lock variable is not working as a lock, it is actually working as a tag 0 and 1

 

Example:

 

Like in a hospital same chamber room can be used by several doctors, now if one doctor enters the chamber and change the tag of the room from vacant to engage. 

 

And it is busy waiting means next doctor knock the door forever until the previous one vacant the room.

 

Now we consider the above code with lock variable. Here two processes P1, P2 runs the following number of instruction.

 

 

# The integer variable Lock, initially 0, keeps track of Lock to enter the critical region and examine or update the shared memory. Initially, process P1 checks lock, finds it to be 0, and enters its critical region. 

 

# Process P2  also finds it to be 0 and therefore sits in a tight loop continually testing lock to see when it becomes 1. 

 

# Continuously testing a variable until some value appears is called busy waiting. It should usually be avoided since it wastes CPU time

 

# Only when there is a reasonable expectation that the wait will be short is busy waiting used. A lock that uses busy waiting is called a spinlock.

 

Now we right the assemble code of the above program with lock variable

 

 

Now another property of the lock variable is one or more processes can inside the critical section at a time.            

 

For example:

 

Initially Lock = 0 (CS is free)

 

 

Step 1: First P1 execute instruction 1, then R0 = 0 and P1 is preempted.

 

Step2: When P2 comes, still lock=0 (CS is free) (because P1 is preemptive just after instruction 1),

so, P2 executes instruction 1 i.e. R0 = 0, next runs instruction 2 which checks R0 is zero or not, hence it is true. Now P2 runs instruction 4 and assign lock =1 then P2 goes to CS and preempted

# At present after  preempted Lock = 1

 

Step3: Now process P1 is ready to execute and instruction 1 is already done (hence P1 skip instruction 1), P1 runs from instruction 2 i.e. R0 = 0, it is true, then execute instruction 4 assign lock =1 and P1 go to the critical section. Next, execute instruction 5 i.e. lock=0, and preempted.

 

It is clear from the above example that P1 and P2 enter into the critical section at the same time.

Therefore mutual exclusion is not satisfied in the case of the lock variable mechanism.

 

This also true for n processes that can enter into the critical section at the same time.

 

 

So, in this mechanism, there is no concept of mutual exclusion.

 

Progress is satisfied no process is stopping here another interesting process which wants to enter into its critical section.

 

This mechanism satisfying:

1)  Mutual Exclusion – No

2)  Progress - Yes

3)  Bounded waitNo

4)  Portable - Yes