Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

re: namepipe

Thread view: 
timothy ma and constance lee - 22 Sep 2007 02:11 GMT
Sir

I got a problem:

Program A need to wirte a file and then program B will use that file after
Program A release the lock of the file.

I think i may use namepipe in C but how about if using java. Any other good
suggestion?

THanks
Ben Phillips - 22 Sep 2007 07:01 GMT
> Sir
>
[quoted text clipped - 5 lines]
> I think i may use namepipe in C but how about if using java. Any other good
> suggestion?

A lot of problems that are best solved in C by using multiple processes
communicating are, in Java, better solved by using multiple threads
inside of a single JVM process. In this instance, one just has a
producer/consumer pattern with a stream-like object that has a
synchronized put and get of some sort. The java.util classes provide
implementations of Deque, Stack, and Queue that you might synchronize on
and use for inter-thread communication (e.g. a job queue). For a
character stream, use a Stream you make that looks like an input stream
to one thread and an output stream to the other, with the input stream
read methods blocking until the other thread writes to the output stream
interface of the object, and synchronization used. Read up on
synchronization, Object.wait(), Object.notify(), and then check up on
java.util.concurrent. Synchronization and other threading matters are
covered in Sun's Java tutorials at java.sun.com and wait, notify, Deque,
Stream, and the like have in-depth javadocs ("synchronized" itself
doesn't as it's a language keyword, though). Packages of interest:
java.util, java.util.concurrent, java.lang (Object and Thread, and
various exceptions), and java.io (streams).

If you must go with interprocess communication, streams are again the
way to go, but you'll have no simple and reliable file locking mechanism
from in Java (at least without resorting to JNI). If the host operating
system provides an atomic nonexistent-file-creation shell command you
can System.exec this to atomically create lockfiles with a failure
return if the lockfile's already been created by another process, and
implement cooperative locking in the manner typical on Unix systems. You
might also want to look into RMI and other such complicated file-less
methods of Java interprocess communication, or replace using a file with
using a database that maintains transactional integrity with concurrent
uses (you get an atomic lock-creation primitive, at minimum, since any
DBMS worth even its weight in cat turds will provide atomic transactions
in general).

You may even be able to use System.exec from one process to create the
other and pipe an OutputStream to the new process's input; check the
System and ProcessBuilder javadocs and experiment to see if you can get
a simple demo working.

But your best bet is probably with turning these processes into threads
and sharing a data structure such as a queue. C provides no standard
threading or internal synchronization tools, which results in the C
idiom being IPC with external files, named pipes, lockfiles, and the
like. Java does provide standard threading and synchronization tools.
Roedy Green - 22 Sep 2007 20:56 GMT
On Sat, 22 Sep 2007 01:11:48 GMT, "timothy ma and constance lee"
<timcons1@shaw.ca> wrote, quoted or indirectly quoted someone who said

>Program A need to wirte a file and then program B will use that file after
>Program A release the lock of the file.

You could do this with an aux dummy lock file that exists while the
file being shared is busy.

In Java 1.6 there are some new file locking tools. I have not played
with them.  I don't even recall what they are called.  Check the
release notes.

There are other ways to tackle the problem.

1. if A and B are threads in the same JVM, they can talk to each other
with a pipe. see http://mindprod.com/applet/fileio.html

2. A and B can communicate via a TCP/IP socket.
http://mindprod.com/jgloss/tcpip.html

3. A can drop the info off in an SQL database.  SQL deals with
locking. http://mindprod.com/jgloss/sql.html

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.