Determining if a python subprocess segmentation faults

前端 未结 1 1624
攒了一身酷
攒了一身酷 2021-01-04 02:39

I am writing a program that grades student programs, and as I am sure you can imagine, they sometimes segmentation fault. The problem I am having is that when the student p

相关标签:
1条回答
  • 2021-01-04 03:23

    Well, in fact, on UNIX, a process that attempts to return -11 will usually end up returning a positive integer instead. This is because the return status from the wait series of functions is actually a set of bitfields, with a field for the signal that ended the process and a separate field for the return value. Python decodes the wait return value from these bitfields.

    On most systems, these fields are unsigned and 8 bits in size, so you will probably see something like this:

    >>> import subprocess
    >>> subprocess.Popen(['python','-c','import os; os.kill(os.getpid(),11)']).wait()
    -11
    >>> subprocess.Popen(['python','-c','exit(-11)']).wait()
    245
    

    In the former case, the process "segfaults" (by killing itself with SIGSEGV), and so wait returns -11. In the latter case, the process exits with a return code of -11, and the resulting wait value is 245 (256-11). You can therefore rest assured that any negative return value from wait must represent a fatal signal, as opposed to a normal return. Note, though, that processes may kill themselves to fake a fatal error.

    0 讨论(0)
提交回复
热议问题