Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to send messages to a child process

Reply
Thread Tools

How to send messages to a child process

 
 
Ross
Guest
Posts: n/a
 
      11-23-2010
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?
 
Reply With Quote
 
 
 
 
Ross
Guest
Posts: n/a
 
      11-23-2010
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?
 
Reply With Quote
 
 
 
 
Nigel Wade
Guest
Posts: n/a
 
      11-23-2010
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

 
Reply With Quote
 
Ross
Guest
Posts: n/a
 
      11-23-2010
Odd. The documentation for OutputStream says that the flush method
does nothing. http://download.oracle.com/javase/1....am.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 );
}

 
Reply With Quote
 
Ross
Guest
Posts: n/a
 
      11-24-2010
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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-26-2010
On Tue, 23 Nov 2010 08:24:04 -0800 (PST), Ross <(E-Mail Removed)>
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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-27-2010
On Tue, 23 Nov 2010 08:35:31 -0800 (PST), Ross <(E-Mail Removed)>
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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-28-2010
On Tue, 23 Nov 2010 08:35:31 -0800 (PST), Ross <(E-Mail Removed)>
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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-28-2010
On Tue, 23 Nov 2010 08:35:31 -0800 (PST), Ross <(E-Mail Removed)>
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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-28-2010
On Tue, 23 Nov 2010 08:35:31 -0800 (PST), Ross <(E-Mail Removed)>
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
pexpect on windows - child process of another child process - quickquestion Z W Python 0 03-09-2013 10:00 PM
Parent process unable to read messages from child process Gauri Perl Misc 3 03-01-2007 02:06 AM
Parent process unable to read messages from child process Gauri Perl Misc 4 02-28-2007 11:18 PM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM



Advertisments