Re: namepipe



timothy ma and constance lee wrote:
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?

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.
.



Relevant Pages

  • Re: atomic operations...
    ... No MUTEXS! ... the Monitor class in .NET isn't strictly speaking a Windows mutex function). ... Until you've measured and confirmed a genuine performance problem, IMHO you should not care so much about the specific synchronization mechanism used. ... The Java implementation trades synchronization in the class itself for synchronization in the memory manager. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: About synchronization methods
    ... The Java guys themselves admit that what Java has is ... not the true monitor but some low level means to implement a monitor- ... Once you name the language, please name the one true language that is worthy enough to implement the runtimes/virtual-machine for its holiness. ... In fact, as I've pointed out before in detail, the whole "language supported" synchronization model of Java is nothing more than very shallow semantic sugar over the standard library mutex and condition variable synchronization model. ...
    (comp.programming.threads)
  • Re: Running Special Programs through Runtime exec()
    ... Why would Java lock up on trying to run a program and NOT throw an error? ... BufferedReader eStream = new BufferedReader (new ... comparision to the input stream data. ... is blocked while the error stream generates enough data to fill the ...
    (comp.lang.java.programmer)
  • Re: how to create random noise, convert to xml and send to tcp/ip socket?
    ... I am new to Java, and really need to your help in this moment. ... and convert the signal into xml format and send to a TCP/IP socket. ... stream into characters. ... I have not been able to find a Base64 ...
    (comp.lang.java.programmer)
  • Rendering a page handling a File Download Dialog causes page state to be lost
    ... On a webserver, I have a set of client pages that are based on XML technology with events triggered on each action, these events are written in java. ... Does anyone know of how I could save the session state or preserve the session state through java? ... The java class FileDownloadRenderer supports streaming binary and text.It has methods to handle a given input stream, ... The following code renders the page A ...
    (comp.lang.java.programmer)