in reply to Locking/unlocking program execution using a queue

If you're prepared to wait indefinitely for the lock, then your scripts should run in the correct order just with your simple model.
#!/usr/bin/perl -w use strict; use Fcntl ':flock'; # import LOCK_* constants my $lockfile_path = "./lockfile"; open(LOCKFILE, ">$lockfile_path") or die "Can't open $lockfile_path: $ +!"; flock(LOCKFILE,LOCK_EX); print "got lock @ARGV\n"; sleep(10); print "finished sleeping @ARGV\n"; flock(LOCKFILE,LOCK_UN);
If you run multiple instances of this script like so...
./test.pl 1 & ./test.pl 2 & ./test.pl 3 & ./test.pl 4 &
...you'll see them running in the correct order. As you point out, this does break if the flock fails and you need to get back in the queue though.

Replies are listed 'Best First'.
Re^2: Locking/unlocking program execution using a queue
by RazorbladeBidet (Friar) on Mar 31, 2005 at 13:17 UTC
    I didn't see that documented anywhere (and I have no man pages installed for flock, fcntl or lockf on the AIX machine I am working on!) so I tested it out.

    By hand it seemed to work... but running it in a script of
    #!/bin/ksh
    
    testlock.pl 1 &
    testlock.pl 2 &
    testlock.pl 3 &
    testlock.pl 4 &
    testlock.pl 5 &
    
    gave me this:

    got lock 4
    finished sleeping 4
    got lock 3
    finished sleeping 3
    got lock 1
    finished sleeping 1
    got lock 5
    finished sleeping 5
    got lock 2
    finished sleeping 2
    
    So I don't think that's supported on all platforms
    --------------
    "But what of all those sweet words you spoke in private?"
    "Oh that's just what we call pillow talk, baby, that's all."
      I had only tried it by hand, but it worked fine when I ran it as per your ksh script just now. I'm on Solaris 5.8. Guess it's not something you can rely on though.
        Yes, unfortunately, I'm finding this to be the case no matter what I do. Whatever locking mechanism I choose will have an issue with execution order. I'm currently on the hunt for an execution start time that's more granular than $^T...
        --------------
        "But what of all those sweet words you spoke in private?"
        "Oh that's just what we call pillow talk, baby, that's all."