Senior
What interprocess communication (IPC) methods exist in Linux? How do SIGTERM and SIGKILL differ?
sobes.tech AI
Answer from AI
In Linux, there are various methods of interprocess communication (IPC):
- Pipes — allow data transfer between processes related by parent-child relationships.
- Named Pipes (FIFO) — similar to pipes but have a name in the filesystem and can be used between any processes.
- Sockets — enable data exchange between processes on the same or different hosts.
- Shared Memory — allocates a memory area accessible by multiple processes for fast data transfer.
- Semaphores — used for synchronizing access to resources.
- Message Queues — allow sending and receiving structured messages.
- Signals — asynchronous notifications about events.
Difference between SIGTERM and SIGKILL:
-
SIGTERM (15) — a signal requesting process termination. The process can catch this signal, perform cleanup, and terminate gracefully. It is a "polite" way to stop a process.
-
SIGKILL (9) — a signal for immediate process termination, which cannot be caught or ignored. The process is forcibly terminated without cleanup.
In Go, the os/signal package is used to handle signals, allowing catching and processing signals, for example:
import (
"os"
"os/signal"
"syscall"
"fmt"
)
func main() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
sig := <-sigs
fmt.Println("Received signal:", sig)
// Cleanup actions can be performed here before exit
}