我有自己的用Java编写的终端应用程序,它有时会运行。要在Linux上使用bash,你必须创建一个pty,fork和exec bash,并将pty分配给它的stdin / out / err。我以几种不同的方式使用JNA完成了这项工作,但没有什么是可靠的。大约30%的时间子进程出现问题并挂起。我必须杀死这个过程。我已经听说过很多关于Java的fork问题,我还需要做些什么吗?当它失败时,我将看到消息"子进程已启动"但是在它调用execvpe之前它会停止一些。它没有使用100%的CPU,我不知道它在做什么。我看过JPty和类似的项目,他们似乎也这样做。它们可靠吗?
这是我使用forkpty()
的代码 private boolean fork_pty(String cmd, String args[], String env[]) {
IntByReference masterRef = new IntByReference();
pid = util.forkpty(masterRef, null, null, null);
if (pid == 0) {
System.out.println("child process started");
//child process (slave)
c.execvpe(cmd, args, env); //searches path for cmd
System.exit(0); //should not happen
}
//parent process (master)
master = masterRef.getValue();
new Thread() {
public void run() {
c.waitpid(pid, new IntByReference(), 0);
close();
}
}.start();
return true;
}
有什么想法吗?如果我在AWT启动前分叉怎么办? gc可能是一个问题???
答案 0 :(得分:1)
我找到了解决方案。而不是fork()ing我使用ProcessBuilder来fork()。我尝试使用ProcessBuilder直接使用redirectInput / Output / Error执行bash到slave pty,但后来我无法调用setsid()并且bash被搞砸了。然后我使用ProcessBuilder来执行另一个java函数,该函数完成了设置stdin / out / err的进程的子进程,然后使用c.execvpe运行bash并且每次都有效。
完整源代码将在JavaForce / 7.35 @ javaforce.sourceforge.net中提供(参见javaforce.jna.LnxPty)
这是我的fork函数:
private boolean fork_nofork(String cmd, String args[], String env[]) {
JFLog.log("fork:no fork version");
String slaveName;
master = c.posix_openpt(O_RDWR | O_NOCTTY);
if (master == -1) return false;
slaveName = c.ptsname(master);
if (slaveName == null) {
JFLog.log("LnxPty:slave pty == null");
return false;
}
if (c.grantpt(master) != 0) {
JFLog.log("LnxPty:grantpt() failed");
return false;
}
if (c.unlockpt(master) != 0) {
JFLog.log("LnxPty:unlockpt() failed");
return false;
}
ArrayList<String> cmdline = new ArrayList<String>();
cmdline.add("java");
cmdline.add("-cp");
cmdline.add("/usr/share/java/javaforce.jar:/usr/share/java/jna.jar");
cmdline.add("javaforce.jna.LnxPty");
cmdline.add(slaveName);
cmdline.add(cmd);
cmdline.add("" + (args.length-1)); //# args
for(int a=0;a<args.length;a++) {
if (args[a] == null) break;
cmdline.add(args[a]);
}
for(int a=0;a<env.length;a++) {
if (env[a] == null) break;
cmdline.add(env[a]);
}
String cl[] = cmdline.toArray(new String[0]);
try {
ProcessBuilder pb = new ProcessBuilder(cl);
pb.directory(new File("/home/" + System.getenv("USER")));
p = pb.start();
} catch (Exception e) {
JFLog.log(e);
return false;
}
writeBuf = Native.malloc(1024);
readBuf = Native.malloc(1024);
new Thread() {
public void run() {
try {p.waitFor();} catch (Exception e) {}
close();
}
}.start();
return true;
}
这是在子进程中运行的main()函数:
/** This becomes the child process. */
public static void main(String args[]) {
if (args == null || args.length < 3) {
System.out.println("Usage : LnxPty slaveName, cmd, #args, [args...], [env...]");
return;
}
init();
String slaveName = args[0];
String cmd = args[1];
int noArgs = JF.atoi(args[2]);
int p = 3;
ArrayList<String> process_args = new ArrayList<String>();
ArrayList<String> process_env = new ArrayList<String>();
for(int a=0;a<noArgs;a++) {
process_args.add(args[p++]);
}
while (p < args.length) {
process_env.add(args[p++]);
}
termios attrs = new termios();
try {
int slave = c.open(slaveName, O_RDWR); //should open this in child process
if (slave == -1) {
System.out.println("LnxPty:unable to open slave pty");
System.exit(0);
}
if (c.setsid() == -1) {
System.out.println("LnxPty:unable to setsid");
System.exit(0);
}
c.tcgetattr(slave, attrs);
// Assume input is UTF-8; this allows character-erase to be correctly performed in cooked mode.
attrs.c_iflag |= IUTF8;
// Humans don't need XON/XOFF flow control of output, and it only serves to confuse those who accidentally hit ^S or ^Q, so turn it off.
attrs.c_iflag &= ~IXON;
// ???
attrs.c_cc[VERASE] = 127;
c.tcsetattr(slave, TCSANOW, attrs);
c.dup2(slave, STDIN_FILENO);
c.dup2(slave, STDOUT_FILENO);
c.dup2(slave, STDERR_FILENO);
c.signal(SIGINT, SIG_DFL);
c.signal(SIGQUIT, SIG_DFL);
c.signal(SIGCHLD, SIG_DFL);
c.execvpe(cmd, process_args.toArray(new String[0]), process_env.toArray(new String[0]));
System.exit(0); //should not happen
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}