Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4429237

Parent process will not send data to Child process System.in

    XMLWordPrintable

Details

    • Bug
    • Resolution: Not an Issue
    • P4
    • None
    • 1.3.0
    • core-libs
    • x86
    • windows_98

    Description



      Name: yyT116575 Date: 03/22/2001


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      I run my main program, the "Parent" program. This program uses the
      Runtime.exec() command to call the java interpreter on the "Child" program.
      The two are supposed to communicate using the input, output, and error streams
      gotten by calling the Process functions .getInputStream(), .getOutputStream(),
      and .getErrorStream(). The parent process is able to read the child process's
      System.out and System.err just fine. However, when the parent process writes
      to the OutputStream created by .getOutputStream(), the information doesn't seem
      to reach the child process.

          To verify this, I wrote the following test program which tests all three
      streams and confirmed that child to parent communication works, but not vice-
      versa. Note that the program performs repeated tests in case there are some
      intermittant problems with the communication:

      import java.io.*;

      public class StreamTest {
        InputStream outFromChild;
        OutputStream inToChild;
        InputStream errFromChild;

        Thread outThread;
        Thread errThread;

        boolean threadGo = true;

        Process child;
        static StreamTest that;

         /* Change to name of local java interpreter, or just replace with
          * "java StreamChild". In this case, I gave the full path name in case I
          * had some old java interpreter floating around and the exec command was
          * finding that instead of 1.3.0.
          */
        static final String childName = "c:\\jdk1.3\\bin\\java.exe StreamChild";

        public static void main(String[] worthlessJunk) {
          that = new StreamTest();
          try {
            DataOutputStream toChild = new DataOutputStream(that.inToChild);
            that.inToChild.write(84);
            /* ASCII 'T' character, verify that there's not a problem with DataOutputStream. */
            toChild.writeBytes("his is data written to child process input.");
            Thread.sleep(5000);

            toChild.writeBytes("More data passed to child process input.");

            Thread.sleep(5000);

            toChild.writeBytes("Yet more data for child process input.");

            Thread.sleep(15000);
            toChild.writeBytes("Final data for child process input.");
            Thread.sleep(20000); // Wait for child to send stuff back.

          } catch (InterruptedException e) {
            System.err.println("Error: failure to let sleeping threads lie.");
            e.printStackTrace();
          } catch (IOException e) {
            System.err.println("IO crash and burn. Now what!?");
            e.printStackTrace();
          }
          that.threadGo = false;
          that.child.destroy();
        }

        public StreamTest () {
          try {
            child = Runtime.getRuntime().exec("c:\\jdk1.3\\bin\\java.exe StreamChild");
          } catch (IOException e) {
            System.err.println("Can't start child.");
            System.exit(666);
          }
          outFromChild = child.getInputStream();
          inToChild = child.getOutputStream();
          errFromChild = child.getErrorStream();

          outThread = new Thread(new Runnable() {
            public void run() {
              try {
                int someRandomByte;
                FileOutputStream f = new FileOutputStream("outFromChild.txt");
                DataOutputStream dataStream = new DataOutputStream(f);
                f.write(84); // ASCII 'T' character
                dataStream.writeChars ("his is a test of outFromChild file.\n");
                while ( threadGo ) {
                  someRandomByte = outFromChild.read();
                  dataStream.write(someRandomByte);
                }
              } catch (Exception e) {
                System.err.println("outThread crashes and burns.");
                e.printStackTrace();
              }
            }
          });

          errThread = new Thread(new Runnable() {
            public void run() {
              try {
                int someRandomByte;
                FileOutputStream f = new FileOutputStream("errFromChild.txt");
                DataOutputStream dataStream = new DataOutputStream(f);
                f.write(84); // ASCII 'T' character
                dataStream.writeChars ("his is a test of errFromChild file.\n");
                while ( threadGo ) {
                  someRandomByte = errFromChild.read();
                  dataStream.write(someRandomByte);
                }
              } catch (Exception e) {
                System.err.println("errThread crashes and burns.");
                e.printStackTrace();
              }
            }
          });
          outThread.start();
          errThread.start();

        }

      }


      import java.io.*;

      class StreamChild {
        Thread inThread;
        boolean threadGo = true;
        public static void main (String[] worthlessJunk) {
          try {
            StreamChild theChild = new StreamChild();
            System.out.println("Written to child System.out.");
            System.err.println("Written to child System.err.");
            Thread.sleep(2000);
            System.out.println("Written to child System.out.");
            System.err.println("Written to child System.err.");
            Thread.sleep(5000);
            System.out.println("Written to child System.out.");
            System.err.println("Written to child System.err.");
            Thread.sleep(10000);
            System.out.println("Written to child System.out.");
            System.err.println("Written to child System.err.");
          } catch (InterruptedException e) {
            System.err.println("Child Error: Didn't let sleeping threads lie.");
            e.printStackTrace();
          }

        }
        public StreamChild () {

          inThread = new Thread(new Runnable() {
            public void run() {
              try {
                int someRandomByte;
                FileOutputStream f = new FileOutputStream("childIn.txt");
                DataOutputStream dataStream = new DataOutputStream(f);
                f.write(84); // ASCII 'T' character
                dataStream.writeChars("his is a test of childIn file.\n");
                while ( threadGo ) {
                  someRandomByte = System.in.read();
                  dataStream.write(someRandomByte);
                }
                dataStream.writeChars("End of reading.");
              } catch (Exception e) {
                System.err.println("Child inThread Crashes and burns.");
                e.printStackTrace();
              }
            }
          });
          inThread.start();

        }
      }

      When the java interpreter is executed on "StreamTest.class", this program
      generates three data files with output results:

      outFromChild.txt:
      T h i s i s a t e s t o f o u t F r o m C h i l d f i l e .
      Written to child System.out.
      Written to child System.out.
      Written to child System.out.
      Written to child System.out.
      ?

      errFromChild.txt
      T h i s i s a t e s t o f e r r F r o m C h i l d f i l e .
      Written to child System.err.
      Written to child System.err.
      Written to child System.err.
      Written to child System.err.
      ?

      childIn.txt
      T h i s i s a t e s t o f c h i l d I n f i l e .

      Note that although a number of characters were allegedly passed to the
      child process by the parent, none of them seem to have made it to childIn.txt.
      If I execute the child process "StreamChild.class" directly and start typing
      random gibberish, I get a childIn.txt file that contains something like this:

      T h i s i s a t e s t o f c h i l d I n f i l e .
      thiejfkdjfkfjdslgdlkjdsfdkfjdthf
      dkekthkjdkdkfjthd
      ekrlekjrkjfdkdf
      kjdlkjdslkjfdlkjfdslk

      Obviously data is getting stuck somewhere in the Stream generated by
      Process and is never reaching the child process.
      (Review ID: 119180)
      ======================================================================

      Attachments

        Activity

          People

            kkladkosunw Konstantin Kladko (Inactive)
            yyoungsunw Yung-ching Young (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: