简体   繁体   中英

Runtime.getRuntime.exec 139 (SIGSEGV) from Java for C programs

I'm creating a Java program to run C executables, using Runtime.getRuntime.exec(String command) method, but sometimes it returns exitValue=139(SIGSEGV) even if C programs work fine when I run them from terminal or Eclipse.

What could be the problem?

Process pro = Runtime.getRuntime().exec("./cExecutable 10");

System.out.println(command + " exitValue=" + pro.exitValue());

If it can be useful, I'm using Ubuntu 18.04.

Important, I also noticed that it happens most frequently when executable output has more than 600 lines.

In fact outputs of my programs are very large

Java process launcher is can freeze if the STDERR/OUT streams of the sub-process are not read, but I've never seen this cause SIGSEGV in the sub-process so this may not help you.

However as you say your apps write a lot of data then it may be worth fixing your launcher to consume the output streams. ProcessBuilder gives better control over the launch than Runtime.getRuntime , this example writes STDERR/OUT streams to files so you don't need to consume in background threads:

String[] cmd = new String[] {"cExecutable","10"};
ProcessBuilder pb = new ProcessBuilder(cmd);

// Set up STDOUT:
Path tmpdir = Path.of(System.getProperty("java.io.tmpdir"));
Path fn = Path.of(cmd[0]).getFileName();
Path out = tmpdir.resolve(fn+"-stdout.log");
pb.redirectOutput(out.toFile());

// EITHER: Set up STDERR:
Path err = tmpdir.resolve(fn+"-stderr.log");
pb.redirectError(err.toFile());
// OR: join err to std with
// pb.redirectErrorStream(true);

// Launch and wait:
Process p = pb.start();
long pid = p.pid();
System.out.println("started PID "+pid);

// could close STDIN to signal end of input
p.getOutputStream().close();

int rc = p.waitFor();

System.out.println("Exit PID "+pid+": RC "+rc +" => "+(rc == 0 ? "OK": "**** ERROR ****"));
System.out.println("STDOUT: \""+Files.readString(out)+'"');
System.out.println("STDERR: \""+Files.readString(err)+'"');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM