Priority and environment variables under Linux (required for beginners)

content

1. Process priority

2. Environment variables

1. Basic concepts of environment variables and common environment variables

 2. Why do we need environment variables?

3. How environment variables are organized

4. Common commands for environment variables

 5. How environment variables are organized

 6. Global properties of environment variables


1. Process priority

What is the priority of a process? In our daily life, we have priorities everywhere. For example, we need to queue up when we are queuing for food in the canteen, and the person in front of us has a higher priority than us? So why do you need priority? In essence, there are too few resources. If there are many windows in the canteen, we don’t need to queue. Processes also have priority under linux. Let's take a look at the concept of process priority:

1. The order of CPU resource allocation refers to the priority of the process.
2. Processes with high priority have priority to execute. Configuring process priority is useful for linux in a multitasking environment and can improve system performance. You can also run the process on the specified CPU, so that the unimportant process can be arranged to a certain CPU, which can greatly improve the overall performance of the system.

 

How to check the priority of a process: ps command ps -al:

 Here are a few important pieces of information to explain:

1.UID: represents the identity of the executor

2.PID: represents the code name of the process 3.PPID: represents which process the
process is derived from, that is, the code name of the parent process PRI: represents the priority of the process that can be executed, the smaller the value, the earlier Executed
4.NI: represents the nice value of this process

PRI sum NI

1. PRI is also relatively easy to understand, that is, the priority of the process, or the order in which the program is executed by the CPU. The smaller the value, the higher the priority of the process.
2. What about NI? The smaller the value of PRI, the faster it will be executed. After adding the nice value, the PRI will become: PRI(new)=PRl(old)+nice. In this way, when the nice value is negative, the priority value of the program will become smaller, that is, its priority will become higher, and the faster it will be executed. Therefore, adjusting the process priority, under Linux, is Adjust the process nice value nice, whose value range is -20 to 19, a total of 40 levels.

 

Modify the process priority: after top
enters top, press "r" -> enter the process PID -> enter the nice value

 Several important concepts:

1. Competitiveness: There are a large number of system processes, but only a small amount of CPU resources, or even one, so the processes are competitive. In order to complete the task efficiently and compete for related resources more reasonably, it has a priority.
2. Independence: Multi-process operation requires exclusive access to various resources, and does not interfere with each other during multi-process operation.

3. Parallel: Multiple processes run separately under multiple CPUs and run at the same time, which is called parallelism.
4. Concurrency: Multiple processes use process switching under one CPU, and within a period of time, multiple processes can be advanced, which is called concurrency.

2. Environment variables

1. Basic concepts of environment variables and common 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 usually write c or c++ code, when linking object files, we never know where they are when including header files, but we can still form executable programs, because there are related environment variables to help The compiler does a lookup.

When we define variables in c language or c++, for example, we define an integer variable int a=10; the variable has the variable name and content, the same environment variable also has the variable name and the content of the variable. That is to say , environment variable = variable name + content.

How to view environment variables:

echo $NAME (NAME: environment variable name)

Example:

 2. Why do we need environment variables?

For the convenience of users, developers and systems perform some of the simplest problems of finding and locating and confirming.

You can find that we don't need to specify the path when we use the ls pwd cd instructions, but we have to bring the path when we run the program we wrote ourselves?

 We need ./mytest to be found by the system, which means that the function of ./ is to find mytest, so why do ls commands do not need a path: First, let's check the environment variable PATH:

 From the above figure, we can know that the path of the ls command is written into the environment variable, so when we use the environment variable, the system will search for the existence of the environment variable PATH. If the search is found, it will be executed, and an error will be reported if it is not found.

So can we also be executed by the system without specifying the path like ls pwd? Of course, there are two methods

Method 1: Copy the path of the program directly to the user/bin path and it will be permanently valid (but it is strongly not recommended to pollute the command pool of the linux system)

Method 2: export PATH=$PARH: path. (It will fail after turning off xsell)

 In this way, it can be executed without specifying the path like ls:

 Related commands for environment variables:

1. echo: Display the value of an environment variable

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

 

3. How environment variables are organized

When we use the ls command we can use different options to display other functions. How does ls use such a function? In fact, the main function in c and c++ uses parameters, but we don't write it at ordinary times. The prototype is as follows:

