任何人都可以告诉我为什么syscall.Kill(pid, syscall.SIGSEGV)
仅打印"handlerSIGSEGV Sent by 0"
一次,但mustSendSIGSEGV会打印"handlerSIGSEGV Sent by 0"
无限次。
我想把golang SIGSEGV传递给C,只处理一次,不多次。任何人都可以帮助我吗?
package main
/*
#include <stdio.h>
#include <signal.h>
#include <string.h>
struct sigaction old_action;
void handlerSIGSEGV(int signum, siginfo_t *info, void *context) {
printf("handlerSIGSEGV Sent by %d\n", info->si_pid);
}
void testSIGSEGV() {
struct sigaction action;
sigaction(SIGSEGV, NULL, &action);
memset(&action, 0, sizeof action);
sigfillset(&action.sa_mask);
action.sa_sigaction = handlerSIGSEGV;
action.sa_flags = SA_NOCLDSTOP | SA_SIGINFO | SA_ONSTACK;
sigaction(SIGSEGV, &action, &old_action);
}
*/
import "C"
import (
"os"
"syscall"
"time"
"fmt"
)
type Test struct {
Num int
}
func mustSendSIGSEGV(){
var r *Test
r.Num = 0
}
func main() {
// C.test()
C.testSIGSEGV()
pid := os.Getpid()
syscall.Kill(pid, syscall.SIGSEGV)
// mustSendSIGSEGV()
for {
// syscall.Kill(pid, syscall.SIGUSR1)
fmt.Print("33")
time.Sleep(time.Second)
}
}
答案 0 :(得分:1)
从“Go编程语言”中我看到了:
如果非Go代码为任何同步信号(SIGBUS,SIGFPE,SIGSEGV)安装信号处理程序,那么它应该记录现有的Go信号处理程序。如果在执行Go代码时出现这些信号,它应该调用Go信号处理程序(执行Go代码时是否发生信号可以通过查看传递给信号处理程序的PC来确定)。否则,一些Go运行时恐慌将不会按预期发生。