Process (3) – Process priority and environment variables [Linux]

Process (3) – Process priority and environment variables [Linux]

  • 1. How the process is executed in the CPU
    • 1.1 Characteristics of processes in the CPU
    • 1.2 Register
      • 1.2.1 Process context
  • 2. Process priority
    • 2.1 How to check process priority
    • 2.2 Modify the priority of the process
      • 2.2.1 NI value
      • 2.2.2 Modification method
  • 3. Environment variables
    • 3.1 What are environment variables:
    • 3.2 PATH environment variable (just pick one to get to know)
      • 3.2.1 Functional understanding
    • 3.3 Operation of environment variables:
      • 3.3.1 View environment variables
        • i. View environment variables from the command line
        • ii. Obtain environment variables in the program
      • 3.3.2 Add environment variables
        • i. Add new environment variables
        • ii. Add to the old environment variables
          • cover:
          • Add to:
      • 3.3.2 Delete environment variables
  • 4. Command line parameters
      • 4.1 env parameter of main function
  • 5. Local variables
    • 5.2 Add local variables
    • 5.1 View local variables
    • 5.3 Cancel local variables
    • 6. Two commands in bash:
      • 6.1 General commands
      • 6.2 Built-in commands

1. How the process is executed in the CPU

Previously we mentioned what a process is and the status of the process.
We know that the data of the process is stored in memory, but we do not yet know how the CPU executes the process.

Some people know that when the CPU executes a process, it just reads the code. This is indeed the case.

So what we are going to talk about here are some details about CPU and processes.

1.1 Characteristics of processes in CPU

A process has the following properties when running in the CPU:

Competition: There are many system processes, but there are only a few CPU resources, so processes have to compete with each other. This leads to the priority discussed below.

Independence: When multiple processes are running, they do not interfere with each other
Parallel: Multiple processes running simultaneously under the CPU are called parallel
Concurrency: In layman’s terms, the CPU uses process switching to advance multiple processes at the same time.

Because the speed of process switching is so fast, special instruments must be used to test the speed of process switching.
Therefore, it is very easy to use process switching to achievethe feeling that multiple processes are in progress

And when process switching is so fast, registers have to be mentioned.

1.2 Register

A register can be said to be a very small high-speed memory in the CPU

I still remember a phenomenon that happened in Function Stack Frame before.

Why the function return value is obtained externally
We use return to return a value. After return, the function is destroyed and the return value is returned to the superior function of the calling function.
return a->mov eax10 will be placed in the CPU register
Which register exists depends on the compiler

Add here:
When returning an object, the assignment overload or constructor of the object will be called.
Instead of returning the entire object, when the object is too large and the register is too small

So after recalling the registers here, our popular function of the registers is:
The register puts the corresponding high-frequency data of the process into the register to improve efficiency.

1.2.1 Process context

The high-frequency temporary data of the process stored in the register is called the context of the process.

When the process is running:
The context of many processes is held in registers.

When a process is to be switched:
When the process leaves the CPU, the context data must be saved and taken away

The purpose of preservation is to restore it in the future.

So when the process is switched, the register needs to go through two steps:
1. Save the context of the current process
2.Resume the previous process context

Therefore, it can be said that the register plays an indispensable role in enabling fast switching of processes.

2. Process priority

Here we mentioned above: CPU resources are limited and there are multiple processes, so there is competition between processes.

And if a process cannot get cpu resources for a long time
The problem that the code of this process cannot be advanced for a long time is: Process starvation problem

2.1 How to check process priority

To understand the priority of a process, we must first look at the priority of the process

Use this command to view properties with priority

ps -al

The PRI here is priority
process priority


Let’s take a look at it here with a code that lasts all the time.

Here we take a look at the priority of this process

ps -al | head -1 & amp; & amp; ps -al | grep 5623


Here we can find that his PRI is 80

2.2 Modify the priority of the process

Here we know how to view the priority of a process

We should know how to modify the priority of a process

When it comes to modifying the priority of a process, you have to mention the NI value (nice)

2.2.1 NI value


In the priority diagram above, we can find that there is an NI value next to PRI

Why do you need to mention the NI value when modifying the priority of a process?

Because the modification of the priority of the process is:
pri(new)=pri(old) + ni(nice)

So we can say:
ni value is used to specifically change the priority value

In order to prevent users from making random adjustments, the system has a limit [-20, 19]
So a process with a priority of 80: The adjustment range is [60~99]

2.2.2 Modification method

Change process priority:
1. Enter top

2. Press lowercase r

3. Enter the process pid

4. Enter the priority value you want to change to

Here we take another look at the PRI

Here we can find that PRI becomes 99
It exactly matches the priority range we obtained through the NI range above.

3. Environment variables

We have also seen many environment variables in Windows systems

When configuring environment variables for Java or various languages, you also need to configure environment variables.

But what are environment variables:

3.1 What are environment variables:

Environment variables generally refer to some parameters in the operating system that are used to specify the operation of the operating system.

A set of variables in the form of name=value provided by environment variables

Different users have different environment variables (This means that different users have different values of environment variables)

Usually global attributes
This is because:
The processes we run are all child processes. When bash itself starts, it will read environment variable information from the operating system’s configuration file.
(So environment variable information is usually global, and the child process will inherit the environment variables of the parent process)

