Home >Backend Development >Golang >golang dos does not exit

golang dos does not exit

王林
王林Original
2023-05-15 12:37:40652browse

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:

  1. Call os.Exit to exit the program and end the process directly;
  2. Call os.Process.Kill to kill the process;
  3. The program exits automatically after all coroutines are completed.

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:

  1. Use the signal.Notify function to register one or more signal receivers;
  2. When the program is running Wait for the reception of the signal and process the received signal;
  3. After processing the signal, you can adaptively perform os.Exit or other cleanup operations in the program.

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!

Statement:
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