Problem with viewing stdout of C program

M

Milind Rao

I am calling a C program from Java using Runtime.exec(). I have a thread
reading from stdout and displaying each line to the console. The program
I'm invoking is very CPU intensive (99%). My problem is that everything the
C program printfs to stdout is not displayed until the C program terminates.
Then the thread in my Java program reading from stdout prints the whole
buffer at one shot to the console. That's not very helpful since I want to
see the progress of the C program. If I run the program from the command
line, there is no problem as I see the periodic outputs in the command
window.

If I call another C program which is not CPU intensive at all and sleeps in
between, I can see all the output in real time in the Java program. I also
ran "cmd.exe /c dir c:\i386" from the java program. That works fine too.
Running the problematic C program using "cmd.exe" showed the same results.

This is on Win2K with JDK 1.4

My questions are

1. Obviously the fact that the program is running at 99-100% CPU is causing
the Java thread to get starved. But the program runs for 10 minutes. At
some point thread switching should have occurred. So why am I not seeing
any out put at all until the C program terminates?

2. How come I can see the output on the command window? Does Windows do
something special for command windows?

3. Is there any way to fix this?

The thread reading stdout is quite simple.

private static class ReadProcessStream extends Thread
{
private BufferedReader iBR;
private InputStream iBIS;
private String iStreamName;

ReadProcessStream(String theStreamName, InputStream theInputStream)
{
iBR = new BufferedReader(new InputStreamReader(theInputStream));
iStreamName = theStreamName;
start();
}

public void run()
{
try {
String currLine;
Thread thisThread = Thread.currentThread();
byte[] buf = new byte[256];
while ((!thisThread.isInterrupted()) &&
(currLine = iBR.readLine()) != null) {
System.out.println(currLine);
}
if (thisThread.isInterrupted())
System.out.println("Interrupted while reading from " +
iStreamName);

} catch(IOException e) {
System.err.println("IOException reading from " +
iStreamName + "!");
}
}
}

Thanks
Milind
 
G

Gordon Beaton

3. Is there any way to fix this?

OS scheduling issues can't be solved by the Java application. Perhaps
a workaround is to have the C program yield occasionally, for example
after each call to printf(). At any rate this is hardly a Java issue.

However the following might help. Try adding one of these near the
start of your C program, *before* the first call to printf():

setvbuf(stdout, NULL, _IOLBF, 512);
or
setvbuf(stdout, NULL, _IONBF, 0);

/gordon
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top