What is the meaning of values from Java Process.ex

2019-03-16 12:13发布

问题:

I am using Process via ProcessBuilder to run an executable made in C code. I am catching the Process.exitValue() to react on this exit values. I noticed not all the exit values are from the executable. For example, I get an exit value of 139 and nowhere in my C code I am returning an exit value of 139.

I am trying to find an overview of exit values, but I cannot find this, and now I found out the exit value can be OS dependent. (I am using Ubuntu by the way).

It seems the only exit value to be sure of is 0 when everything goes right. Are there specifications about exit values? Can I be sure that a certain range can be used only for my own program? What exit codes are reserved for the OS.

I found out that 139 is probably a memory error in the C code. I want to get rid of the probably. I can't get any overview of exit values (e.g. 139 = .....)

This is the simplified code by the way:

ProcessBuilder p = new ProcessBuilder(executableName,
   executableArguments);
final Process shell = p.start();
InputStream shellIn = shell.getInputStream();
int shellExitStatus = shell.exitValue();

Note: Running the C executable in the Ubuntu shell gives no error at all (i.e. exit value 0). But, doing the same command in Java gives exit value 139.

回答1:

If the system kills your application (like in the case of Segmentation fault) it sets the exit code to 128 + SIGNAL - see linux signal(7) manpage for signal values.

Also, for linux, there are several default exit codes defined in sysexits.h header file, and it is recommended that programmers use those constants instead of manually defining own values. From exit(3) manpage:

BSD has attempted to standardize exit codes; see the file <sysexits.h>.

You can find the file for example here, and the values included are:

#define EX_OK           0  /* successful termination */

#define EX__BASE        64  /* base value for error messages */

#define EX_USAGE        64  /* command line usage error */
#define EX_DATAERR      65  /* data format error */
#define EX_NOINPUT      66  /* cannot open input */
#define EX_NOUSER       67  /* addressee unknown */
#define EX_NOHOST       68  /* host name unknown */
#define EX_UNAVAILABLE  69  /* service unavailable */
#define EX_SOFTWARE     70  /* internal software error */
#define EX_OSERR        71  /* system error (e.g., can't fork) */
#define EX_OSFILE       72  /* critical OS file missing */
#define EX_CANTCREAT    73  /* can't create (user) output file */
#define EX_IOERR        74  /* input/output error */
#define EX_TEMPFAIL     75  /* temp failure; user is invited to retry */
#define EX_PROTOCOL     76  /* remote error in protocol */
#define EX_NOPERM       77  /* permission denied */
#define EX_CONFIG       78  /* configuration error */

#define EX__MAX         78  /* maximum listed value */

However, using them is not mandatory, and you are free to use any value you want.

The general answer is - if your application fails gracefully (i.e. it is able to handle the error an finish execution), then it sets the exit code by itself. If the application is killed by the system, it's the system who sets the exit code.

You can also see this thread for some additional information.