Home > Backend Development > Golang > Go: Get the signal source

Go: Get the signal source

王林
Release: 2024-02-12 08:30:09
forward
835 people have browsed it

Go: Get the signal source

#phpA practical tool recommended by editor Baicao is Go: Obtain signal source. This tool can help developers obtain relevant information about signal sources, including signal strength, signal type, operators, etc. By using this tool, developers can easily conduct signal testing and optimization to improve user experience. At the same time, the tool also provides a wealth of functional options, such as signal chart display, signal change monitoring, etc., allowing developers to have a more comprehensive understanding of the signal situation, so as to better debug and optimize. Whether it's mobile app development or web optimization, Go: Get Source is a useful tool.

Question content

I am using go to launch several scripts, when they encounter some problems, they use "alert" signals, I know go can catch these signals, but I need Know the pid of the signal. In c, the signal handler is passed a structure to know the pid of the signal, but in go, this does not seem to be 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)
}
Copy after login

The following example (extracted from the signals documentation) sends me the signal that initiates the call, but does not send any useful information (like the pid)

Workaround

No, you can't Do this in an officially supported way. The go runtime is required to have signal handlers and does not expose additional information in any way.

You can still do this from c by setting up a new signal handler, but I would be very cautious about this (see questions like /7227). You're probably better off using another communication method other than signaling.

The following is a partial example based on the ian code in 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)
    }
}
Copy after login

The above is the detailed content of Go: Get the signal source. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template