Process priority and environment variables

Table of contents

1. Process priority

1. Priority and authority

2. View process priority

3. PRI and NI

4. Modify the priority of the process

5. Notes on process priority

2. Other concepts of process

1. Competitiveness

2. Independence

3. Parallelism and Concurrency

3. Environment variables

1. What is an environment variable

2. Classification of environment variables

3. View the content of the environment variable

(1) env command

(2) echo command

4. Common environment variables

5. Modify environment variables

(1) Temporary modification

(2) Permanent modification (user-level environment variables)

6. Local variables and environment variables

(1) Define local variables

(2) set finds local variables

(3) Create environment variables

(4) unset clears environment variables

7. Get environment variables

(1) getenv function

(2) Parameters of the main() function

(3)environ


1. Process priority

1. Priority and authority

Priority: The order of allocation of cpu resources, that is, which process is executed first, is the priority of the process.

If only whoever has the highest priority will be executed first, will the process with the lowest priority never be executed?

The answer is no: for the Linux operating system, the priority of a process generally increases over time, so those processes with low priority will also reach high priority and be processed.

Permissions: It emphasizes whether a process can be operated, that is to say, whether a certain operation can be performed.

2. View process priority

ps -l: use detailed format to display process status

ps -al: Displays the programs executed under all terminals, except the stage job leader, and displays their detailed format.

3. PRI and NI

The PRI value is easier to understand, that is, the priority of the process (priority), its size determines the order in which the program is executed by the CPU, the smaller the value, the higher the priority of the process. PRI has an initial value, which cannot be changed after being determined, so we introduce the nice value below.

The real name of NI is nice value. It is a correction value that can be positive or negative. The value ranges from -20 to 19, a total of 40 levels, indicating the correction value of the priority of the process that can be executed.

The value of PRI = the initial value of PRI + NI value, that is to say, the PRI we finally see is the sum of these two values. As NI changes, the value of PRI will change, but the initial value of process PRI will not change. .

4. Modify the priority of the process

There are many ways to modify the priority of a process, but in essence it is to change the nice value to achieve the purpose of changing the priority.

Only one method is introduced here - the top command

Let's run a process first, and view the current process through ps -la. At this time, the pid of the process is 14506, the PRI is 20, and the NI is 0. Let us change its priority.

Specific steps are as follows:

(1) Enter the top command with the root user (if it is an ordinary user, you need to use sudo to escalate the privilege), and enter the Linux task manager.

(2) Press r and enter the pid of the process whose priority you want to change

(3) After the input process pid is completed (the pid I entered here is 12127), then enter a new NI, for example, set NI to -19

The priority of the process changes from 80 to 61 (80+0 becomes 80+(-19)).

However, the modification of the NI value must be within a certain range. If we make the PRI value too large or too small, the operating system will not work properly. Therefore, in order to avoid affecting the normal operation of the operating system, the change of our NI value can only be between -20 and 19. If we input a number greater than 19, the operating system can only change NI to 19; if we input a number less than -20, the operating system can only change NI to -20, and we must not cross the boundary.

5. Notes on process priority

(1) We generally do not modify the priority of the process. It is enough to leave these things to the most professional operating system. Improper modification of the priority of the process may cause huge problems to the management of the operating system.

(2) The priority initialization PRI of the general process is 80, and the NI is 0. We can only modify the PRI by modifying the NI.

(3) The process priority can be raised. The improvement here does not mean that the user should increase it, but that the operating system increases the priority of jobs residing in memory, which generally increases linearly with time.

(4) The larger the priority value of the process, the lower the priority.

(5) The value range of the process PRI. Since the value range of NICE is [-20,19], the value range of PRI is [60,99].

2. Other concepts of process

For processes, in addition to the above basic concepts, process status and priority concepts, there are some concepts we need to understand.

1. Competitiveness

Competitiveness: The number of system processes ranges from dozens to hundreds, and there are only a few fixed CPUs (a single-core is one CPU, and multi-core is multiple CPUs), so the processes are competitive, and they are all for efficient completion Tasks, more reasonably compete for related resources, so priorities appear.

2. Independence

Independence: When multiple processes are running, various resources will serve the processes, but each process does not affect each other, which is called process independence.

Each process has its own PCB and code, and does not affect each other, even if it is a parent-child process.

3. Parallelism and Concurrency

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

