in reply to Directory locking

Use a lock file, a semaphore, or any other queuing method. What have you tried so far yourself? Or is this homework?