int main(int argc ,char*argv[],char*env[]); where the first parameter refers to the number of parameter lists. When we do not pass commands, the default is 1, and the second parameter is a pointer Array, which stores a pointer of char* type, points to strings, and these strings correspond to different parameters. The third parameter is an environment variable.

 The following is demonstrated by a piece of code:

    1.#include<iostream>
    2 using namespace std;
    3 int main(int argc ,char*argv[],char*env[])
    4 {
    5      for(int i=0;i<argc;i++)
    6      {
    7        cout<<argv[i]<<endl;                                                                                                                               
    8      }               
    9      return 0;       
   10 }                    
                        

We run this program:

 Let's look at a piece of code below.


    1 #include<string>
    2 #include<iostream>
    3 using namespace std;
    4 int main(int argc,char*argv[],char*env[])
    5 {
    6   for(int i=0;i<argc;i++)
    7   {
    8     cout<<"argv["<<i<<"]"<<":"<<argv[i]<<endl;
    9   }
   10   string str="-a";
   11   string str2="-b";
   12   if(argc<2)
   13   {
   14     cout<<"请输入命令行参数"<<endl;
   15   }
   16   else if(argv[1]==str)
   17   {
   18     cout<<"I am -a"<<endl;
   19   }
   20   else if(argv[1]==str2)
   21   {
   22     cout<<"I am a -b"<<endl;
   23   }
   24   else
   25   {
   26     cout<<"输入错误"<<endl;
   27   }                                                                                                                                                       
   28   return 0;
   29 
   30 }
  ~
  ~

We run this program:

 In this way, we can roughly understand that the commands ls and cd can display different contents according to different options. The essence is to get the command line parameters in the program, and then make their own judgments to achieve different functions.

4. Common commands for environment variables

1. echo: Display the value of an environment variable

 

2.export: set a new environment variable

 

3.env: show all environment variables


4.unset: clear environment variables


5.set: Display locally defined shell variables and environment variables

 5. How environment variables are organized

1. Each program receives an environment table, which is an array of character pointers, each pointer points to an environment string terminated by '\0'.

 How to get environment variables? We mentioned the three parameters of the main function above, which we can obtain with the help of the parameters of the main function. Please see the code below:

    1 #include<string>
    2 #include<iostream>
    3 using namespace std;
    4 int main(int argc,char*argv[],char*env[])
    5 {
    6   for(int i=0;env[i];i++)
    7   {
    8     cout<<env[i]<<endl;//打印环境变量                                                                                                                     
    9   }                                        
   10   return 0;                                
   11                                            
   12 }                    

We run this program:

 Is there any other way, maybe many old irons say that I don't want to pass the parameters. At this time, we can obtain it through the third-party variable environ: please see the code for details:

  1 #include<string>  
  2 #include<iostream>  
  3 using namespace std;  
  4 int main()  
  5 {  
  6   extern char** environ;//通过第三方变量获取环境变量                                                                                                        
  7   for(int i=0;environ[i];i++)               
  8   {                                         
  9     cout<environ[i]<<endl;                  
 10                                             
 11   }                                         
 12                                             
 13   return 0;                                 
 14 }                                           
~                                               
~                            

 In the same way, we can also get the environment variables, but we find that what we get is all the environment variables. We need to find specific environment variables one by one. In fact, there is a better way through getenv;

 

#incude<stdlib.h>  
#include<string>  
  2 #include<iostream>  
  3 using namespace std;  
  4 int main()  
  5 {  
  6   cout<<getenv("PATH");                                                                                                                                     
  7   return 0;                                                                                                                      
  8 }                   

 

  6. Global properties of environment variables

Environment variables usually have global properties and can be inherited by child processes

Let's look at an example:

  1.#include<stdlib.h>                                                                                                                                          
  2 #include<iostream>                   
  3 using namespace std;                 
  4 int main()                           
  5 {                                    
  6   char* tmp=getenv("MYENV");         
  7   if(tmp)                            
  8   {                                  
  9     cout<<tmp<<endl;                 
 10   }                                  
 11                                      
 12   return 0;                          
 13 }                     

The first run has no result. This is because MYENV is a local variable. The second time the environment variable MYENV is exported through the parent process, we run again:

 

Why can the child process also obtain this environment variable? This is because the child process will be based on the environment variable of the parent process bash, and the creation of the child process takes the parent process as a template

Summary: Environment variables have global properties because they can be inherited by child processes.

Guess you like

Origin blog.csdn.net/qq_56999918/article/details/123857602
Recommended