The following program creates a child process, writes a string to it, and then sleeps. The child sees the string. (The child, by the way, dumps its output to the file /tmp/t.out so it can be watched.)
Do this. Then kill the parent from another shell. Lo and behold, the child does not see an EOF. It just sits there, happily waiting for more input. This is wrong.
For what it's worth, an interesting fact: If you change the child's output from /tmp/t.out to /dev/pty/XXX (where XXX is your xterm's pty), the behavior is slightly different. The child stays around after the parent is killed until you hit <RETURN> in your xterm, when the child wakes up and gets its EOF (it does not get anything you type before the <RETURN>). This behavior is odd, and it sounds like it should tell me something, but I don't see it.
import java.io.*;
class t {
public static void main(String[] args) {
if (args.length == 0) { // the parent
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("java t CHILD");
PrintWriter pout = new PrintWriter(p.getOutputStream(), true);
pout.println("goodbye");
System.out.println("Saying goodbye");
pout.flush();
String dbg = new BufferedReader(
new InputStreamReader(p.getInputStream())
).readLine();
System.out.println("Got " + dbg);
//Thread.sleep(1000000);
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
} else {
PrintWriter tty;
try {
tty = new PrintWriter(new FileWriter("/tmp/t.out", true));
} catch (IOException e) {
System.exit(-100);
return; // not reached, but makes flow clear to compiler
}
try {
System.out.println("Child ready");
tty.println("Child looping");
tty.flush();
int ch;
while ((ch = System.in.read()) != -1) {
tty.write(ch);
tty.flush();
}
tty.write("Child got EOF, exiting");
System.exit(0);
} catch (IOException e) {
e.printStackTrace(tty);
}
}
}
}
Do this. Then kill the parent from another shell. Lo and behold, the child does not see an EOF. It just sits there, happily waiting for more input. This is wrong.
For what it's worth, an interesting fact: If you change the child's output from /tmp/t.out to /dev/pty/XXX (where XXX is your xterm's pty), the behavior is slightly different. The child stays around after the parent is killed until you hit <RETURN> in your xterm, when the child wakes up and gets its EOF (it does not get anything you type before the <RETURN>). This behavior is odd, and it sounds like it should tell me something, but I don't see it.
import java.io.*;
class t {
public static void main(String[] args) {
if (args.length == 0) { // the parent
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("java t CHILD");
PrintWriter pout = new PrintWriter(p.getOutputStream(), true);
pout.println("goodbye");
System.out.println("Saying goodbye");
pout.flush();
String dbg = new BufferedReader(
new InputStreamReader(p.getInputStream())
).readLine();
System.out.println("Got " + dbg);
//Thread.sleep(1000000);
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
} else {
PrintWriter tty;
try {
tty = new PrintWriter(new FileWriter("/tmp/t.out", true));
} catch (IOException e) {
System.exit(-100);
return; // not reached, but makes flow clear to compiler
}
try {
System.out.println("Child ready");
tty.println("Child looping");
tty.flush();
int ch;
while ((ch = System.in.read()) != -1) {
tty.write(ch);
tty.flush();
}
tty.write("Child got EOF, exiting");
System.exit(0);
} catch (IOException e) {
e.printStackTrace(tty);
}
}
}
}