以下尝试形成代码,但SIGUSR1的触发器不起作用。 什么是不够的?
signal(SIGUSR1, SIG_IGN);
let que = dispatch_queue_create("Que", DISPATCH_QUEUE_CONCURRENT)
let source = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, UInt(SIGUSR1), 0, que)
dispatch_source_set_event_handler(source, {
NSLog("BLOCK doing")
})
dispatch_resume(source)
sigaction(SIGUSR1, nil, nil)
答案 0 :(得分:1)
import Foundation
var signalSource: dispatch_source_t!
var signalOnceToken = dispatch_once_t()
let signalID:Int32 = SIGUSR1
func initSignalHandler(signal: Int32) {
dispatch_once(&signalOnceToken) {
let queue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT)
signalSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL,
UInt(signal), 0, queue)
if let source = signalSource {
dispatch_source_set_event_handler(source) {
NSLog("signal: \(signal)")
}
dispatch_resume(source)
}
}
}
initSignalHandler(signalID)
sleep(1)
// send signal
Darwin.raise(signalID)
NSLog("End")