runtime.exec child process - C program, I/O blocked

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • angelotti
    New Member
    • Jun 2007
    • 6

    runtime.exec child process - C program, I/O blocked

    Hi everyone , i am banging my head over the following problem for a couple of weeks now: i am starting a MyProg.exe(a simple C app) from a java class with .exec() as a system process(not in it's own console) and i would like to communikcate with its I/O before it is finished. I don't have this issue if for instance i am starting another java app.

    The problem i meet is that the I/O is bloked while the process is being executed.When i terminate it , it is all released but it doesn't do me any good...

    Here is some source , i'd really a ppreciate some help as i am stuck here....

    [CODE=java] String[] command={"cmd", "/c","prog.exe "};
    Runtime rt = Runtime.getRunt ime();
    Process proc = rt.exec(command );
    InputStream stderr = proc.getInputSt ream();
    PrintWriter out = new PrintWriter(new OutputStreamWri ter(proc.getOut putStream()), true);

    String str="123";
    BufferedOutputS tream bufStr= (BufferedOutput Stream) proc.getOutputS tream();
    bufStr.write(23 1);
    bufStr.flush();

    InputStreamRead er isr = new InputStreamRead er(stderr);
    BufferedReader br = new BufferedReader( isr);
    String line = null;
    while ( (line = br.readLine()) != null)
    System.out.prin tln(line);


    int exitVal = proc.waitFor();
    System.out.prin tln("Process exitValue: " + exitVal);
    } catch (Throwable t)
    {
    t.printStackTra ce();
    }
    [/CODE]
    the C prog i am running:

    [CODE=c] #include <stdio.h>
    #include <conio.h>

    void main(void){

    int i;
    printf("sdfsdcd s");
    scanf("%d",&i);
    printf(" \n %d",i);

    }[/CODE]
    Last edited by r035198x; Jun 13 '07, 04:21 PM. Reason: added code tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by angelotti
    Hi everyone , i am banging my head over the following problem for a couple of weeks now: i am starting a MyProg.exe(a simple C app) from a java class with .exec() as a system process(not in it's own console) and i would like to communikcate with its I/O before it is finished. I don't have this issue if for instance i am starting another java app.

    The problem i meet is that the I/O is bloked while the process is being executed.When i terminate it , it is all released but it doesn't do me any good...

    Here is some source , i'd really a ppreciate some help as i am stuck here....

    [code=java]
    String[] command={"cmd", "/c","prog.exe "};
    Runtime rt = Runtime.getRunt ime();
    [/code]
    When you're starting an executable image there's no need to start a shell (cmd)
    first; just use "prog.exe" as the command.

    Originally posted by angelotti
    [code=java]
    String str="123";
    BufferedOutputS tream bufStr= (BufferedOutput Stream) proc.getOutputS tream();
    bufStr.write(23 1);
    bufStr.flush();
    [/code]
    Note that you're just writing a single byte here; not what your C process expects.
    Your C process expects a number, so better write "123\n" to the process.

    kind regards,

    Jos

    Comment

    • angelotti
      New Member
      • Jun 2007
      • 6

      #3
      Thanks for the input Jos but it doesn't help me. My problem is that the (C prog) exe process seems to be blocking his ouput stream until finishing!!? Which i can't see happening when i do the same with a Java app.

      What i also find interesting is that my Java basic class which starts the child (exe) thread seems to hang waiting exactly at the line where i ask it to read from the InputStream....

      if it rings a bell please write, anyone :)

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by angelotti
        Thanks for the input Jos but it doesn't help me. My problem is that the (C prog) exe process seems to be blocking his ouput stream until finishing!!? Which i can't see happening when i do the same with a Java app.

        What i also find interesting is that my Java basic class which starts the child (exe) thread seems to hang waiting exactly at the line where i ask it to read from the InputStream....

        if it rings a bell please write, anyone :)
        If your thread seems to 'hang' the C process isn't procucing any output and your
        Java process is waiting for it. In the mean time your Java process can't send any
        input to that other C process either. I always use different threads for reading and
        writing from/to external processes.

        kind regards,

        Jos

        Comment

        • angelotti
          New Member
          • Jun 2007
          • 6

          #5
          The thing is that my 'C' prog produces output - a couple of printf statements , and the innput towards the "C"prog is not blocked. For instance i have a couple of printfs and scanfs - if i push some data to fill the scanfs , after the "C" prog is termianted the whole ouput comes in the console....

          I am trying to emulate a interaction with this "C"prog through a java written web service for a browser, and that is why this doesn't work for me. I have also tryed to separate all the streams in different threads with the same result...

          Comment

          • smok
            New Member
            • Jun 2007
            • 2

            #6
            Hello, I have the same problem with interaction from Java app to C app. My C app gives some output and waits for input. I read its stdout and stderr streams in separate threads, and in java main thread I push some data to C app input.

            My stderr/stdout stream consumers also hang, like C app doesn't print anything to console. Also, if it prints something and then exits, I receive all its output in my consumers.

            It seems like C app's output just hangs out in some buffer until it's finished. Please post here if you find a solution...

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by smok
              Hello, I have the same problem with interaction from Java app to C app. My C app gives some output and waits for input. I read its stdout and stderr streams in separate threads, and in java main thread I push some data to C app input.

              My stderr/stdout stream consumers also hang, like C app doesn't print anything to console. Also, if it prints something and then exits, I receive all its output in my consumers.

              It seems like C app's output just hangs out in some buffer until it's finished. Please post here if you find a solution...
              Don't use buffered input streams then for reading from stdout and/or stderr.
              If your C process doesn't properly flushes its output streams the streams will
              only be flushed when the process exits. I'm almost sure it's a buffer problem;
              same for you and the OP who's thread you hijacked ;-)

              kind regards,

              Jos

              Comment

              • smok
                New Member
                • Jun 2007
                • 2

                #8
                Originally posted by JosAH
                Don't use buffered input streams then for reading from stdout and/or stderr.
                If your C process doesn't properly flushes its output streams the streams will
                only be flushed when the process exits. I'm almost sure it's a buffer problem;
                same for you and the OP who's thread you hijacked ;-)

                kind regards,

                Jos
                I don't use buffered input streams, just an InputStreamRead er around Process's InputStream (which is its stdout). Anyway, process.getInpu tStream().avail able() always returns 0...

                I just tried to call fflush(stdout) in my C app after printf(), and it works! I got its stdout in my java program exactly where I want :)
                thanks for advice!

                But I do see my app's output when I run it in console, without any explicit fflush-es. Why it is flushed "automatica lly" when I run it manually, and does not when I run it from JVM.... Modifying C app is a good workaround, but it won't help if I cannot modify my C app's code (and that is the case actually).

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by smok
                  I don't use buffered input streams, just an InputStreamRead er around Process's InputStream (which is its stdout). Anyway, process.getInpu tStream().avail able() always returns 0...

                  I just tried to call fflush(stdout) in my C app after printf(), and it works! I got its stdout in my java program exactly where I want :)
                  thanks for advice!

                  But I do see my app's output when I run it in console, without any explicit fflush-es. Why it is flushed "automatica lly" when I run it manually, and does not when I run it from JVM.... Modifying C app is a good workaround, but it won't help if I cannot modify my C app's code (and that is the case actually).
                  The C process console driver behaves differently from the one where no console
                  is present; the latter is buffered and won't show up until either the buffer is full
                  or the process terminates. Normal console output streams are just line buffered.

                  It's not Java's fault, it's the other side that acts up ;-)

                  kind regards,

                  Jos

                  Comment

                  Working...