Hi, I am new to the group and this is my first post. I have written a serial program..that communicates with a modbus device on the modbus protocol. this code is at the backend of a CGI 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 on the serial port. i read abt the file-lock, but i am not sure if that will put the process that cud not get the port to sleep until it is free. Is there some way of doing this with the help of any system calls, or some parameters to open (which wud block) the device file?
Any help will be appreciated. thanks a lot Manasi
----------------------------------------------------------------------------------------------------- Sooner or Later.. The Man who wins is not the stronger or the faster, but the one who thinks he CAN ..
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