Lock a file. AND Wait if file is locked

Hi,

I want to do the foll steps:

  1. Check if someone has a lock on my file1.
  2. if file1 is locked by any other user
    wait in a loop till another user releases lock
  3. when lock released, lock file1.
  4. do procesing (write) on file1.
  5. processing complete. release lock on file1.

Please also tell me how the can Wait mechanism can it be implemented best?

Please reply

Are you doing this in a script or a language, - C, etc. ?

i want to do this in unix shell script...