如何修复python代码中的“总线错误”以接收信号?

时间:2019-03-06 17:41:41

标签: python macos signals

我在this description之后创建了一个python代码:

import os
import time
import signal

def receiveSignal(signalNumber, frame):
    print('Received:', signalNumber)
    return

signal.signal(signal.SIGUSR1, receiveSignal)

# output current process id
print('My PID is:', os.getpid())

# wait in an endless loop for signals
while True:
    print('Waiting...')
    time.sleep(3)

运行正常。但是当我尝试从另一个终端发送SIGUSR1信号时,例如:

kill -10 55947

python代码失败如下:

My PID is: 55947
Waiting...
Waiting...
Bus error: 10

代码在MacOS 10.13.6上运行,我尝试使用python 2.7.12和python 3.6.2。在两种情况下,我都得到Bus Error

是否可以解决该问题?也许这是Mac问题?我希望正在运行的代码能够执行方法receiveSignal,但否则将停留在shile循环中。

1 个答案:

答案 0 :(得分:1)

我认为问题是,在MacOS中,SIGNAL 10的意思是SIGBUS

查看信号列表https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/signal.3.html

查看信号详细信息,

     1     SIGHUP       terminate process    terminal line hangup
     2     SIGINT       terminate process    interrupt program
     3     SIGQUIT      create core image    quit program
     4     SIGILL       create core image    illegal instruction
     5     SIGTRAP      create core image    trace trap
     6     SIGABRT      create core image    abort program (formerly SIGIOT)
     7     SIGEMT       create core image    emulate instruction executed
     8     SIGFPE       create core image    floating-point exception
     9     SIGKILL      terminate process    kill program
     10    SIGBUS       create core image    bus error
     ...
     ...
     30    SIGUSR1      terminate process    User defined signal 1
     31    SIGUSR2      terminate process    User defined signal 2

因此,错误。

尝试发送SIGNAL 30,然后查看:)