Go: Signal Origin Retrieval
In Go, capturing signals is possible, but determining the PID that triggered a signal can be challenging.
Limitations of Signal Handling
Go manages signal handlers internally, and there is no official way to access the PID of the signal originator. The returned signal information, as observed in the example provided, does not include this data.
Alternative Approaches
Using an alternative method for communication, such as pipes or semaphores, may be more suitable in cases where signal origin determination is crucial.
Complex Solution from C
While not officially supported, it is possible to set up a custom signal handler in C that can capture the PID. However, this approach is intricate and poses potential risks.
Here is an example based on a solution proposed by Ian from GitHub issue 7227:
package main import ( "C" "os" "syscall" "time" ) func main() { C.test() pid := os.Getpid() for { syscall.Kill(pid, syscall.SIGUSR1) time.Sleep(time.Second) } }
This example uses C's sigaction to establish a custom signal handler, which prints the sender's PID.
The above is the detailed content of How Can I Determine the PID of a Signal's Originator in Go?. For more information, please follow other related articles on the PHP Chinese website!