jeanluca has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

when I think more and more about the problem challenge I'm dealing with, the solution doesn't seem to get any closer.....

The program in question normally only reads stuff, so multiple instances are OK. However, when the options, lets say -w is used, some writing will be involved too. Meaning that there can only be 1 instance running with the -w options.

It seems that LockFile::Simple is a good solution, but if possible I would like to have a solution that doesn't create any files!
I tried Unix::PID as well but it seems there is now way to tell what the commanline agruments are of the instance already running.

Any other suggestions how to determine what the commandline parameters are from an other instance ?

Thnx a lot
LuCa

Replies are listed 'Best First'.
Re: how many instances are running ?
by davorg (Chancellor) on Mar 06, 2007 at 09:52 UTC
      I always get the feeling that there are always problems involved with lockfiles. I can think of a 'permission denied' when creating one. I can think of a situation that the instance died unexpectedly. And what if different users run the same program, can they remove eachother lockfiles if necessary ?
      I cannot imagine that all those examples are just nonsense, or is it ?

      Anyway, it would be very nice to have this locking mechanism based on PIDs and the COMMANDs that were used!

      LuCa
        jeanluca,

        davorg has the right solution. You can have lots of instances of a program that reads a file or filehandle, but they will fail to read as soon as one instance starts up to lock and write that same filehandle. You need to accomodate that fail-to-read condition as well as fail-to-lock. It's really simple. Stevens is a good reference, everybody should (and most sysprogs do) own this.

        Reading the /proc filesystem (through ps) to read PIDs & arguments is the way to do what you're trying to do, at least on Linux. You can then parse the response. It's not the way to solve your original problem, though.

        Your use of '-w' for your write flag has bart scratching his head. He thinks you're referring to Perl's -w warnings-on switch. /me injects coffee into bart. :)

        HTH. :D

        Don Wilde
        "There's more than one level to any answer."
Re: how many instances are running ?
by Moron (Curate) on Mar 06, 2007 at 12:10 UTC
    To find a loophole in your specifications, the program could always flock the Perl file itself on receiving the -w parameter and unlock itself when done writing.

    Alternatively, Proc::ProcessTable::Process can retrieve the full command line of all the processes.

    -M

    Free your mind

Re: how many instances are running ?
by jeanluca (Deacon) on Mar 06, 2007 at 13:41 UTC
    Monks

    Thnx a lot for all the suggestions!!
    Not only do I understand file locking a lot better (also thnx to bart although he confused the -w), but also I learned there is no really other way to prefent multi occurences of the same program without FileLocks.
    The script I posted above does not only work on Linux, but it will also produce useful output on Unix (change 'aux' into '-ef').
    Because I don't want to use lock-files, I think I will write my own LockFile.pm based on the above script (it will be very basic).

    If there is a really good argument agains my approach I will abandon my precious and use FileLocks :)

    LuCa