Monday, August 27, 2018

Process Synchronization--Part 5 (Test, Set and Lock)

**The entire code and discussion ideas for this post have been taken from Galvin. The slides of this book helped me immensely during GATE preparation and I think they are really great, so are the ones of the book by Korth for DBMS. Whenever I happen to remember some other ones, I will jot them down in some un-related posts :P


Till now, we have discussed the following posts on Process Synchronization. Before reading this post, please read the following post on Process Synchronization:-


So far, we have seen a few software solutions to the 2-process synchronization problem. Now, we have more than 2 processes, Let us assume that we have n processes. Before designing software solutions for this situation, let us diverge a bit.

Till now, in the software solutions we have seen, before entering its critical section, each process acquires some kind of a lock on the shared data item, enters the critical section, and after it has finished, releases the lock on that data item.

But what if we have a situation where a process is not allowed to interrupt while it executes so that there is no question of mutual exclusion being violated? For one, this approach of disabling interrupts will be problematic on multi-processor environments because disabling interrupts on 1 processor won't suffice because a thread on another processor may violate mutual exclusion. So, we have to disable and re-enable interrupts on all processors which is difficult. Also, there will be issues with timing if we disable the clock interrupt.

What if the hardware supports atomic instructions? Operations which give a guarantee that there will be no interrupt while they are executing? One such operation is known as "Test, Set and Lock", abbreviated as TSL.

TSL can be implemented as a function which returns a boolean value. Let us look at its code first.


Before analyzing what is happening inside this piece of code, let us see how to implement Mutual Exclusion using TSL.


So, let us see how to implement Mutual Exlcusion using TSL.


So, when a process wants to enter the critical section, it will execute the code given just above. It is now time to explain what exactly is meant by the fact that TSL is atomic. It means that from the time the TSL is function is invoked by a process, and until the return value of the TSL function is received by the process inside its while loop, the process cannot be pre-empted.

Analysis


  • Lock is a boolean variable which can hold either true or false.
  • In the funtion TSL, the address of the lock variable is being passed which is accepted inside the formal parameter of TSL function in a variable ptr which is a pointer to the variable lock.
  • If there is some other process currently executing inside its critical section, the value of lock will be already true.
  • In the TSL function, the current value of the variable lock is saved in a local variable and returned to the calling process. Before returning, the value of lock is set to true by its pointer variable. This is a win-win situation. If there is no process currently within the critical section and this process is getting chance to enter, it claims its chance by setting lock to true so that no other process can enter their critical sections. If on the other hand, there is already some other process inside the critical section, then setting lock to true won't do any harm as its already true.
  • If lock was false at the time TSL was called which means there was no other process inside the critical section, then this process will get chance to enter its critical section by exiting the waiting while loop.
  • After exiting its critical section, this process sets lock to false so that other (waiting) processes can now enter.
  • The above code implements mutual exclusion and satisfies progress as well but does not satisfy bounded waiting as there is no guarantee which of the waiting processes will get chance to enter its critical section once a process sets lock to false. It may happen that a slow process always lags behind and concedes the chance to other processes.
In the next post, we will move on to Semaphores.

Click here to read Part 6

Friday, August 3, 2018

Process Synchronization--Part 4 (Peterson's Solution)

Till now, we have discussed the following posts on Process Synchronization. Before reading this post, please read the following post on Process Synchronization:-


The solution to the 2-process synchronization problem discussed in the previous post did not satisfy progress as it could lead to a deadlock situation. In this post, we discuss Peterson's solution, which satisfies Mutual Exclusion, Progress and Bounded Waiting.



Analysis

  • This solution uses both the turn variable and flag array approaches.
  • There is a small change in the waiting loop condition as well compared to the previous solution.
  • Let us consider process P0.
  • First, it expresses interest to enter the critical section by setting flag[0] = true
  • Then, it sets turn variable to 1.
  • P0 enters the waiting loop and it will continue to wait only if turn is 1 AND flag[1] =1.
  • We know that P0 had set turn to 1 in previous statement
  • But it will wait in the loop only if flag[1] = 1 which means P0 will wait only if P1 had expressed interest to enter the critical section EARLIER than P0.
  • So, in Peterson's Solution, the process which expresses interest earlier will get the chance to enter critical section earlier.

Mutual Exclusion

MUTUAL EXLCUSION HOLDS which we can prove by an argument similar to the one used for the previous solution.

Progress

The difference between Peterson's solution and the previous algorithm is that here, there is a turn variable which can hold either 0 or 1. It cannot hold both values at the same time. As the value of turn
is used in an AND (mandatory) condition in the waiting loop, at least 1 process will be able to proceed to its critical section, so there will be no deadlock. Combining this idea with the general proof of progress in the previous algorithm, we see that PROGRESS HOLDS.

Bounded Waiting

We find here that each process sets turn to the other process' desirable value. For example, P0 sets turn to 1 and P1 sets turn to 0. So, if P1 is interested to enter the critical section (if it has set flag[1] to true) and P0 is currently inside the critical section, P1 will be allowed to enter right after P0 exits and sets turn to 1. So, P1 will enter (if it wanted to !) before P0 enters again.So, BOUNDED WATING HOLDS.

So, we reach the end of the 2-process synchronization problem. In the next post, we will discuss a solution to the critical section problem when more than 2 processes are involved, using something called Semaphores. We will also see that Semaphores can be used for other applications as well.

Was it a nice read? Please subscribe to the blog by clicking on the 'Follow' button on the right hand side of this page. You can also subscribe by email by entering your email id in the box provided. In that case, you will be updated via email as and when new posts are published.Also, please like the Facebook Page for this blog at Gate Computer Science Ideas. Cheers!

Click here to read Part 5