But when the child process changes the environment variables, it will not change the environment variables of the parent process

3.2 PATH environment variable (just pick one to get to know)

3.2.1 Functional understanding

For example, in Linux, we enter instructions and the operating system can directly execute the instructions

At this time our PATH environment variable is used:
The PATH environment variable is: the instruction search path in Linux

We have said that the essence of instructions is files.

But the operating system can find the corresponding file for the instructions we enter.


Here we can use which to search for the pwd location.

Then we print the PATH environment

You can find the path where pwd is located in PATH:
/usr/bin

3.3 Operation of environment variables:

There are only three operations that come to mind:
Add, view and delete

3.3.1 View environment variables

There are also two ways to view environment variables here:

i. View environment variables from the command line

View all environment variables:

env

Just enter it directly on the command line

Here you can see that all environment variables have popped up.

View specified environment variables:

echo $environment name

ii. Obtain environment variables in the program

We know that the operating system kernel is written in C language
Therefore, C language must also have an interface for displaying environment variables:

The C language contains the header file stdlib.h
getenv() function


Here we use the getenc interface to directly find the PATH environment variable.

3.3.2 Add environment variables

After knowing how to view environment variables, the next step is to add environment variables.

There are two ways to add environment variables here:

i. Add new environment variables

Adding a new environment variable means naming and defining the environment variables you add yourself.

export variable name=corresponding value

Here we add a TEST for testing

Here we use env to check again, and we can see that the environment variables have been added.

ii. Add to the old environment variables
Override:

The usual environment variables are NAME=NUM

Generally, environment variables are directly equal to a value. If they need to be modified, they are usually overwritten directly.

direct

Variable name=value

Just modify it directly

Add:

As we have seen above, the PATH environment variable stores a large number of instruction search paths:

Therefore, rather than overwriting, it is more necessary to add a new path based on the original path.

PATH=$PATH:/own file path

The $PATH here represents the path of the original PATH.


Here you can find that our file path is newly added to the path.

But here we have to note that all the environment variables we add are memory-level environment variables.
After we restart the shell, the environment variables will disappear.

3.3.2 Delete environment variables

Here we first add an environment variable casually

unset variable name

You can delete the variable

4. Command line parameters

You may have seen in some books that the main function can pass parameters in this form:

int main(int argc,char* argv[])
{<!-- -->}

We may not be able to see it when we usually write code in the compiler.

But when we start writing using the command line in Linux, we can take a closer look.

Let’s use it casually here:

argv is an array of pointers which stores each string.

Then let us traverse it, print each element in argv, and view it.

It is found that argv here stores segments of command line parameters.

Here we can explain the form of this command line parameter.

When we usually enter parameters on the command line

./parm_test -a -b -c

In the eyes of the compiler, you just input a large string:
"./parm_test -a -b -c"

The compiler will then group the parameters (stored in argv):

argv is an array of pointers that stores each string.

Here we can see that the following elements are empty
So the way we traverse can be optimized:

for(;argv[i];i + + )

Because when NULL is encountered, it is equivalent to the integer 0 and will automatically stop.

4.1 env parameter of main function


The argv and env mentioned in the figure here are called core vector tables.

In some textbooks, the first two may be more common, but the third one is indeed relatively rare.

We can see that this env is not our previous environment variable

But this env is an attribute of the pointer array.
So it can be seen here that In fact, environment variables are also large strings

So the environment variables seen by env before are actually strings

5. Local variables

Environment variables mentioned above

Let’s talk about local variables here

We mentioned above
Environment variables have a very important feature: globality

Because the child process will inherit from the parent process.

But the local variables we want to mention here will not be inherited.

5.2 Add local variables

Here directly add a local variable to see:

Variable name = variable value

The difference between this and environment variables is the addition of an export


Here we add a variable

5.1 View local variables

After adding it, you need to know how to view it
Of course it is impossible to view it with env.

Because env is used to view environment variables
Environment variables need to be inherited by child processes, but local variables are not inherited.
So it will not be placed in environment variables.

To check the environment variables here, you need to use

set

Here you can see that this set not only has local variables but also environment variables.

Here we use grep to find the environment variables we added.

5.3 Cancel local variables

Here we find the added local variables.

You can cancel the environment variables

unset variable name

This is the same way to delete environment variables.


Here you can find that the deletion was successful

6. Two commands in bash:

Why is the bash line command suddenly pulled out here:

because:

Here we can directly use echo to print out local variables.

We have clearly mentioned before that local variables cannot be inherited by child processes.

Logically speaking, echo is an instruction. The essence of an instruction is a file, and running a file is a process. When bash creates a process to run, it needs to create a sub-process of echo.

So it stands to reason that echo cannot use this local variable of the bash parent process.

Here is a misunderstanding to be corrected:
Instructions launched from the command line do not necessarily create child processes.

Here we will divide the commands into two categories.

6.1 General commands

One is: some commands cannot be executed directly by the operating system and need to call files, so subprocesses need to be created.
Leave the function to the child process to execute.

This is called a:Regular command – done by creating a child process.

6.2 Built-in commands

Another one is: The system is very reliable in execution, and it can be said that it is good at it
This is called: Built-in command – bash does not create a child process, but executes it by itself, similar to bash calling functions written by yourself or provided by the system