[Linux Process Control (1)] Process Termination–How to kill a process?

Blogger CSDN homepage:Hangdian Code Farmer-NEO
?
?Column classification:Linux from entry to proficiency?
?
Code Warehouse: NEO’s Learning Diary
?
Follow meTeach you more operating system knowledge
?

Process terminated

  • 1 Introduction
  • 2. The overall structure of the article
  • 3. Return the process from the main function
  • 4. Use the library function exit to terminate the process
  • 5. The difference between system call _exit and exit
  • 6. Scenarios where the process terminates abnormally
  • 7. perror function and variable errno
  • 8. Summary

1. Preface

Starting from this article, you will learn about the process
Control content includes: Process termination, process waiting
Program replacement of processes and processes, not much content, medium difficulty
Please study patiently, students!

Compared to entering CTRL + C directly on the interface
To terminate a process, we prefer to use
Some functional interfaces (system interfaces) to control process exit
The exit of a process is divided into normal exit and abnormal exit.

Key points of this chapter:

This article focuses on explaining when the process exits
Three scenarios and common exit methods,
And compare the C library function exit and system call
The difference and connection of function _exit. Finally we will
Use signals to simulate some abnormal exit situations

2. The overall structure of the article

Let’s sort out the structure of the entire article first

First, the process has three exit scenarios:

  • After the code is executed, the result is correct
  • After the code is executed, the result is incorrect
  • The code terminated abnormally

First, both exit scenarios are
Belongs to the range of normal exit, normal exit
There are also the following common methods:

  • Return from main function
  • Call exit to terminate the process
  • Call _exit to terminate the process

The third exit scenario is abnormal exit
Abnormal exit is often caused by signals.
A signal we are familiar with is:

  • CTRL + c, signal terminates process

The entire article will focus on the above content!

3. Return process from main function

When we wrote C/C++ code before, we always wrote
Write return 0 after int main, but the program can only
Return 0? The answer is definitely not!

First give two conclusions:

  1. Conclusion 1:
  • When the non-main function executes the return statement
    Indicates that this function has been executed!

  • When the main function executes the return statement
    Indicates that this process has been executed!

  1. Conclusion 2:
  • The program is executed normally and the result
    Returns 0
    when correct

  • The program was executed normally but the result was incorrect
    Returns non-0

There is a question that arises in front of us. Since
The result is an incorrect return value that is non-zero, but
There are many non-zero values, 1, 2, 3, 4, 5, etc., they
What do they mean? If you don’t understand, just verify it!

Before verifying, you must first understand a function:


It can convert error codes into error strings

int main()
{<!-- -->
    int i=0;
    for(i=0;i<200;i + + )
    {<!-- -->
        printf("[%d]: %s\\
",i,strerror(i));
    }
    return 0;
}

As expected, 0 corresponds to success!,

And after error No. 134, it is an unknown error

View the exit code of the most recent process:

Use command: echo $?

Write a code and test it by returning directly:

int main()
{<!-- -->
return 66;
}

4. Use the library function exit to terminate the process

The parameter of exit is the error code, and the main function
The return value means a

The difference between exit function and return:

  • return is only used when used in main
    Exit on behalf of this process

  • The exit function can be used anywhere in the program.
    You can exit the program directly and return the error code

Write a piece of code to verify:

void test1()
{<!-- -->
exit(10);
}
void test2()
{<!-- -->
exit(20);
}
int main()
{<!-- -->
test1();
exit(50);
test2();
return 0;
}

Use echo $? to print clearly here
The exit code that comes out is 10!

5. The difference between system call_exit and exit

Let’s look at man’s second manual
You can see the _exit system call:

Like exit, it terminates the process
And the parameter of _exit also represents the error code
So what is the difference between the two of them?

I use the following two pieces of code to verify:
Use exit for one section and _exit for another section:

Code 1:
printf("Can you see me?");
sleep(1); //Sleep for one second
exit(10);
Code two:
printf("Can you see me?");
sleep(1); //Sleep for one second
_exit(10);

The following two pictures correspond to the running results:

The effect of looking at the picture directly is not very good, here is a suggestion
Students, please type this code yourself

Phenomena: The first one prints the text, but the second one does not print it

We know that if the data printed by printf is not used
\\
If you wrap the line, the data will be stored in the buffer.
It will not be printed for the time being, but use the exit function
After the process ends, the data in the buffer is printed out
So it can be concluded that the exit function will help us
Refresh the data in the buffer, but the _exit function does not

Little Thoughts:

Since exit is a library function provided by C language
And _exit is a system call provided by the operating system
_exit cannot refresh the buffer, does it mean that the buffer is
It’s not in the operating system at all? In other words, the buffer
Not maintained by the operating system, but by the C standard?

The answer is, correct!

6. Scenarios of abnormal process termination

When we enter CTRL+C at the command line pass
When a signal kills a program, it is an abnormal termination.
When a wild pointer is used or an array is written out of bounds in the program,
, the program will crash, and the crash is also an abnormal end of the process.

For example:

Situation 1:
int* p = NULL;
*p = 20;
Situation 2:
int a[10]={<!-- -->0};
a[11]=10;

After running the program at this time, the program will exit
At this time, use the command: echo $?
It makes no sense!

The program crashed abnormally and the exit code was meaningless.
This is because generally speaking, the exit code corresponds to
The return statement crashed before it was executed!

7. perror function and variable errno

errno is a global variable in C language

It stores the latest error code
For example, when using the fopen function to open a file,
If the open fails, not only the file pointer fp
will be assigned a value of NULL, in which case the error code errno
It will also be automatically assigned by the system!

perror is a commonly used error function in C language

The perror function is always paired with errno
This function will output the error corresponding to the errno error code
information, and where the parameter of perror represents
If there is a problem, it is up to the user to decide! For example, we usually
Write a piece of code like this:

FILE* fp = fopen("csdn.txt","r");
if(fp==NULL)
{<!-- -->
perror("fopen");
exit(1);
}

Note: In the printed information, the preceding fopen:
is the information entered by the user, and the following statement is errno
Error message corresponding to error code

8. Summary

The topic of process termination is the simplest in process control
topic, learning process termination well can improve our
Code readability, error messages can be
Show more clearly!

The buffer concept presented in this article is only used for
The difference between exit and _exit, more about buffers
Content, we will explain it in the basic IO of Linux!

Next issue preview: Linux process waiting