I'm trying to implement a function that would call an interrupt signal in Go. I know how to intercept interrupt signals from the console, by using signal.Notify(interruptChannel, os.Interrupt)
, however, I can't find a way to actually send the interrupt signals around. I've found that you can send a signal to a process, but I'm not sure if this can be used to send a top-level interrupt signal.
Is there a way to send an interrupt signal from within a Go function that could be captured by anything that is listening for system interrupt signals, or is that something that's not supported in Go?
Sending a Signal to Another Process: System Call kill() System call kill() takes two arguments. The first, pid, is the process ID you want to send a signal to, and the second, sig, is the signal you want to send. Therefore, you have to find some way to know the process ID of the other party.
An interrupt is a signal emitted by a device attached to a computer or from a program within the computer. It requires the operating system (OS) to stop and figure out what to do next. An interrupt temporarily stops or terminates a service or a current process.
When an interrupt signal arrives, the CPU must stop what it's currently doing and switch to a new activity; it does this by saving the current value of the program counter (i.e., the content of the eip and cs registers) in the Kernel Mode stack and by placing an address related to the interrupt type into the program ...
Interprocessor interrupts (IPIs). An IA-32 processor can use the IPI mechanism to interrupt another processor or group of processors on the system bus. IPIs are used for software self-interrupts, interrupt forwarding, or preemptive scheduling. APIC timer–generated interrupts.
Assuming you are using something like this for capturing interrupt signal
var stopChan = make(chan os.Signal, 2)
signal.Notify(stopChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
<-stopChan // wait for SIGINT
Use below from anywhere in your code to send interrupt signal to above wait part.
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
Or if you are in the same package where where stopChan variable is defined. Thus making it accessible. You can do this.
stopChan <- syscall.SIGINT
Or you can define stopChan as a global variable (making the first letter in Capital letter will achieve the same), then you can send interrupt signal from a different package too.
Stopchan <- syscall.SIGINT
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With