我正在使用Go来启动几个脚本,当他们遇到一些问题时,他们使用"警报"信号,我知道Go可以捕获这些信号,但我需要知道发出信号的PID.在C中给信号处理程序传递一个结构来知道发起信号的pid但是在Go中看起来并非如此
package main
import (
"fmt"
"os"
"os/signal"
)
func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
s := <-c
fmt.Println("Got signal:", s)
fmt.Printf("%+v\n",s)
}
Run Code Online (Sandbox Code Playgroud)
下面的示例(从信号文档中提取)向我发送发起呼叫但不是任何有用信息的信号(如pid)
不,你不能以官方支持的方式做到这一点.Go运行时需要拥有信号处理程序,并且不会以任何方式暴露额外信息.
您仍然可以通过设置新的信号处理程序从C执行此操作,但我会非常谨慎地执行此操作(请参阅问题/ issue/7227).除了信号之外,您可能最好使用其他方法进行通信.
以下是基于问题7227的Ian代码的部分示例:
package main
/*
#include <stdio.h>
#include <signal.h>
#include <string.h>
struct sigaction old_action;
void handler(int signum, siginfo_t *info, void *context) {
printf("Sent by %d\n", info->si_pid);
}
void test() {
struct sigaction action;
sigaction(SIGUSR1, NULL, &action);
memset(&action, 0, sizeof action);
sigfillset(&action.sa_mask);
action.sa_sigaction = handler;
action.sa_flags = SA_NOCLDSTOP | SA_SIGINFO | SA_ONSTACK;
sigaction(SIGUSR1, &action, &old_action);
}
*/
import "C"
import (
"os"
"syscall"
"time"
)
func main() {
C.test()
pid := os.Getpid()
for {
syscall.Kill(pid, syscall.SIGUSR1)
time.Sleep(time.Second)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
835 次 |
| 最近记录: |