Linux process (2) --- process priority and environment variables

I want to complete an unfinished topic in the previous chapter here. The last one we talked about zombie process means that the child process has ended, but the parent process is still running and has not had time to recycle . At this time, the child process is a zombie process.

But if the parent process finishes running and ends without recycling, then who will maintain the child process?

At this time, the orphan process will be adopted by the No. 1 init process , and of course it must be recycled by the init process.

Now that the tail is finished, let's get to the point:

Table of contents

process priority

basic concept

View process priority

PRI and NI

How to adjust the priority of a process

The difference between PRI and NI

other concepts

competitive

independence

parallel

concurrency

the context

environment variable

concept

problem introduction

common environment variables

PATH

View environment variable methods

Import the PATH environment variable

HOME

SHELL

Commands related to environment variables

How environment variables are organized

Get environment variables

Get environment variables through code

Get or set environment variables through system calls

Environment variables usually have global properties

command line parameters


process priority

basic concept

1. The order in which the CPU allocates resources refers to the priority of the process.

2. The process with high priority has the right to execute first. Configuring process priority is useful for Linux in a multitasking environment and can improve system performance.

3. It is also possible to run the process on a designated CPU. In this way, assigning unimportant processes to a certain CPU can greatly improve the overall performance of the system.

In a word: Process priority is to confirm who gets the resource first and who gets the resource later.

So why should there be priority?

The essence is because the CPU is limited and there are too many processes.

There are too many processes, and we need to compete for resources in some way. For example: when we line up to buy tickets, soldiers can have priority, and ordinary people line up, where the priority of soldiers is relatively high.

In the computer, we can use some data to indicate the priority, only the priority is not enough, there must be a judgment, for example, if you are the first in your school, no one cares what grade you are if you are put on an isolated island , only in school, teachers, and leaders will judge you based on these grades, so that it will be effective.

These evaluation priorities are called schedulers. This priority is our scheduling indicator. Those with high priority will be dispatched to the CPU for execution first.

View process priority

Let me talk about the conclusion first: the specific method of priority under Linux is:

Priority = old priority + nice value. This will be explained later.

Let's write a program first.

 

Then the old operation, while executing, while monitoring

 It is found that our process will also have two attributes, ps -al is to display more detailed process information.

Among them, PRI represents the priority that this process can be executed, and the smaller the value, the earlier it will be executed.

NI stands for nice value.

 We will introduce them separately.

PRI and NI

Regarding PRI and NI, we need to know the following points.

1. PRI is still relatively easy to understand, that is, the priority of the process , or in layman's terms, the order in which the program is executed by the CPU. The smaller the PRI value, the higher the priority of the process.

2. NI is what we call the nice value, which represents the modified value of the priority of the process that can be executed.

3. The smaller the PRI value, the faster it will be executed. After adding the nice value , the PRI will become: PRI(new)=PRI(old)+nice.

4. In this way, when the nice value is negative , the priority value of the process will become smaller , that is, its priority will become higher , and the faster it will be executed .
5. Therefore, to adjust the process priority, in Linux The next step is to adjust the nice value of the process
6. The value range of nice is -20 to 19, a total of 40 levels

How to adjust the priority of a process

As mentioned above, adjusting the priority of the process is to adjust the nice value. How to adjust it?

First enter the top command, and then press the 'r key.

 There will be such a sentence, prompting you to enter the process to modify the nice value.

 

 The pid of the process we just ran was 23494, so we enter 23494.

 Then let us enter the nice value, we enter 15, and the process priority at this time should be the previous 80+15=95.

Let's look at it again:

 It is found that the process priority is indeed 95 as we thought, and the NI value is now the value we adjusted to 15.

It should be noted that each adjustment starts with 80 as the benchmark value and will not be affected by the current PRI value.

We enter -15 at this time

 Instead of 95-15 and becoming 80, it starts at 80, 80-15=65.

The result is exactly what we said. 

The difference between PRI and NI

PRI is the priority of the process, and the NI (nice) value is the correction data of the process priority

The nice value of the process is not the priority of the process, they are not a concept, but the nice value of the process will affect the priority change of the process.

other concepts

competitive

Competitiveness: There are many system processes, but only a small amount of CPU resources, or even one, so the processes are competitive . In order to efficiently complete tasks and compete for related resources more reasonably, they have priorities .

independence

Independence: Multi-process operation requires exclusive use of various resources , and does not interfere with each other during multi-process operation. For example, if the browser is hung up, it will not affect the normal use of qq. If the child process is hung up, it will not affect the parent process. If the parent process is hung up, it will not affect child processes.

parallel

Parallelism: Multiple processes run separately and simultaneously under multiple CPUs , which is called parallelism.

 

concurrency

Concurrency: Multiple processes use process switching under one CPU to allow multiple processes to advance within a period of time, which is called concurrency.

Do you have such a doubt here: if the process switching method is used for concurrency, if the process is not finished and you switch to another process, then when you switch back to this process next time, where does it start? start from scratch? The efficiency is too low, we should start from the original position, so how does the CPU know where the last execution was?

