Home >Backend Development >Golang >golang dos does not exit
Recently, when using Golang to write programs for the Windows platform, I encountered a strange problem: after starting the program, the program cannot be exited normally through "Ctrl C" or closing the console. After the program exits, you can still see the process it is running in the task manager, resulting in resources that cannot be released and seriously affecting the performance of the system. After checking a lot of information, I finally found the reason why the program could not exit normally and found the solution.
Cause of the problem
In Golang, there are three main ways to exit the program:
However, under the Windows platform, when using the os.Exit or os.Process.Kill method to exit the program, the DOS console will not exit normally. This is because on the Windows platform, the DOS console creates a new process group when starting the program, and the actual process of the program and the DOS console process are not in the same process group.
When using the os.Exit or os.Process.Kill method to exit a program, the actual program process and the DOS console process exit from different process groups respectively, causing the console to fail to exit normally.
Solution
While looking for a solution, I found a Golang standard library with a package name "os/signal", which provides the function of receiving system signals and processing them accordingly. So we can realize the normal exit of the program by capturing the signal of the operating system.
The steps to use the "os/signal" library are as follows:
The sample code is as follows:
package main import ( "fmt" "os" "os/signal" "syscall" ) func main() { fmt.Println("start program") sigs := make(chan os.Signal, 1) done := make(chan bool, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) go func() { sig := <-sigs fmt.Println() fmt.Println(sig) done <- true }() fmt.Println("waiting for signal") <-done fmt.Println("end program") os.Exit(0) }
In the above code, we first use the signal.Notify function to register the system interrupt signal SIGINT and the program stop signal SIGTERM into the signal receiver. Then when the program is running, it waits for the reception of the signal. When the signal is received, the signal type is output, and the status of the received signal is passed to the main program through the pipeline, and is processed by the main program. Finally, after the main program handles the signal, exit the program normally through os.Exit(0).
Summary
Generally speaking, Golang, as an efficient, simple, and safe programming language, provides a simple and powerful library interface when processing system signals. Under the Windows platform, using the "os/signal" library can effectively solve the problem of the program not being able to exit normally. However, it should be noted that the definition of signals may be different under different operating system environments, and corresponding adjustments need to be made for different operating systems. At the same time, when using signal processing programs, you also need to pay attention to the cleanup and release of global resources to avoid resource leaks and system performance degradation.
The above is the detailed content of golang dos does not exit. For more information, please follow other related articles on the PHP Chinese website!