Generally speaking, when we study the operating system, we think that there is only one CPU, and one CPU can only run one process at the same time (running a process and running a process are not the same thing). And some computers have two CPUs, so such a computer can run at most one process per CPU at the same time, that is, two processes. This phenomenon of multiple CPUs running different processes at the same time is called parallelism.

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

The current CPU uses the time slice rotation method when processing the process, instead of processing one and then processing the other as we imagined, but defines a very short time slice. Whenever the CPU processing process reaches a time slice, this The process must be stripped off the CPU. If the processing is completed within the time slice, it can be stripped directly.

The temporary data of the processing progress of this process is stored in the registers of the CPU, and when the process is stripped, these temporary data are transferred and stored in the PCB of the process (it is not completely stored on the PCB in actual operation, but here we can understand it like this) , and then the process PCB enters the waiting queue or the running queue again. The CPU will process the next process again. When the process is processed by the CPU again, these temporary data will be loaded into the registers again, and the CPU will continue to execute the unfinished instructions until the process is completed. (The register and the data in the register are not the same thing. When dealing with any process, the execution data of the process is saved by the register, and the data in the register can be copied and transferred back and forth)

In this way, the continuous process switching realizes the simultaneous operation of multiple processes that we see in the computer.

3. Environment variables

1. What is an environment variable

Environment variables (environment variables) generally refer to some parameters used to specify the operating environment of the operating system in the operating system, and are a batch of global variables preloaded in the memory by the operating system in order to realize some functions.

For example, when the C/C++ program we write is linked, we find our dynamic library (header file) through environment variables. Although we don't know where the dynamic and static libraries we link are, the program can still find them successfully and generate an executable program because there are related environment variables to help the compiler find them.

Environment variables usually have some special purpose and are usually global in the system.

2. Classification of environment variables

Environment variables are divided into system-level environment variables and user-level environment variables.

All environment variables that exist, the operating system will load the environment variables into memory every time any user turns on the computer. The user-level environment variables will only load the corresponding environment variables into the memory when a certain user logs in, and the system-level environment variables will be loaded into the memory no matter which user logs in to the machine.

3. View the content of the environment variable

(1) env command

Can view all environment variables

The above are all the environment variables of Linux

The env command can also use grep to find specific environment variables, for example: env | grep PATH

All environment variable contents containing PATH are displayed

(2) echo command

Format: echo+$+environment variable name

Use the echo command to view the contents of environment variables

For example, I want to view the contents of the PATH environment variable, enter: echo $PATH

You can see that this is the content stored under the PATH environment variable, where the path of the executable program is generally stored.

4. Common environment variables

(1) PATH: PATH stores the search path of executable programs

We have learned many instructions before, and the Linux system is also written in C language. Therefore, the instructions of the system are also a C language program in the final analysis, and these instructions entered will eventually execute the code of the corresponding program in the computer.

However, if we run an executable program named test, we often enter: ./test instead of directly entering program names such as pwd like instructions, which means that we need absolute or relative paths when running our own programs. The program of the instruction does not need it. This phenomenon is related to environment variables.

In the case of not specifying a path, enter a command on the command line, and the operating system will search in the path stored in PATH one by one, execute the executable program if it finds it, and report an error if it cannot find it. This explains why the executable file generated in the current directory needs to be executed with a path, because we need to tell the compiler that I am executing the executable program in the specified directory.

(2) HOME: The current user's home directory (that is, the default directory when logging in to LInux)

When we enter cd ~, this ~ corresponds to the HOME environment variable

(3) SHELL: The current shell interpreter, usually /bin/bash

bash is a system process

(4) LANG: The language, region, and character set of the Linux system

(5) HOSTNAME: The host name of the server.

(6) HISTSIZE: Save the number of historical commands.

Although the system can store up to 3000 executed commands, we can only trace up to about ten commands when we use the up key to view the executed commands.

(7) USER: The username of the currently logged in user.

For example, the cd command will check the current HOME environment variable to detect who you are and whether you have permission to operate files or folders

(8) PWD: the current directory.

Our pwd command is also implemented by reading this PWD environment variable

(9) LD_LIBRARY_PATH: The directory that C/C++ language dynamic link library files search, it is not a default environment variable of Linux, but it is very important for C/C++ programmers

