Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9188851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:06:42+00:00 2026-06-17T20:06:42+00:00

On Linux I have an executable I wish to run via Python script. The

  • 0

On Linux I have an executable I wish to run via Python script. The executable divides by zero, and I can’t seem to handle the signal from subprocess.
I read around and it seem that the preexec_fn should handle SIGFPE, but no luck so far.

I’m using Python 2.7

My code:

# b.py
import os
import subprocess
import signal
import sys

def pref_fun():
    signal.signal(signal.SIGFPE,foo)

def foo(signal,frame):
    print "Caught signal!"
    sys.exit(0)

sub = subprocess.Popen(["a.out"], preexec_fn=pref_fun)
sub.wait()
v = sub.returncode
print "value: ", v

and my child:

a.c
#include <stdio.h>
#include <stdlib.h>
int main() {
    printf("Now dividing by zero\n");
    fflush(stdout);
    double x = 5;
    x= 5/0;
    printf("oh no\n");
    return 0;

}

The expected output is “Caught signal!”, but I don’t seem to get it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:06:43+00:00Added an answer on June 17, 2026 at 8:06 pm

    I am afraid that we can’t do it in such way. Though the signal handler was installed in the forked child, after ./a.out was exec()ed, the image which has the signal handler was replaced by the latter one — so there’s no handlers in the child process any more. Yet we can check whether the child process was terminated by a signal by using os.WIFSIGNALED(v), if True, we can use os.WTERMSIG(v) to get which signal it was, after this, we can do something in parent.

    However, I got very odd results from os.WTERMSIG(v) on my machine, and found that the real signal number seemed to be the negative of v (I’d checked SIGSEGV and SIGFPE), I don’t know whether if it is my machine’s issue, anyway, hope this helps :).

    import os
    import subprocess
    import signal
    import sys 
    
    def pref_fun():
        signal.signal(signal.SIGFPE,foo)
    
    def foo(signal,frame):
        print "Caught signal!"
        sys.exit(0)
    
    sub = subprocess.Popen(["./a.out"], preexec_fn=pref_fun)
    sub.wait()
    v = sub.returncode
    print "value: ", v
    print os.WIFSIGNALED(v)
    print "signal:", os.WTERMSIG(v)
    print "SIGFPE", signal.SIGFPE    
    

    which outputs:

    Now dividing by zero
    value:  -8
    True
    signal: 120
    SIGFPE 8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an executable (.exe) of a perl script on a 64-bit linux
I have a C code executable for Linux. For release, I can have two
I have a watershed model executable in linux that when executed, reads from a
I have debian linux. I've created from user crontab -e this text: 0 *
I have an executable that I need to run some tests on in C++
I have jdk1.7.0_07 installed on server(linux). The file permission to ~/bin files are executable(755).
I have a PHP script I'd like to compile into a standalone command-line executable
I have an executable under linux. I have an 8 core processor. I want
I have an executable I want to be able to distribute and run in
If I have progA | progB , then can I make some progB executable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.