On Mon, 1 Mar 2004, Manasi Shah wrote:
script. I would like to block the serial port, whenever every fork of the scrip is using it. in short i wud like to serialize the requests
You can use file locks. Any process which cannot get the lock will block (sleep) until it does. If you don't want the process to sleep, add the LOCK_UN option to your lock type - this will cause flock to return with an error. You can trap this error and proceed with other options until you feel like trying again.
Finally, every program that tries to use the serial port will have to implement the same lock, which could be a problem.
Standard programs keep a lock file in /var/lock somewhere. These create a file, but that's a race condition, since locking needs to be atomic.
You could try something like this:
open("file.lock", O_WRONLY, O_EXCL|O_CREAT);
but this could also bomb over NFS, or on some systems.
Have a look at "man 2 open" for some tips on using link(2) for locking.
Philip