Seeing the above environment variables, you can understand that many of the instructions we learned before are actually implemented through environment variables, and there are many usage scenarios for these environment variables, which we can gradually understand in the process of learning Linux.

5. Modify environment variables

(1) Temporary modification

Format: export+environment variable name=$environment variable name: new path

$Environment variable name: In order to retain the previous environment variable, it is similar to adding on the basis of the previous environment variable instead of directly modifying it

Temporarily modified features: only valid when the system is in use, and invalid after restarting.

If we also put the directory where an executable program is located into the environment variable, we can also execute this program without a path. We can give it a try.

Because the modification here is only to modify the contents of the environment variables loaded in the current memory

(2) Permanent modification (user-level environment variables)

Enter in the home directory: vim ./.bash_profile, and then go to the corresponding location to change the corresponding data.

Then enter: source ~/.bash_profile to let the host re-read the environment variables. Since we have changed the file storing the environment variables, each time the environment variables are loaded into the memory, the modified content will be loaded, which means permanently modified.

Permanently changing environment variables can cause huge problems with operating system usage, so don't change them if you can.

6. Local variables and environment variables

(1) Define local variables

In Linux, we can define local variables, and we can also use echo to view them. For example, I define a myval variable with a value of 12345, and then I can check it with echo. But local variables are only valid in the current process (that is, in bash), and are invalid in other processes. So we can't find myval when we look at the environment variables.

(2) set finds local variables

Just like the env command can find all environment variables, the set command can find all environment variables and local variables in the current process (bash).

We can find the originally defined myval local variable, but env cannot.

(3) Create environment variables

We can upgrade local variables to environment variables

Format: export+environment variable name

Local variables defined in Linux can be converted into environment variables, as long as export is added before the name of the environment variable, and the environment variable can also be found in myval at this time.

You can also create environment variables directly

Format: export+environment variable name=environment variable content

Environment variables can also be defined directly with export and content.

(4) unset clears environment variables

Format: unset+environment variable name

Use unset to clear the environment variable, unset myval, and found that we can no longer find myval

7. Get environment variables

First of all, we need to realize that environment variables are global and can be inherited by other processes. Just like the global variable in our C/C++ program, you can use it in any function. Because different processes are independent of each other and cannot interfere with each other out of thin air, without such inheritance, it is impossible for child processes to obtain environment variables.

(1) getenv function

char *getenv(const char *name)

Header file: stdlib.h

This function can obtain the first address of the content of the environment variable corresponding to the name through the system call.

The above is a piece of code we wrote, and you can see the content after running

(2) Parameters of the main() function

Seeing this, you may be surprised. It turns out that the main function also has parameters? Why have I never written parameters to the main function? These questions can be answered today.

The declaration of the main function is: int main(int argc, charargv[], charenvp[]);

  • char* argv[] is an array of pointers to store all command line parameters, each element points to a command line parameter, and the command line parameter is the option added after entering the command for us. (ls -l, where ls is an instruction, -l is an option) We can execute a certain piece of code in the C program by making judgments in the C/C++ program, so as to achieve different functions.
  • int argc is the number of command line parameters, which is essentially the number of elements in the char* argv[] array. The default is 1. If you add an available option, argc will increase by one.
  • char* envp[] is an array of pointers to store environment variables, and each element points to an environment variable.

First of all, the environment variables loaded into the memory by the operating system are all strings, both in name and content, so the address pointed to by each element of the two pointer arrays stores a piece of content, and ends with NULL at the end.

That is to say, these parameters of the main function are the environment variables and various command lines imported by the operating system into a table, and imported into the main function as parameters, and the user can access the environment variables and other content by using these two arrays.

Why don't we write these parameters up?

On the one hand, we don’t often use so many environment variables in our programming, and there are already functions in C language that can obtain environment variables. In many cases, we don’t write parameters to the mian function.

When we first learned the C language, we stipulated the writing specification of the main function, the return type is int, and the return value is 0

The return value modification returns 0 successfully, and returns a non-zero value if it fails

(3)environ

We can also achieve the purpose of accessing environment variables without passing the parameters of the main function, but using environ needs to be declared with extern. environ is a secondary pointer, pointing to the first element of charenvp[], we see the following code can also implement the env command.

Look at the results, it is indeed consistent

Guess you like

Origin blog.csdn.net/qq_65285898/article/details/128226588