1

I have code:

Process.spawn(RbConfig.ruby, "a ruby file", "arg")

and I wait and check its status by:

Process.wait
$?.success?

Most of the time, it works well. But sometimes, $?.success? is false and $?.to_i is 0x0100. It seems the failed process didn't get a chance to run any code before 0x0100 was returned (I didn't send any signal to the process). I wonder the meaning of 0x0100. I further want to know if Ruby's spawn may fail when the command is all right. Could anyone help?

sawa
  • 165,429
  • 45
  • 277
  • 381
Robert
  • 1,964
  • 1
  • 22
  • 22
  • 1
    I don't know the specific meaning in this context, but that data structure (`0x0000`) is a hexadecimal mask. Each digit is a number from 0 to 15 (with 10 to 15 repesented by the letters A to F). 0x0100 is the number 256. – AJFaraday Dec 01 '15 at 15:50
  • What does `$?.inspect` tell you? Also, how about `$?.to_s` – Kopernik Dec 01 '15 at 15:54
  • Does the code execute if you run it normally? Either by running it on the command line with ruby? –  Dec 01 '15 at 15:55
  • I ran the code all in the same way. Most of the time it works well. But occasionally it fails. I strongly doubt that it has something to do with low memory so that Ruby failed to spawn a process, and that Ruby or Linux doesn't return a failure immediately when spawn returns, and it returns such failure after you wait and check the code(that is Process::Status in Ruby). – Robert Dec 01 '15 at 16:32
  • Ruby docs says these values are platform dependent - Some details are here: http://stackoverflow.com/questions/1101957/are-there-any-standard-exit-status-codes-in-linux - not necessarily the answer of your question – Wand Maker Dec 01 '15 at 16:49
  • Yes, so maybe my question need to correct as: what does "0x0100" mean for Ruby Process::Status on Ubuntu 14.04? – Robert Dec 01 '15 at 16:56

2 Answers2

0

Here is a quote from the Process::Status class documentation:

Posix systems record information on processes using a 16-bit integer. The lower bits record the process status (stopped, exited, signaled) and the upper bits possibly contain additional information (for example the program's return code in the case of exited processes). Pre Ruby 1.8, these bits were exposed directly to the Ruby program. Ruby now encapsulates these in a Process::Status object. To maximize compatibility, however, these objects retain a bit-oriented interface. In the descriptions that follow, when we talk about the integer value of stat, we're referring to this 16 bit value.

The method Process::Status#to_i returns this stat as a Fixnum.

  • I read that Document. However, I want to know the meaning of the specific code 0x0100: does it mean a certain system error, say out of memory? BTW, I ran the code on Ubuntu 14.04. – Robert Dec 01 '15 at 16:16
  • Hey. So it looks like the status is set in this method. Unfortunately, I no next to nothing about C, so I am not sure if I will be able to follow the path further. http://rxr.whitequark.org/mri/source/process.c#705 –  Dec 02 '15 at 08:51
  • If I am reading it correctly, first the pid and then a wait_data struct (that contains a pid) are cast as st_data_t. I wasn't able to find how this cast is done, but st_data_t is a core ruby type used all throughout the code base and it is defined here: http://rxr.whitequark.org/mri/source/include/ruby/st.h#020 –  Dec 02 '15 at 09:25
0

OK, finally I got the answer: when a ruby process throws an uncaught exception, the process' exit code will be 0x0100. This is from my observation on Ubuntu 14.04 and Ruby 2.2. For example: there's ruby file a.rb, and in another file, say src.rb, there's a code snippet:

Process.spawn(RbConfig.ruby, "a.rb", "arg")
Process.wait

If a.rb throws an uncaught exception, then $?.to_i will be 0x0100. What's more, I also observed that a.rb sometimes didn't get executed before its process failed with 0x0100. So I guess it may have something to do with the Ruby interpreter since I'm sure a.rb is OK.

Anyway, there' no official document mentioning the exact behavior. So my experience is for your reference.

Robert
  • 1,964
  • 1
  • 22
  • 22