Brothers,
I'm seeking wisdom and potentially flashes of inspiration...
I have a process that receives files via NDM from an external partner in pairs - a data file and a status file. The files may arrive in any order, and could be anything from minutes apart to fractions of a second. I'm trying to create a process that populates the file content into an Oracle database via a Perl script triggered after each file arrives. Although the files can arrive in pseudo parallel, I need to process them in sequence, so I'm trying to semaphore lock them (
IPC::Semaphore). If they arrive separated by, say, 1 second the lock works nicely, but if they arrive a tenth of a second apart, both Perl processes say that they're the first one and create and initialize the semaphore.
$sem = IPC::Semaphore->new( 4321, 1, S_IRUSR | S_IWUSR );
if ( $sem ) {
# Semaphore already exists so just open it
print "Semaphore already exists - just open it\n";
$sem = IPC::Semaphore->new( 4321, 1, S_IRUSR | S_IWUSR );
} else {
# The semaphore didn't already exit so create it
print "Create semaphore \n";
$sem = IPC::Semaphore->new( 4321, 1, IPC_CREAT | S_IRUSR | S_IWUSR
+ );
print "Semaphore created\n";
$sem->setval(0,1);
print "Semaphore initialised\n";
}
print "Locking other threads\n";
$sem->op(0, -1, SEM_UNDO);
Depending on exact timing, I see that it's possible for the first process to create the semaphore and attempt to lock the other process, but the other one running a fraction behind has not detected the creation so it does its own and sets the semaphore to 1, so revoking it's partner's lock!
I can't see a fool proof way of getting around this, so any divine inspiration gratefully received.
Jerry
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.