Simplicity is Beauty

        Simplicity is Beauty


Index

  1. How?   

    1. How do I achieve synchronization?

     

  2. Where?
  3. Why?
  4. Who?
  5. What?      
  6. When?

How?

1. How do I achieve synchronization?

a) By using a semaphore:

In java, you can use counting semaphore as a mutex by setting "available number" to be one.

i.e.

Semaphore available = new Semaphore(1);
//entering critical_section
available.acquire();
//doing what you must do and this part should be short. Be careful, don't do blocking job here, otherwise you are dead.
...
//leaving critical_section
available.release();
 

b) By using a file lock:

i) How to get?

In java, you can acquire a FileLock from FileChannel.

i.e.

RandomAccessFile myFile=new RandomAccessFile("myFile.txt", "rw");

...

//Be careful! This is a blocking call and you may get deadlock if you don't handle properly

FileLock myLock= myFile.getChannel().lock(); //there are several versions of locking, see below

//You reach here means you already get the file lock and you don't have to worry file may be changed by others.

...

ii) How to use?

In java, filelock is mandatory which means even the owner must "release" filelock before you can modify file.

i.e.

RandomAccessFile myFile=new RandomAccessFile("myFile.txt", "rw");

...

FileLock myLock= myFile.getChannel().lock();

//If you reach here, it means you get the lock. But then why should I release what I just get? This seems to be insane. :)

myLock.release(); //And here you have a synchronization flaw and I leave it to you to solve.

//operating on file

...

 

iii) Choice: blocking or non-blocking?

Basically it is your design choice to use either blocking or non-blocking call of various "lock" method, but there is some

subtle issue you may encounter in your server implementation. Blocking version (i.e. FileChannel.lock() ) has potential

risk of deadlock and usually has a low throughput. So, I suggest you always choose non-blocking version like "tryLock()".

However, you have to handle the exception in case the file is already locked by others.

 

top

Where?

1. Where can I get help?

Search your question in www.sun.com

 

top

Why?

1. Why should I use synchronization?

Because you are writing non-trivial server instead of baby toy UI program and if you don't handle this issue properly you

may run into big troubles.

top

Who?

I am TA and POD of comp6231 in summer of 2006.

And you can reach me by email: qinz_hu@cs.concordia.ca

top

What?

1. What are you expected to do?

a) You are supposed to write a server which satisfies the general properties of server like concurrency, high-performance, deadlock-free etc.

b) You are supposed to demonstrate by a design-choice report indicating how you design to achieve these goals.

c) You are supposed to implement by a program to achieve these goals.

d) You are supposed to write program to demonstrate that you have achieved these goals.

e) You are supposed to show me the result of your demo program.

top


When?

POD time slots:
1. -T---- 20:30-22:00  in H-929     (will share with comp 471 lab)
2. ---J-- 17:30-19:00  in H-929
top

(C)2006 Qingzhe Huang