in reply to Locking/unlocking program execution using a queue

Use a FIFO :)

(This solution may not scale well if there are many directories, but you'll be able to adapt to your case)

Create a fifo (mknod -p fifoname) and have a script continuosly read from it. If there's nothing in the FIFO the process won't consume resources.

Programs to run are simply inserted in the FIFO as strings; in the cron job, you substitute the script execution with a write into the FIFO, i.e. you prepend echo and append a redirection to the FIFO. The reader script will extract and execute them, via some eval mechanism, or fork(), or whatever you like.

There are tons of theme variations, but I think this should give you the idea.

Flavio

Don't fool yourself.

Replies are listed 'Best First'.
Re^2: Locking/unlocking program execution using a queue
by RazorbladeBidet (Friar) on Mar 30, 2005 at 15:31 UTC
    Interesting idea. However, I don't have much control over the system and the reader script could be killed accidentally (or intentionally), so that's a little concerning.

    What I was hoping for was some sort of IPC... the same script adding to the FIFO and being awoken.

    The only problem with the sleep above is that the processes could get out of order... I'd rather keep it all in the same script if possible.
    --------------
    "But what of all those sweet words you spoke in private?"
    "Oh that's just what we call pillow talk, baby, that's all."
      You can take into account the accidental kill by putting some monitor script in the crontab itself - but you would have an explosion of scripts :)

      My previous solution was clean in the sense that it voids active waits. If you don't bother having slightly active waits, you could keep a queue file in each directory, with write access protected via a lock, in which you append your 'name' (e.g. pid number), then keep controlling until it's your turn. When you're done, you simply remove yourself from the top of the file (using the same locking to prevent multiple writings). In this way, the access to the queue file should be rather quick, then a check every second should suffice without slowing your machine to a crawl.

      Note that if you use PIDs, you can also check if the current first-in-the-list is alive, just to prevent infinite locking by a badly dead script.

      Flavio

      Don't fool yourself.
        There is also a problem if the reading script stalls and requests continue to be written, overflowing the FIFO's buffer. Nonetheless it would normally work and if the commands are short a lot of them would fit into 4kB or whatever size the FIFO buffer is.