Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Ensuring only one copy of a perl script is running at a time

by PockMonk (Beadle)
on Dec 19, 2006 at 16:52 UTC ( #590699=note: print w/ replies, xml ) Need Help??


in reply to Ensuring only one copy of a perl script is running at a time

"A workmate asked me the best way to ensure that only one copy of his Perl script is running at a time".

I just developed a perl script that needed to ensure exactly that since it is CGI and can be run my multiple users over the internet and I need to ensure only one conucrrent usage. I simply created a file called "status.txt" ont he server, and had the script overwrite its status on the file contents at start and exit - 0 was disabled, 1 was enabled but not in use, 2 was enabled and in use. I'm sure its not the quickest or best way, but as always with Perl, TMTOWDI and all that, and its simple and works just fine.

It also has the benefit of letting you examine the file contents yourself and deciding on action based on them. You could also use this to set a limit of say 2 concurrent access, or 3 or 4 or... you get the point, by simply incrementing on each script start and decrementing (is that a word?) on each script exit.


Comment on Re: Ensuring only one copy of a perl script is running at a time
Re^2: Ensuring only one copy of a perl script is running at a time (race)
by tye (Cardinal) on Dec 19, 2006 at 17:25 UTC

    It sounds like it is time for you to update your computer science knowledge by learning about race conditions.

    I need to ensure only one conucrrent usage [....] works just fine

    It works just fine as far as you have noticed so far. It certainly doesn't "ensure" only one concurrent use; it more like usually prevents more than one concurrent use. (:

    You code must perform the following steps:

    1. Check current status
    2. if not 1 then exit
    3. Set current status to 2
    4. Do work
    5. Set current status back to 1

    And, in a modern computer system, CPU resources are shared, so each process that is serving a CGI request can be interrupted between any of those steps (or in the middle of steps) in order to let some other process do some work for a bit. Two CGI requests coming it at roughly the same time can thus perform those steps in the following order:

    One process Other process my $status= CheckStatus(); exit if 1 != $status; my $status= CheckStatus(); exit if 1 != $status; SetStatus( 2 ); SetStatus( 2 ); DoWork(); DoWork(); SetStatus( 1 ); SetStatus( 1 );

    Note that they both see the status as "1" and both end up running concurrently. This is why operating systems provide locking mechanisms and why you often need to use such.

    - tye        

      You will be happy to learn that I do use flock on opening the file so there is no danger of what you describe happening.

      Rather than locking a file for the duration of the script, I simply lock it while changing its status etc. As posted, this allows me greater flexibility to post more info than just in use/not in use by using the file content to write codes to. Thanks anyway!

      Dan
Re^2: Ensuring only one copy of a perl script is running at a time
by ikegami (Pope) on Dec 19, 2006 at 17:32 UTC

    That's wrong. It suffers from a race condition.

    +===============================+===============================+ | Process 1 | Process 2 | +===============================+===============================+ | open the status file | | T | read the status (status is 1) | | i +-------------------------------+-------------------------------+ m | | open the status file | e | | read the status (status is 1) | | | | write 2 to the status file | | | | proceed | | +-------------------------------+-------------------------------+ v | write 2 to the status file | | | proceed | | +===============================+===============================+

    If you use flock, then it's just an extention of what the OP posted.

    ( Oops! Seems like tye posted something similar when I was writing this node. )

      Suggest you read my response posted 4 minutes *before* your reply.

      20061220 Janitored by Corion: Content restored

        Suggest you read my update posted 15 minutes *before* your reply.

        (tye's post wasn't there when I started replying, so your reply to his post wasn't either!)

        Citing tye:
        It sounds like it is time for you to update your computer science knowledge by learning about race conditions.
        When a question/issue raises up, it's likely that more than one person will try to answer. This is not even a shared CPU environment: it's true concurrency. But humans tend to have personal ways of doing stuff: there's the verbose one, the one that uses only two fingers to write on the keyboard, the one that answers to the telephone... and in the end you have that many answers overlap each other, even if their times are quite different. It's like a race... a race condition :)

        In this case, I'd also enhance your answer to the OP giving enough evidence of the need to use flock (and marking such a modification clearly, in order to keep the following answers "meaningful"): the fact that your personal script used flock was not entirely evident :)

        Flavio
        perl -ple'$_=reverse' <<<ti.xittelop@oivalf

        Don't fool yourself.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://590699]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2014-08-20 21:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (124 votes), past polls