Wednesday, April 22, 2009

CLIPC Semaphores in Action


The Short Story

How the CLIPC Semaphore class can be used to solve the lost update problem.


The Long Story

You can find the source code for CLIPC on SourceForge. The example that I'm showing here can also be found on SourceForge.


The Lost Update: How Not to Run a Bank

Here is some code that implements the bank application described in the previous blog entry on semaphores.

An account is implemented as a text file that contains the balance of the account. Clients use the following process:

  1. Pause for a random period of time
  2. Read the account balance from the file
  3. Pause for a random period of time
  4. Calculate the new balance/decision the withdraw
  5. Write out the new balance to the file

The program randomly decides the amount to deposit/withdraw, with the limitation that it cannot withdraw more money than exists in the account.


public void run(File file, Random r) throws Exception
{
ThreadUtils.sleep(r.nextInt(2000));
int balance = readBalance(file);
sleep(r.nextInt(4000));
int amount = generateTransactionAmount(r, balance);
printMessage(balance, amount);
balance = balance + amount;
writeBalance(file, balance);
}

Here is a screenshot of the app not working quite as desired:



With Semaphores: the Lost Update Found!

By adding a semaphore, the lost update problem can be avoided. The previous process needs to be modified a little bit:
  1. Lock the account (reserve the semaphore)
  2. Read the account balance from the file
  3. Pause for a random period of time
  4. Calculate the new balance/decision the withdraw
  5. Write out the new balance to the file
  6. Unlock the account (release the semaphore)

The modified code:


public void run(Semaphore sem, File file, Random r) throws Exception
{
ThreadUtils.sleep(r.nextInt(2000));
sem.decrement();
int balance = readBalance(file);
sleep(r.nextInt(4000));
int amount = generateTransactionAmount(r, balance);
printMessage(balance, amount);
balance = balance + amount;
writeBalance(file, balance);
sem.increment();
}

The output from the revised sample program:


The code to connect to the semaphore or to create it if it does not already exist is:

 Semaphore(fileName);

The file is an actual file that is used to allow clients to connect to the same semaphore. I'll explain more in the next post.

This code creates a binary semaphore. To create a semaphore that can have a max value of "n" use this call:

 Semaphore(fileName, max value);

For this example, a binary semaphore was used. A command line argument passed the name of the semaphore file.

 sem = new Semaphore(argv[1]);

Conclusion

This posting contained a quick example of how to solve the "Lost Update" problem described in a previous post using a binary semaphore from the CLIPC library. The sample code can be obtained from SourceForge, along with the rest of the CLIPC code.

This example uses file naming for semaphores without really explaining what file naming is or what it is for. In the next exciting chapter of the ongoing CLIPC saga, I will explain how that works.

No comments:

Post a Comment