How to send messages to a child process

Discussion in 'Java' started by Ross, Nov 23, 2010.

  1. Ross

    Ross Guest

    How do you send messages to a child process from inside Java?

    I'm running a process by first getting a Runtime from
    Runtime.getRuntime();

    I then exec a process using Runtime.exec( String [] );

    I then get the OutputStream to send info to the process using
    Process.getOutputStream();

    I then send lines of text to the OutputStream by sending blocks of
    bytes. These strings are terminated with newlines.

    At the other end (c++ program), I'm using cin to read from standard
    output.

    But, it seems, whatever I do, the data doesn't get through to the c++
    program unless I close the outputstream. Which means that I have to
    spark a new process for each command. Which doesn't work. I need to be
    sending a continual stream of realtime commands to the c++ program.
    How do I do this? Surely there is a way?
     
    Ross, Nov 23, 2010
    #1
    1. Advertising

  2. Ross

    Ross Guest

    I tried using read( 0, _, _ ) in the C++ program instead of using
    getline on cin, no improvement. Data still gets stuck between the Java
    parent process, and the c++ child process.

    Any ideas?
     
    Ross, Nov 23, 2010
    #2
    1. Advertising

  3. Ross

    Nigel Wade Guest

    On 23/11/10 16:35, Ross wrote:
    > I tried using read( 0, _, _ ) in the C++ program instead of using
    > getline on cin, no improvement. Data still gets stuck between the Java
    > parent process, and the c++ child process.
    >
    > Any ideas?


    Have you tried flushing the stream after each write?

    Are you reading any response that the C++ program may return in a
    different thread, or otherwise ensuring that the C++ program doesn't
    block on output?

    --
    Nigel Wade
     
    Nigel Wade, Nov 23, 2010
    #3
  4. Ross

    Ross Guest

    Odd. The documentation for OutputStream says that the flush method
    does nothing. http://download.oracle.com/javase/1.4.2/docs/api/java/io/OutputStream.html#flush()

    I thought I'd checked whether flush() solves the problem, and it
    didn't. After you both recommended flush(), I gave it a second try. I
    wrote new trial Java and this time an external c program, both
    included below. Now it's working. Messages get sent to the c program
    and written to output.txt continuously, while without os.flush();,
    they all arrive only when the os is closed. I don't know if this is
    because I'm currently working on Linux at home rather than Mac at
    work, but I'll find out tomorrow.

    So, at the moment, this does appears solved. Thanks. I hope :)

    // Test.java

    import java.io.*;

    public class Test
    {
    public static void main( String args[] ) throws Exception
    {
    Runtime r = Runtime.getRuntime();

    Process p = r.exec( new String[] { "./test" } );

    OutputStream os = p.getOutputStream();

    for ( int index = 0; index < 100; index++ )
    {
    String message = "Message #" + (index+1) + "\n";
    System.out.println( "Java Program sending: " + message );
    os.write( message.getBytes() );
    os.flush();

    Thread.sleep( 500 );
    }

    os.close();

    p.waitFor();

    }
    }









    // test.c

    #include <stdio.h>
    #include <string.h>

    int main( int argc, char *argv[] )
    {
    char buffer[1024];
    int outFD;

    outFD = creat( "output.txt", 0600 );

    while( fgets( buffer, 1024, stdin ))
    {
    write( outFD, buffer, strlen( buffer ));
    }

    close( outFD );
    }
     
    Ross, Nov 23, 2010
    #4
  5. Ross

    Ross Guest

    This is all working now, both on Linux and Mac. So, problem
    convincingly solved. I thought that I had found an old thread saying
    that flush didn't work, and when a quick experiment suggested that it
    didn't (presumably due to some other bug), didn't try it again. Thanks
    muchly.

    The Process object gives me a BufferedOutputStream which inherits from
    FilteredOutputStream. And flush() works on that fine as you say.
     
    Ross, Nov 24, 2010
    #5
  6. Ross

    Roedy Green Guest

    On Tue, 23 Nov 2010 08:24:04 -0800 (PST), Ross <>
    wrote, quoted or indirectly quoted someone who said :

    >How do you send messages to a child process from inside Java?


    You can send/receive via the System.out System.err and System.in. You
    need to set up threads to prevent blocking.
    http://mindprod.com/jgloss/exec.html


    You an also talk over a TCP/IP port.

    You can talk via a database.

    There are many other ways. see
    http://mindprod.com/jgloss/remotefileaccess.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    If you give your kitchen floor a quick steam mop every few days, you will find you never have to get out buckets and brushes for deep cleaning. Similary, if you keep your code tidy, refactoring as you go, you probably won't need major rewrites.
     
    Roedy Green, Nov 26, 2010
    #6
  7. Ross

    Roedy Green Guest

    On Tue, 23 Nov 2010 08:35:31 -0800 (PST), Ross <>
    wrote, quoted or indirectly quoted someone who said :

    >I tried using read( 0, _, _ ) in the C++ program instead of using
    >getline on cin, no improvement. Data still gets stuck between the Java
    >parent process, and the c++ child process.


    The tricks are documented at http://mindprod.com/jgloss/exec.html

    The usual problem is you try to pull it off without threads, and it
    works to pass a few test chars, then fails when you go into
    production. The other common problem is failing to use flush().

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    If you give your kitchen floor a quick steam mop every few days, you will find you never have to get out buckets and brushes for deep cleaning. Similary, if you keep your code tidy, refactoring as you go, you probably won't need major rewrites.
     
    Roedy Green, Nov 27, 2010
    #7
  8. Ross

    Roedy Green Guest

    On Tue, 23 Nov 2010 08:35:31 -0800 (PST), Ross <>
    wrote, quoted or indirectly quoted someone who said :

    >I tried using read( 0, _, _ ) in the C++ program instead of using
    >getline on cin, no improvement. Data still gets stuck between the Java
    >parent process, and the c++ child process.


    The tricks are documented at http://mindprod.com/jgloss/exec.html

    The usual problem is you try to pull it off without threads, and it
    works to pass a few test chars, then fails when you go into
    production. The other common problem is failing to use flush().

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    If you give your kitchen floor a quick steam mop every few days, you will find you never have to get out buckets and brushes for deep cleaning. Similary, if you keep your code tidy, refactoring as you go, you probably won't need major rewrites.
     
    Roedy Green, Nov 28, 2010
    #8
  9. Ross

    Roedy Green Guest

    On Tue, 23 Nov 2010 08:35:31 -0800 (PST), Ross <>
    wrote, quoted or indirectly quoted someone who said :

    >I tried using read( 0, _, _ ) in the C++ program instead of using
    >getline on cin, no improvement. Data still gets stuck between the Java
    >parent process, and the c++ child process.


    The tricks are documented at http://mindprod.com/jgloss/exec.html

    The usual problem is you try to pull it off without threads, and it
    works to pass a few test chars, then fails when you go into
    production. The other common problem is failing to use flush().

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    If you give your kitchen floor a quick steam mop every few days, you will find you never have to get out buckets and brushes for deep cleaning. Similary, if you keep your code tidy, refactoring as you go, you probably won't need major rewrites.
     
    Roedy Green, Nov 28, 2010
    #9
  10. Ross

    Roedy Green Guest

    On Tue, 23 Nov 2010 08:35:31 -0800 (PST), Ross <>
    wrote, quoted or indirectly quoted someone who said :

    >I tried using read( 0, _, _ ) in the C++ program instead of using
    >getline on cin, no improvement. Data still gets stuck between the Java
    >parent process, and the c++ child process.


    The tricks are documented at http://mindprod.com/jgloss/exec.html

    The usual problem is you try to pull it off without threads, and it
    works to pass a few test chars, then fails when you go into
    production. The other common problem is failing to use flush().

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    If you give your kitchen floor a quick steam mop every few days, you will find you never have to get out buckets and brushes for deep cleaning. Similary, if you keep your code tidy, refactoring as you go, you probably won't need major rewrites.
     
    Roedy Green, Nov 28, 2010
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Rodriguez
    Replies:
    23
    Views:
    1,200
    David Schwartz
    Dec 9, 2003
  2. nushin
    Replies:
    1
    Views:
    484
    Donn Cave
    Jul 23, 2003
  3. Gauri
    Replies:
    3
    Views:
    196
  4. Gauri
    Replies:
    4
    Views:
    206
    -berlin.de
    Feb 28, 2007
  5. Z W
    Replies:
    0
    Views:
    194
Loading...

Share This Page