Problem with viewing stdout of C program

Discussion in 'Java' started by Milind Rao, Jan 13, 2004.

  1. Milind Rao

    Milind Rao Guest

    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
     
    Milind Rao, Jan 13, 2004
    #1
    1. Advertising

  2. On 13 Jan 2004 07:59:55 -0800, Milind Rao wrote:
    > 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

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Jan 13, 2004
    #2
    1. Advertising

  3. Milind Rao

    Milind Rao Guest

    setvbuf worked. Thanks.

    Regards
    Milind
     
    Milind Rao, Jan 13, 2004
    #3
    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. joanduan
    Replies:
    0
    Views:
    337
    joanduan
    Apr 10, 2004
  2. joanduan
    Replies:
    1
    Views:
    428
    Roman Vottner
    Apr 11, 2004
  3. Elad
    Replies:
    0
    Views:
    420
  4. Andreas S
    Replies:
    3
    Views:
    274
    Eric Hodel
    Dec 9, 2006
  5. Replies:
    2
    Views:
    353
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page