• <bdo id='RpI3g'></bdo><ul id='RpI3g'></ul>
    <i id='RpI3g'><tr id='RpI3g'><dt id='RpI3g'><q id='RpI3g'><span id='RpI3g'><b id='RpI3g'><form id='RpI3g'><ins id='RpI3g'></ins><ul id='RpI3g'></ul><sub id='RpI3g'></sub></form><legend id='RpI3g'></legend><bdo id='RpI3g'><pre id='RpI3g'><center id='RpI3g'></center></pre></bdo></b><th id='RpI3g'></th></span></q></dt></tr></i><div id='RpI3g'><tfoot id='RpI3g'></tfoot><dl id='RpI3g'><fieldset id='RpI3g'></fieldset></dl></div>

  • <legend id='RpI3g'><style id='RpI3g'><dir id='RpI3g'><q id='RpI3g'></q></dir></style></legend>

      <tfoot id='RpI3g'></tfoot>

      <small id='RpI3g'></small><noframes id='RpI3g'>

      1. 确定 python 子进程分段是否错误

        时间:2023-08-06
          1. <legend id='N8TaU'><style id='N8TaU'><dir id='N8TaU'><q id='N8TaU'></q></dir></style></legend>

              <tbody id='N8TaU'></tbody>
            <i id='N8TaU'><tr id='N8TaU'><dt id='N8TaU'><q id='N8TaU'><span id='N8TaU'><b id='N8TaU'><form id='N8TaU'><ins id='N8TaU'></ins><ul id='N8TaU'></ul><sub id='N8TaU'></sub></form><legend id='N8TaU'></legend><bdo id='N8TaU'><pre id='N8TaU'><center id='N8TaU'></center></pre></bdo></b><th id='N8TaU'></th></span></q></dt></tr></i><div id='N8TaU'><tfoot id='N8TaU'></tfoot><dl id='N8TaU'><fieldset id='N8TaU'></fieldset></dl></div>

              • <tfoot id='N8TaU'></tfoot>
                • <bdo id='N8TaU'></bdo><ul id='N8TaU'></ul>

                  <small id='N8TaU'></small><noframes id='N8TaU'>

                  本文介绍了确定 python 子进程分段是否错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在编写一个对学生程序进行评分的程序,我相信您可以想象,它们有时会出现分段错误.我遇到的问题是,当学生编程分段错误时,没有迹象表明发生了什么.

                  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 programs segmentation fault, there is no indication that is what happened.

                  proc = subprocess.Popen(student_command, 
                                          stdout=subprocess.PIPE, 
                                          stderr=subprocess.PIPE)
                  self.stdout, self.stderr = proc.communicate()
                  self.returncode = proc.returncode
                  

                  我从 subprocess 中提取 stderrstdout 和返回码,但如果程序分段出错,stderr 为空,stdout 为空,返回码为-11.现在我可以查找 -11 退出代码并假设如果这是返回代码,则存在分段错误,但也没有什么可以阻止学生的代码将 -11 作为返回代码仅仅因为学生感觉想要返回-11.

                  I pick up the stderr, stdout, and the return code from the subprocess, but if the program segmentation faults, stderr is empty, stdout is empty, and the return code is -11. Now I could look for the -11 exit code and assume that if that is the return code there was a segmentation fault, but there is also nothing to prevent a student's code from having -11 as a return code just because the student felt like returning -11.

                  您如何判断子进程分段是否出错,而不是仅仅感觉返回 -11?我不太关心 stderr 和 stdout 中的内容,为此我看到了许多帖子,包括 this 处理拾取输出,但我不太在意输出,尽管获得 "分段错误"从 stderr 中取出字符串,但我真正需要的是一种明确说明子进程发生了什么的方法.

                  How do you tell if a subprocess segmentation faults, as opposed to just feeling like returning -11? I don't really care all that much about what is in stderr and stdout, and to that end have seen a number of posts including this that deal with picking up the output, but I don't care all that much about the output, although it would be nice to get the "Segmentation Fault" string out of stderr, but what I really need is a way to definitively tell what happened to the subprocess.

                  推荐答案

                  事实上,在 UNIX 上,尝试返回 -11 的进程通常最终会返回一个正整数.这是因为 wait 系列函数的返回状态实际上是一组位域,其中一个域用于结束进程的信号,另一个域用于返回值.Python 从这些位域解码 wait 返回值.

                  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.

                  在大多数系统上,这些字段是无符号的,大小为 8 位,因此您可能会看到如下内容:

                  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
                  

                  在前一种情况下,进程segfaults"(通过使用 SIGSEGV 杀死自己),因此 wait 返回 -11.在后一种情况下,进程以 -11 的返回码退出,结果 wait 值为 245 (256-11).因此,您可以放心,来自 wait 的任何负返回值都必须代表致命信号,而不是正常返回.但请注意,这些进程可能会杀死自己以伪造致命错误.

                  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.

                  这篇关于确定 python 子进程分段是否错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python 分段错误:OSX 上的 11 下一篇:segfault 使用 numpy 的 lapack_lite 在 osx 上进行多处理

                  相关文章

                  最新文章

                    <i id='oio0A'><tr id='oio0A'><dt id='oio0A'><q id='oio0A'><span id='oio0A'><b id='oio0A'><form id='oio0A'><ins id='oio0A'></ins><ul id='oio0A'></ul><sub id='oio0A'></sub></form><legend id='oio0A'></legend><bdo id='oio0A'><pre id='oio0A'><center id='oio0A'></center></pre></bdo></b><th id='oio0A'></th></span></q></dt></tr></i><div id='oio0A'><tfoot id='oio0A'></tfoot><dl id='oio0A'><fieldset id='oio0A'></fieldset></dl></div>
                    • <bdo id='oio0A'></bdo><ul id='oio0A'></ul>
                  1. <tfoot id='oio0A'></tfoot>
                    1. <legend id='oio0A'><style id='oio0A'><dir id='oio0A'><q id='oio0A'></q></dir></style></legend>

                      <small id='oio0A'></small><noframes id='oio0A'>