去:获取信号源 [英] Go: Get signal origin

查看:182
本文介绍了去:获取信号源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Go来启动几个脚本,当他们遇到一些问题时,他们使用alert信号,我知道Go可以捕获这些信号,但我需要知道产生Signal的PID。在C中向信号处理程序传递一个结构来知道发起信号的pid,但在Go中看起来不是这样的情况

 包主要

进口(
fmt
os
os /信号


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)
}

下面的示例(从信号文档中提取)向我发送发起呼叫但没有任何有用信息的信号(如pid)

解决方案

不,您不能以官方支持的方式进行此操作。 Go运行时需要拥有信号处理程序,并且额外的信息不会以任何方式公开。

你可能仍然可以通过建立一个新的信号处理程序来做到这一点,但我会非常谨慎地处理这个问题(请参阅 issue / 7227 )。您可能最好使用另一种通讯方式,而不是信号。



以下是基于伊恩代码7227的部分示例:

  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(发送者%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 =处理程序;
action.sa_flags = SA_NOCLDSTOP | SA_SIGINFO | SA_ONSTACK;
sigaction(SIGUSR1,& action,& old_action);

$ import $ C
$ b $ os $ $ b $系统调用
time

$ b $ func main(){
C.test()
pid:= os.Getpid()
for {
syscall .Kill(pid,syscall.SIGUSR1)
time.Sleep(time.Second)
}
}


I'm using Go to launch a couple of scripts and when they have some problems they use the "alert" signal, I know Go can capture those signals but I need to know the PID that originated the Signal. in C to the signal handler is passed a structure to know the pid who originated the signal but in Go looks like is not the case

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)
}

the example below (extracted from the signal doc) send me the signal who originated the call but not any useful info (like the pid)

解决方案

No, you can't do this in an officially supported manner. The Go runtime needs to own the signal handlers, and that extra information isn't exposed in any way.

You may still be able to do this from C by setting up a new signal handler, but I would be very cautious going about this (see issues like issue/7227). You're probably better off using another method for communication other than a signal.

Here's a partial example based on Ian's code from issue 7227:

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)
    }
}

这篇关于去:获取信号源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