What is lock variable? Discuss its working and problems associated with it in detail.

This answer is restricted. Please login to view the answer of this question.

Login Now
  • The lock variable is a synchronization mechanism.
  • It uses a lock variable to provide synchronization among the processes executing concurrently.
  • However, it completely fails to provide synchronization.

It is implemented as-

- Hamro CSIT

Initially, the lock value is set to 0.

  • Lock value = 0 means the critical section is currently vacant and no process is present inside it.
  • Lock value = 1 means the critical section is currently occupied and a process is present inside it.

Working:

This synchronization mechanism is supposed to work as explained in the following scenes-

Scene-01:

  • Process P0 arrives.
  • It executes the lock!=0 instruction.
  • Since the lock value is set to 0, so it returns the value 0 to the while loop.
  • The while loop condition breaks.
  • It sets the lock value to 1 and enters the critical section.
  • Now, even if process P0 gets preempted in the middle, no other process can enter the critical section.
  • Any other process can enter only after process P0 completes and sets the lock value to 0.

Scene-02:

  • In another process, P1 arrives.
  • It executes the lock!=0 instruction.
  • Since the lock value is set to 1, so it returns value 1 to the while loop.
  • The returned value 1 does not break the while loop condition.
  • The process P1 is trapped inside an infinite while loop.
  • The while loop keeps the process P1 busy until the lock value becomes 0 and its condition breaks.

Scene-03:

  • Process P0 comes out of the critical section and sets the lock value to 0.
  • The while loop condition of process P1 breaks.
  • It sets the lock value to 1 and enters the critical section.
  • Now, even if process P1 gets preempted in the middle, no other process can enter the critical section.
  • Any other process can enter only after process P1 completes and sets the lock value to 0.

Failure of the Mechanism:

  •  The mechanism completely fails to provide synchronization among the processes.
  • It can not even guarantee to meet the basic criterion of mutual exclusion.
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .