Linux ctrl-c explain (ctrl-c detail)

Posted by shenmue232 on Fri, 10 May 2019 16:38:56 +0200

Catalog

  • 1 Background posture
  • 2 foreplay
  • 3 dinner
  • 4 systemctl stop docker
  • 5 demo

1 Background posture

drive

Driver, full name device driver, is a special program added to the operating system, which contains information about hardware devices. This information enables the computer to communicate with the corresponding equipment.

interrupt

A way in which the trigger system returns the running time from the user-mode program to the kernel-mode.

terminal

terminal

Pseudo Terminal - >/dev/pts/8

Conversation

signal

A software interrupt sent to a program to indicate an important event.
After the system receives the signal, the CPU time will be handed over to the kernel state, and then exit the program, hang up, restore, or customize the operation.

Common signal

signal Key Significance
SIGINT Ctrl-C Exit all front-end processes of the current session
SIGTSTP Ctrl-Z Hang up the front desk process
SIGTERM Elegance kills the specified process and can be blocked
SIGKILL Mandatory killing of specified processes
Ctrl-D Not signal, write EOF to input

Sending signal

kill -SIG pid

Custom Signal

trap "echo 'signal SIGINT received'" SIGINT

Extended Tips 1:

The origin of nohup:

nohup python3 manage.py runserver &

When the terminal interrupts (disconnection, wifi disconnection, etc.), the relevant driver will give the current session.
(session, in short, a login shell is a session)
All programs (foreground and background) send hang up(HUP) signals to make the program exit.

Extended Tips 2:

Ctrl- signal SIGQUIT
Ctrl+J is actually a newline character.
Pressing Ctrl+I has the same effect as pressing Tab.
Ctrl+[same as ESC.
Ctrl+H replaces the BackSpace key.
Right mouse button shortcut: VK_APPS (93)

2 Foreplay - linux Process Life Cycle

3 dinner

4 systemctl stop docker

crime scene

An article by Uncle Mouse Blog

What is system d?

init program, process scheduling management program, id 1, father of all processes

System CTL is the shell interface of system D. Common operations: system CTL start | stop | status | reload

systemctl stop

The behavior of docker service is defined in / usr / lib / system D / system / docker.

man: the process is terminated by sending the signal specified in KillSignal=(SIGTERM) when service stop is requested.

dockerd trap

After system CTL stop dockerd executes, it sends SIGTERM signal to dockerd. When dockerd captures this signal, it calls cleanup cleanup to clean up the container under it. At the same time, the counter adds one. After receiving three times SIGTERM or SIGINT (such as pressing ctrl-c), it will start "force exit without cleanup" and forcibly exit dockerd, which will cause dockerd to retreat. Out, but the container does not exit, so the container occupies but the resource (ip address, etc.) will not be released. In addition, if kill-9, SIGKILL will be sent and dockerd will be killed forcibly, but the container will not be cleared. So use ctrl-c carefully, kill-9 carefully!!

// * If SIGINT or SIGTERM are received, `cleanup` is called, then the process is terminated.
// * If SIGINT or SIGTERM are received 3 times before cleanup is complete, then cleanup is
//   skipped and the process is terminated immediately (allows force quit of stuck daemon)
// * A SIGQUIT always causes an exit without cleanup, with a goroutine dump preceding exit.
c := make(chan os.Signal, 1)
    // we will handle INT, TERM, QUIT, SIGPIPE here
    signals := []os.Signal{os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGPIPE}
    gosignal.Notify(c, signals...)
    go func() {
        interruptCount := uint32(0)
        for sig := range c {
            if sig == syscall.SIGPIPE {
                continue
            }

            go func(sig os.Signal) {
                logger.Info(fmt.Sprintf("Processing signal '%v'", sig))
                switch sig {
                case os.Interrupt, syscall.SIGTERM:
                    if atomic.LoadUint32(&interruptCount) < 3 {
                        // Initiate the cleanup only once
                        if atomic.AddUint32(&interruptCount, 1) == 1 {
                            // Call the provided cleanup handler
                            cleanup()
                            os.Exit(0)
                        } else {
                            return
                        }
                    } else {
                        // 3 SIGTERM/INT signals received; force exit without cleanup
                        logger.Info("Forcing docker daemon shutdown without cleanup; 3 interrupts received")
                    }
                case syscall.SIGQUIT:
                    DumpStacks("")
                    logger.Info("Forcing docker daemon shutdown without cleanup on SIGQUIT")
                }

code is here.

Extended Tips3:

How to delete processes and subprocesses:

Processes and subprocesses in the same group can kill a group of processes according to PGID

kill -- -$PGID   Kill using the default signal (TERM = 15)
kill -9 -$PGID   Kill using the KILL signal (9)

other methods

5 I want more

Unix Environment Advanced Programming
Learn C!

Topics: Linux Docker Session shell