[Linux] process termination, waiting (not including process replacement)

The signal part will be discussed in detail later, this article does not cover 

Table of contents

1. Process termination and understanding of exit codes

2. The process exits

3. Process waiting 


1. Process termination and understanding of exit codes

1. Situation classification 

(1) After normal execution

a. The result is correct 

b. The result is not correct and reflect on why?

(2) Crash during operation (process exception) The nature of the crash: for some reason, the process receives a signal from the operating system (such as kill-9)

The main function we wrote before will be equipped with return 0; this number 0 is the exit code of the main function process

Process exit code: indicates whether the result of the process execution is correct , generally 0 means success, non-zero means failure

Therefore, the exit code can be used by the user to determine the health status of the process exit

How to extract the exit code?

use this command 

Then I don’t know what the exit code is, but I know that the exit code contains certain information. This information will tell us the exit status of the process . Now I only know that 0 means that the process exited successfully. What about other exit codes?

First of all, we need to know the function to get the error message

Entering the error code will get the error message string. In fact, we often see that the return value of the function call in Linux has a sentence. The error code is stored in errno (corresponding to the as if set to errno of the above strerror function), then The exit code is also an error code, which can be used to read the error message, so we use such a function to view the exit code in the system

  

turn out

...

There is basically no error message until 134, that is, the exit code of the system is actually 133

Specifically, 0 corresponds to success (normal exit), and 2 corresponds to the absence of the current file/directory you mentioned.

This error is so familiar~~~~~

Demonstrate (the current directory does not have the tmp file)

 ! ! ! Why do I feel that I didn't write the code, but I can also find the exit code?

Because your command ll is a process, we did not write ll before, process, command, program, there is no difference between these three, so as long as the process has an exit code, then the command also has an exit code

Speaking of which, I can simply write a program to see how to get the exit code 

Exit must be familiar to everyone. I used to write exit (-1) when the program needs to exit abnormally.

  It is found that it is indeed obtained, but echo $? is always 0

As I said just now, the command also has an exit code. echo $? checks the exit code of the latest program , and the second check is the exit code of the first echo $?. Since this command is successful, the exit code is 0

Since the system has an exit code, we can customize the exit code

Method: Write a table full of error messages, and then assign values ​​to the strings in the table

2. The process exits

  • How to understand process exit?

Process = kernel data structure + code and data

If there is one less process in the OS, the code and data corresponding to the process must be released (if there is an independent one)

  • What are the ways to exit

1. What about the return of the main function and the return of other functions? It only means that the function returns, and the essence of process execution is main execution flow execution

2. exit exit

You can use man to check the No. 3 manual to see exit

 There is also a function _exit that can be found in the No. 2 manual

 At this time, I already feel that something is wrong

man 1: user instructions

man 2: system calls

man 3: C library calls

We recommend exit more, the reason is to see the following code

 This code uses _exit but without us manually refreshing the cache area, but it has not been refreshed until the end (because the result after running does not see anything)

replace with exit()

see hello

So the difference between _exit and exit is naturally reflected

 So let's guess where the buffer is?

It is definitely not inside the operating system. If it is, _exit also calls the system interface, so the buffer can also be refreshed. This does not match the facts, so only the user layer and the C library are left (we will talk about it later)

It can be guessed that exit() is actually a package of _exit

This answers the question of how the process exits (i.e. how to delete the process)

The deletion process must be completed by the operating system, so the operating system provides the interface to the user, and exit calls this interface to complete the deletion


3. Process waiting 

1. Why wait?

a. Avoid memory leaks (must be done now)

b. Obtain the execution result of the child process (if necessary)

This leads to new thinking why create subprocesses?

Because sometimes the parent process wants to assign some tasks to others, and at the same time, it can do other things, or it can do nothing by itself (waiting for the deduction process), so I need to know what my child process will do when I hand over the task. Whether things are done well or not - get the execution result of the child process

The execution results are as follows:

1. The result of the code running is right --> exit code

2. The result of the code running is wrong ---> exit code

3. The code runs abnormally ———> signal

So the exit code + signal is the most important in this process

2. What is waiting?

The method of obtaining the exit code or exit signal of the subprocess through the system call releases the memory problem by the way (because the exit code/signal contains a lot of information, we can know how the subprocess is completed by obtaining it)

Who is waiting? parent process

Who are you waiting for? child process

3. How does the process wait?

用wait/waitpid

Checked by man 2 wait (Manual No. 2 is the system call interface manual)

Look at these two functions, the parameter is int *status --> output parameter

That is, the value of status will be obtained inside the wait/waitpid function, and the status will be obtained at the end of the function call

Then take waitpid as an example (he has more parameters, you can understand him if you understand waitpid)

Make it clear what we want to get through status?

The exit code of the child process + signal

So how to check the signal? kill -l

The found signal is also an integer

So what are bitmaps?

For example: Suppose four people eat, there are eight chairs in the room (ordered), we use 1 to indicate that there are people on this chair, then the eight chairs can be represented as 0111 0100 - this can be a bitmap

The same permission is also a kind of bitmap (octal representation of permission)

 That is, the 32 bits of status are as follows

Among them, the core dump will not be mentioned for the time being

 So how to get exit status and signal respectively? bit operation

Get signal - status & 0x7F

Get exit code - (status>>8) & 0xFF

  • How does the parent process obtain information about the child process?

  • If the child process does not exit when the parent process is waiting, what is the parent process doing?

  •  What if we don't want to block and wait? That is, what should I do if I don’t want to get stuck in waitpid?

short story:

You call a guy and he says he's busy, you say let him go first but don't hang up

Calling corresponds to the system call waitpid, at this time he did not hang up the phone - blocking call - there are two results: OK/error

You still have something to do with him the next day, but this time he hangs up after saying something, you call again and again, and he hangs up after saying that he is not ready again and again-do many status checks-non-blocking polling Result: ok/not ok/error

  • How to set up non-blocking polling?

plus return 0; 

 Of course, in fact, we can use macros to judge whether the process exits normally and check the exit code of the process.

Memory methods are bolded in the figure

Of course, the parent process can do something else during non-blocking polling (you can write your own code) 

There is a follow-up: process replacement, follow and don’t get lost

Guess you like

Origin blog.csdn.net/weixin_71138261/article/details/130541316