Linux– Process priority and environment variables

Directory

process priority

basic concept

How to check priority

PRI and NI

NI value setting range

How to modify the NI value

Modification method 1: Modify the priority through the top command

Modification method 2: Modify the priority through the renice command

Four important concepts of process

environment variable

basic concept

Common Environment Variables

View environment variables

The role of three environment variables (not recommended to modify)

PATH

Method 1: Put our executable file in the PATH path

Method 2: Add our current path to the PATH path

HOME

SHELL

Environment variable related instructions

How environment variables are organized

The parameters of the main function

Get environment variables through system functions


Priority of the process

Basic concept

  1. What is process priority?

The order of cpu resource allocation refers to the priority of the process (priority)

  1. Why do priorities exist?

The reason why priority exists is essentially because of insufficient resources. In the system, there are multiple processes but only one cpu.

How to check priority

We can use the ps -l command to view the process

ps -l

  • UID The identity ID of the executor
  • PID represents the code name of the process
  • PPID stands for the ID of the parent process
  • PRI represents the priority of the process
  • NI represents the nice value of this process and is used to modify the priority of the process

PRI and NI

  • PRI is the priority of the process, which is the order in which (lightweight) processes are executed by the CPU. The smaller the value, the higher the priority of the process
  • NI Indicates the modified value of the priority at which the process can be executed
  • The relationship between PRI and NI values conforms to the following formula PRI (new) = PRI (old) + NI
  • When the NI value is negative, the program will have a smaller priority value and a higher priority
  • Adjusting the process priority is to adjust the nice value of the process
  • The value range of nice is -20~19, a total of forty values

In the Linux system, the PRI (old) value defaults to 80, so the priority in Linux is PRI (new) = 80 + NI

Setting range of NI value

The setting range of NI is fixed and must be -20~19. If it exceeds this value, the set nice value will become the value closest to this range.

If you set the NI value to -100 then its value will be set to -20

If you set the NI value to 100, its value will be set to 19

Why is the range of NI values set like this

Because the operating system should try its best to ensure the fair operation of each process. If we can set the priority of the process to be very low, the operating system will tend to execute this process, which will cause other processes to be unable to be executed by the CPU. Therefore, PRI Values are preferably in the range

How to modify NI value

Modification method 1: modify the priority via the top command

The top command is equivalent to the task manager

After we call the top command, an interface like this will come out

Hold down the r key to enter the PID of the process whose NI value needs to be adjusted

After entering the PID, press Enter and then enter the NI value

Enter the modified NI value of 15 and press Enter, press and hold the q key to exit top

This is because 6646 is a child process of 5850 and will inherit the code and data of the parent process

Modification method 2: modify the priority via the renice command

The command is renice + NI value + process number

Ordinary users who want to renice need to use the sudo command to temporarily elevate their privileges

Four important concepts of process

  • Competitiveness: There are many system processes and only a small amount of CPU resources, or even one CPU resource. Therefore, there is competition between processes. In order to complete tasks efficiently and compete for related resources more reasonably, they have priority
  • Independence: Multi-process operation requires exclusive use of various resources and does not interfere with each other during multi-process operation
  • Parallelism: Multiple processes run simultaneously under multiple CPUs
  • Concurrency: Multiple processes use process switching under one CPU to allow multiple processes to advance within a period of time, which is called concurrency

Environment variables

Basic concepts

Environment variables (environment variables) generally refer to some parameters used in the operating system to specify the operating environment of the operating system

Since it is in the operating system, it is generally a global variable

Common environment variables

  • PATH: Specifies the search path for commands
  • HOME: Specify the user’s main working directory (that is, the default directory where the user logs in to the Linux system)
  • SHELL: The current shell, its value is usually /bin/bash

View environment variables

echo $PATH

PATH

Why can we execute the executable files of our system directly but not the executable files compiled by ourselves?

This is because of the existence of the environment variable PATH

The reason why your own executable files need to specify the location is because they are not in the PATH path

Method 1: Put our executable file in the PATH path

Method 2: Add our current path to the PATH path

export PATH=$PATH:path

HOME

SHELL

The various commands typed in the Linux operating system actually need to be interpreted by the command line interpreter, and there are many command line interpreters in Linux (such as bash, sh). We can check the environment variable SHELL to know which one we are currently using. Types of command line interpreters

We view SHELL using the following command

env : display all environment variables

set: Display locally defined shell variables and environment variables

unset : Clear environment variables

Environment variable organization method

In Linux, environment variables are organized through a table. If we use the knowledge of C language to understand, the environment variable table is actually a secondary pointer.

It points to a first-level pointer array, which contains various environment variables. The last environment variable is NULL.

parameters of main function

The main function has parameters

It has three parameters, namely argc argv envp

Where argv is a pointer, it points to an array, which stores data of type char*
argc is an integer which identifies the number of valid elements in argv

Below code to verify

 int main(int argc, char* argv[])
{
      if(argc != 2)
      {
          printf("Usage: %s -[a|h]\\
", argv[0]);
          return 1;
      }
      
      if(strcmp(argv[1], "-h") == 0)
      {
          printf("hello world\\
");
      }
      else if(strcmp(argv[1], "-a") == 0)
      {
          printf("hello all\\
");
      }
      else
      {
          printf("hello\\
");
      }
    
      return 0;
}

Environment variables can be obtained through the edge

int main(int argc, char* argv[], char* env[])
{
      for(int i = 0; env[i]; + + i)
      {
        printf("%d->%s\\
", i, env[i]);
      }

      return 0;
}

You can also directly use the secondary pointer environ to get environment variables

Note that the global variable environ defined in libc points to the environment variable table. environ is not included in any header file, so it must be declared with extern when using it

extern char** environ;
for(int i = 0; environ[i]; i ++ )
{
    printf("%d->%s\\
", i, environ[i]);
}

Get environment variables through system functions

You can use the getenv system function to view environment variables

printf("PATH: %s\\
", getenv("PATH"));
printf("HOME: %s\\
", getenv("HOME"));
printf("SHELL: %s\\
", getenv("SHELL"));