LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Locking issue with serial port (https://www.linuxquestions.org/questions/programming-9/locking-issue-with-serial-port-750283/)

smartgupta 08-26-2009 12:53 AM

Locking issue with serial port
 
Hi,
In my application,I need to monitor the devices connected through serial port in solaris sparc 9 OS.I used c and java code for it and I am able to do the same but the problem comes when I restart the system and the open my application and try to monitor it.but I m unable to monitor the devices and getting the message --

Quote:

Warning: The serial port "/dev/term/a" cannot be opened for use.
Please verify that it is entered correctly and that it is not already in use.
but when I delete /var/lock/LCK..a file.I am able to monitor again.

So plz let me know why it is happening and how to solve it.

Thanks in advance..

theNbomr 08-26-2009 08:46 AM

It sounds like you simply need to perform a graceful shutdown of the system, in order for the lockfile(s) to be removed.
--- rod.

wje_lq 08-26-2009 11:06 AM

Quote:

Originally Posted by theNbomr (Post 3658250)
It sounds like you simply need to perform a graceful shutdown of the system, in order for the lockfile(s) to be removed.

Lock files, if used properly, shouldn't depend on graceful shutdown. They should be equally usable even with a crash caused, say, by power failure.

A lock file should be used by opening (creating, if necessary) the lock file and locking it. If for some reason the process which locks the file goes away, then the lock is removed from the file and some other process should be able to lock the file. There should never be a reason to actually remove the file.

But this message:
Quote:

Warning: The serial port "/dev/term/a" cannot be opened for use.
Please verify that it is entered correctly and that it is not already in use.
rather implies that the problem is not with the lock file, but with the serial port itself. Is that true? Look at the code which generates this message, and determine whether the message is triggered by:
  1. a failure opening the serial port itself;
  2. a determination that the lock file already exists;
  3. a failure to lock the lock file; or
  4. something else.
If it's (1), then maybe the code isn't using the lock file properly, and thinks that it's OK to continue, and then fails on opening the serial port.

If it's (2), then the code is not using lock files properly. Don't blow up if the lock file exists; blow up if you can't lock it. That's why it's called a lock file.

If it's (3), then somehow there's a process which already (or still) has the lock file locked. It's quite possible for you to delete a file that has a lock on it (and the lock will still continue to be on this phantom file). But if you delete it, your program can then go in and create a new lock file and lock it, oblivious to the fact that some other process thinks it still has the file locked.

This is the most likely possibility, since deleting the lock file lets your program run. Find out why it still has a lock on it. Which process has that file still open? To do that, when you encounter this difficulty, instead of just deleting the file, use lsof. For more information, enter this at the shell prompt:
Code:

man lsof
This should open your eyes to what else is going on that you might not have considered.

Hope this helps.

smartgupta 09-22-2009 01:40 AM

Hi,

I checked the code and i found out that when i restart the system,locked file is present in /var/lock dir but my application is unable to communicate it and throws the exception-

Quote:

Warning: The serial port "/dev/term/a" cannot be opened for use.
Please verify that it is entered correctly and that it is not already in use.
it shows that after restarting the system link is being broken but I am unable to get the right thing to resolve it.

Please provide ur valuable ideas..

thanks in advance....

lutusp 09-22-2009 03:16 AM

Quote:

Originally Posted by smartgupta (Post 3692493)
Hi,

I checked the code and i found out that when i restart the system,locked file is present in /var/lock dir but my application is unable to communicate it and throws the exception-



it shows that after restarting the system link is being broken but I am unable to get the right thing to resolve it.

Please provide ur valuable ideas..

thanks in advance....

If the orphan lock file is created by your application, then the remedy is to rewrite your application to remove the lock file no matter how it is shut down.

1. Change your code to be more bulletroof about how it shuts itself down.

2. Test your changes by running the application and then forcing an abnormal exit:

Code:

# kill -9 (your process ID)
See if the lockfile is removed during this kind of exit. If it is not, learn how to properly exit shell scripts.


All times are GMT -5. The time now is 02:56 AM.