Delivered signal info in exit status of a process?

Donn Cave donn at
Fri Sep 24 21:23:07 CEST 2004

In article <938a4680.0409241052.72867804 at>,
 rattan at (Ishwar Rattan) wrote:

> 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

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, donn at

More information about the Python-list mailing list