tags:

views:

338

answers:

3

Hi,

this is a bit related to this question.

I'm using make to extract some information concerning some C programs. I'm wrapping the compilation using a bash script that runs my java program and then gcc. Basically, i'm doing:

make CC=~/my_script.sh

I would like to use several jobs (-j option with make). It's running several processes according to the dependency rules.

If i understood well, I would have as many instances of the jvm as jobs, right ?

The thing is that i'm using sqlite-jdb to collect some info. So the problem is how to avoid several processes trying to modify the db at the same time ? It seems that the sqlite lock is jvm-dependant (i mean one lock can be "see" only inside the locking jvm), and that this is the same for RandomAccessFile.lock().

Do you have any idea how to do that ? (creating a tmp file and then looking if it exists or not seems to be one possibility but may be expensive. A locking table in the dB ? )

thanks

+2  A: 

there are several solutions for this. if your lock should be within the same machine, you can use a server socket to implement it (The process that manages to bind to the port first owns the lock, other processes waits for the port to become available).

if you need a lock that span across multiple machines you can use a memcached lock. this will require a memcached server running. I can paste some code if you are interested in this solution.

you can get Java library to connect to memcached here.

Omry
just a pointer for the memcached server should be fine, thanks.
LB
updated the annswer to include memcached and memcached java lib links.
Omry
+6  A: 

java.nio.channels.FileLock allows OS-level cross-process file locking.

However, using make to start a bash scripts that runs several JVMs in parallel before calling gcc sounds altogether too Rube-Goldbergian and brittle to me.

Michael Borgwardt
Love the reference... for me too, that's too brittle. But I've to capture all the parameters given to gcc, and the system built is quite complex (and the building system too), i didn't have much choice.
LB
A: 

You may try Terracotta for sharing objects between various JVM instances. It may appear as a too heavy solution for your needs, but at least worth considering.

Grzegorz Oledzki