Delivered signal info in exit status of a process?

I

Ishwar Rattan

Here is a piece code (according to blurb on os.wait, the lower
order 7 bits of exit status of process should contain the signal
number of signal that terminated the process..) and signal number
should be 8 (SIGFPE), similar logic in C-code produces expected
result.

Any pointers?

-ishwar
----
import os

cpid = os.fork()
if cpid == 0:
2 / 0 # divide by zero
else if cpid > 0:
ps, st = os.wait()
print 'child got signal: %d, exit status: %d' %(st&0177, st>>8)

prints::

child got signal: 0, exit status 1
---
 
D

Donn Cave

Here is a piece code (according to blurb on os.wait, the lower
order 7 bits of exit status of process should contain the signal
number of signal that terminated the process..) and signal number
should be 8 (SIGFPE), similar logic in C-code produces expected
result.

Any pointers?

-ishwar
----
import os

cpid = os.fork()
if cpid == 0:
2 / 0 # divide by zero
else if cpid > 0:
ps, st = os.wait()
print 'child got signal: %d, exit status: %d' %(st&0177, st>>8)

prints::

child got signal: 0, exit status 1
---

One branch of this question leads into Python's
arithmetic and whether SIGFPE plays any role there.
I sure don't know (I would have thought the FP stood
for floating point, but anyway I see the effect isn't
very different if I actually use floating point values.)
Maybe someone who would know will read this thread despite
the subject line.

But even if a SIGFPE is in fact delivered to the interpeter
as a rule on a divide by zero, at any rate the end result
is a Python exception. If you execute the instructions
directly, for example at the interpreter prompt, you'll
get a traceback and notification, or whatever result the
effective exception handler at the time gives you. You
could expect an error (non-zero status) exit, from the
default exception handler for a script, but not a termination
signal.

And I hope the documentation mentioned that you might use
os.WTERMSIG() instead of the computation above - not that
it's necessarily different, but it will be more reliable
where layout of the signal value can change (it isn't the
same on all UNIX-like platforms.)

Donn Cave, (e-mail address removed)
 
M

Michael Hoffman

Ishwar said:
Here is a piece code (according to blurb on os.wait, the lower
order 7 bits of exit status of process should contain the signal
number of signal that terminated the process..) and signal number
should be 8 (SIGFPE), similar logic in C-code produces expected
result.

But that's not what happens when you throw a ZeroDivisonError in python:

$ python -c "2/0"
Traceback (most recent call last):
File "<string>", line 1, in ?
ZeroDivisionError: integer division or modulo by zero

$ echo $? # this is the exit status in bash
1

If there is a SIGFPE, Python is trapping it and turning it into an
exception. And it exits with status 1 due to an uncaught exception.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top