the context

Let me talk about a phenomenon first: if process A is running, then the temporary data of process A must be saved in the registers of the CPU ! These temporary data, called the context of A , include what the last return value of the function was, the running status, the running location, and so on.

So when process A is temporarily switched off, process A needs to take away its own context data by the way, which is equivalent to saving it in process A.

The purpose of temporary saving: it must be taken out next time, and if it can be restored, it can continue to run backwards according to the previous logic, as if it has not been interrupted.

There is only one register in the CPU, but there are multiple contexts, corresponding to different processes ! Only one copy of the context can be read per register.

environment variable

concept

Environment variables (environment variables) generally refer to some parameters used in the operating system to specify the operating environment of the operating system.
For example: when we write C/C++ code, when we link, we never know the dynamics of our link Where is the static library, but it can still be linked successfully to generate an executable program, because there are relevant environment variables to help the compiler find it. Environment variables usually have some special purpose, and they usually have global characteristics
in the system .

problem introduction

Have you ever noticed something like this:

We need to add ./ (that is, the current path) to run the self-written executable program , and do not need to add ./ when executing commands such as ls, touch, and rm . Of course, it is also possible to add its path.

 

 The only difference between these ls commands and the ones we wrote is: one is written by ourselves, and the other is a command that comes with the system. Since the system has already configured environment variables, it can be run directly without adding a path.

Now you know that this is the role of environment variables, and you will understand it later.

common environment variables

PATH

View environment variable methods

echo $PATH

 Note that the paths between them are separated by: .

When we input ls, the system will first search from the first path , if there is, it will be executed directly, if not, it will continue to search backward until it is found, if there is no one after searching, then an error will be reported, command not found, command could not find it.

So if we want to execute our commands, we don’t need to add paths. We can write our executable programs into these PATH paths, but we don’t recommend this, because it will pollute the command pool already written by others.

So we can put what we wrote into the environment variable, which is equivalent to a single .

Import the PATH environment variable

Enter the command:

export PATH=$PATH:/home/liqi/process

Inside this: The following is the path of your executable file, you can use pwd to view it

Then we look at the environment variables again.

 

Found that it has been imported successfully.

At this point, we can directly run the executable program under the process.

 

 When you log out and log back in, the environment variables will be initialized to their original state.

HOME

It will record and display the home directory of the current user.


So the cd ~ mentioned before, enter your own home directory, in fact, it is carried out according to this HOME.

SHELL

Display the current shell, usually /bin/bash.

 

Commands related to environment variables

1. echo: display an environment variable value
2. export: set a new environment variable
3. env: display all environment variables
4. unset: clear environment variables
5. set: display locally defined shell variables and environment variables

How environment variables are organized

Each program will receive an environment table, which is an array of character pointers, and each pointer points to an environment string ending with '\0'

 

Get environment variables

Get environment variables through code

In fact, our main function has three parameters, int argc, char* argv[], these two are command line parameters , which will be discussed later.

char* env[] is the environment variable parameter.

This is an array of pointers, which is the table in our previous section.

We can write the following program:

 

Then exit, make compiles, then runs

 

It can be found that all environment variables are output in this way. 

Get or set environment variables through system calls

There are two interfaces:

putenv is used to set, which will be discussed later

getenv, get environment variables.

For example, we want to get the PATH environment variable.

Enter code:

 

Then make compiles and runs:

Replace PATH with HOME:

 

Similarly, it is also output. 

 Do we have such a question, who imported these environment variables into these variables?

In fact, it inherits the environment variables of the parent process . By default, all environment variables will be inherited by the child process. The parent process is our bash , which is a simple process replacement, which will be explained later.

This also illustrates the fact that environment variables have global attributes !

Because it can be accessed by many child processes.

Environment variables usually have global properties

Let's do an experiment to prove it:

 We changed the environment variable name to myenv.

It can be found that there is nothing and a segment error has occurred.

At this point we import another environment variable named myenv.

export myenv="hello,world"

 At this time, bash already has the environment variable myenv. To run a program, you need to create a child process first, and the child process will inherit the parent process. It is because the environment variable has global attributes !

At this point, we compile and run make again, and find that this environment variable has been successfully output.

This shows two things:

1. The environment variables of the child process come from the parent process.

2. By default, all environment variables will be inherited by child processes (environment variables have global attributes).

Then the child process of the child process will also inherit the environment variables of the child process

command line parameters

This is an add-on.

When we explained the environment variables just now, we said that the main() function has three parameters, the last parameter we said, then there are the first two parameters, these two parameters are called command line parameters .

Let's write a program to see what this is.

 

 We found that it actually passed the options we entered into argv as parameters, and the specific number is determined by argc.

What's the point of these command line parameters?

The biggest function is: Let the same program use different sub-functions through different options!

This is true for ls -a, ls -a or ls -a -l.

These are all done internally with command line parameters.

Or we implement a simple

The scenario would be as follows:

 

 So this is the real usage of command line parameters.

 

Guess you like

Origin blog.csdn.net/weixin_47257473/article/details/131717187
Recommended