[Linux] Process priority environment variable

Process priority environment variable

  • 1. Process priority
    • 1. Basic concepts
    • 2. View and modify the priority of the system process
    • 3. Some other instructions and function calls about process priority
    • 4. Some process properties related to process priority
  • 2. Environment variables
    • 1. Basic concepts
    • 2. Commands related to environment variables
    • 3. Introduction to common environment variables in Linux
    • 4. The organization of environment variables and how to obtain environment variables in C code
    • 5. How are the environment variables of different users formed?
    • 6. The command line parameters of the main() function

1. Process priority

1. Basic concepts

The order of cpu resource allocation refers to the process priority (priority).
Processes with higher priority have the right to execute first. Configuring process priority is useful for Linux in a multitasking environment and can improve system performance.
You can also run the process on a specified CPU. In this way, assigning unimportant processes to a certain CPU can greatly improve the overall performance of the system.

2. View and modify the priority of the system process

In Linux or Unix system, using the ps –al command will output the following content similarly, among them:

  • UID : represents the identity of the executor
  • PID: represents the code name of this process
  • PPID: Represents which process this process is derived from, that is, the code name of the parent process
  • PRI: Represents the priority that this process can be executed, the smaller the value, the earlier it will be executed
  • NI: Represents the nice value of this process, which represents the modified value of the priority of the process that can be executed.

    We see that we now have two processes bash ps and their process priority is 80. If we want to modify their priority, we need to use the nice value of NI.

    P

    R

    I

    (

    no

    e

    w

    )

    =

    P

    R

    I

    (

    o

    l

    d

    )

    +

    no

    i

    c

    e

    PRI(new)=PRI(old) + nice

    PRI(new)=PRI(old) + nice

From this formula we know that new PRI = old PRI + nice value, but the value of this old PRI refers to the initial default value of PRI, such as the above bash ps is 80, then no matter how this PRI is changed to the old PRI in the future, it will be 80. Of course, the default PRI of most processes is 80.

In addition, nice has a range! The value range is from -20 to 19, a total of 40 levels.
So let’s try to change the priority of the processC process.
Note: To increase the process priority (that is, to set the nice value to a negative number) requires the root user to operate!

Linux commands to modify process priority:

  • top command
  • Enter top and press “r” -> input process PID -> input nice value

press “r”

Enter -20

View the relevant information of the process again:

We found that the priority of the process has indeed changed, but we can change the priority of the process is limited

[

?

20

,

19

]

[-20,19]

[?20,19], because the scheduler does not allow us to set the priority of a process too high, which makes it difficult for other processes to be scheduled.

3. Some other instructions and function calls about process priority

  • nice directive
    The function of the nice command is to adjust the priority of the process and allocate system resources reasonably.
    The -n parameter is the priority level of the nice value,

Execute the specified program with a nice value of -5

View process priority

  • The renice command can modify the scheduling priority of a running process.
    renice changes the scheduling priority of one or more processes. The first parameter is the priority value to use, and the other parameter is expressed as process identification information.
renice [-n] priority [-gpu] identifier

-g, followed by the pgid of the group, to change the process priority of a group
-u, followed by user name or uid, change the process priority owned by a user.
-p, followed by pid, change the process priority of a process.

Use the renice command

  • function call
    In Linux, there are two main function calls for changing process priority: getpriority() and setpriority(). We will not introduce the specific detailed usage here. If you are interested, you can read another explanation about getpriority() and setpriority() in my 【Linux】column.

4. Some process properties related to process priority

  • Competition: There are many system processes, but only a small amount of CPU resources, or even one CPU resource, so there is competition among the processes. In order to complete tasks efficiently and compete for related resources more reasonably, they have priorities
  • Independence: Multi-process operation requires exclusive use of various resources, and does not interfere with each other during multi-process operation.
  • Parallel: Multiple processes run simultaneously under multiple CPUs, which 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

2. Environment variables

1. 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. The environment variables usually have some special purposes and generally have global characteristics in the system.

For example: when we write C/C ++ code, we never know where our linked dynamic and static libraries are when we link, but we can still link successfully and generate executable programs because there are related environment variables Helps the compiler to find it.

2. Commands related to environment variables

  1. env: Display all environment variables

  2. echo: Displayed variable value (requires $ sign)

  3. export: Set a new environment variable, or promote a local variable to an environment variable.

  4. unset: Clear environment variables

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

3. Introduction to common environment variables in Linux

  • PATH : Specifies the search path for commands
    For example, the ls pwd commands we use in Linux are actually small programs written in C language. Why do we use to run the programs we write? ./ + your own program name, and we run ls pwd never add ./, which is the same as the environment variable PATH It’s about!
    We can use the echo $environment variable command to view environment variables:
    By default, the Linux command we use will go to the PATH path to find the source program, because ls instruction is in the PATH environment variable, so we don’t need to add ./
    We now try to add our path to the PATH environment variable, so that our program does not need to add ./, then we need to use a new command: export
    The export command can promote local variables to environment variables, so we can add our path to PATH and write like this:
 export PATH=$PATH: the path you want to add


Of course, we use the export command to temporarily promote local variables to environment variables. When we exit the cloud server or shut down and restart, the environment variables temporarily promoted by export will be eliminated. Modify We need to modify the corresponding configuration file.

Of course, we can also copy the program we wrote to the default path of Linux's PATH, so we don't need to use ./. In Linux, copy the executable program to the default path of the system. The way we can directly access is equivalent to the installation of software under Linux!

  • HOME: Specify the user's main working directory (that is, the default directory when the user logs in to the Linux system)


    Due to the existence of the HOME environment variable, we use the same command cd ~ but get different results.

  • SHELL : The current Shell, its value is usually /bin/bash.

4. Organization of environment variables and how to obtain environment variables in C code

Inside the Shell, environment variables are actually maintained in the form of an environment variable table!

In addition, environment variables are also global. We know that the parent process of the program we run under bash is bash, so bash can pass its own environment variables to the child process, and in the child process Play a role!

Let's look at a piece of code to verify that the environment variable is global.

1. The C library function getenv() obtains a single environment variable
Before looking at the verification code, let's understand a function getenv() getenv() is a C library function that can get the content of an environment variable
Function prototype:

The parameter of the function is the name of the environment variable, and the return value is a char* string to record the contents of the environment variable. If the call fails, it will return a NULL pointer.

example code

#include<stdio.h>
#include <stdlib.h>
int main()
{<!-- -->
    char* env = getenv("USER");//USER is an environment variable
    if(env == NULL)
    {<!-- -->
        perror("getenv fail:");
    }
    printf("%s\
",env);
    return 0;
}

code output result

The USER variable we added in the code is the environment variable from the test1c process passed to us by the Shell!
2. main() function parameters get environment variables
In addition, we can also use the parameters of the main function to obtain the addresses of all environment variables, through which we can also traverse all environment variables

function prototype

int main(int argc, char *argv[], char *envp[]);

Here we don't talk about the parameters of the function argc *argv[], let's talk about the third parameter! Among them, *envp[] is a character array pointer, pointing to a pointer array, the array name represents the address of the first element, the first element is a character pointer, *envp[]It just happens to be a pointer to the first element, so *envp[] is actually a second-level pointer!

With this in mind, let's look at the following piece of code:

#include<stdio.h>
int main(int argc, int *argv[], int *envp[])
{<!-- -->
    for(int i =0; envp[i] != NULL ; + + i)
    {<!-- -->
       //Print all environment variables, equivalent to the env command!
        printf("envp[%d]-->%s\
", i, envp[i]);
    }
    return 0;
}


You can see that we did print out all the environment variables, and this environment variable comes from its parent process bash.

3. C language global variable environ obtains environment variable

Variable details:


The environ variable is a secondary pointer similar to the char *envp[] of the main() function parameter. Iterating over all environment variables can also be written like this:

#include<stdio.h>
#include <unistd.h> int main()
{<!-- -->
    extern char** environ;
    for(int i =0; environ[i] != NULL; + + i)
    {<!-- -->
        printf("environ[%d]-->%s\
", i, environ[i]);
    }
    return 0;
}

5. How are the environment variables of different users formed

Through the above explanations, we know the concept and function of environment variables. Each of the environment variables has its own purpose: some are for path search, some are for identity authentication, and some are for dynamic library search. Some are used to confirm the current path, etc. Each environment variable has its own specific application scenario.

We also know why, The environment variables are not the same for different users, for example, some of the environment variables of our root user are the same as some of the environment variables of pan, so how is Linux What about forming different environment variables?

Here is the conclusion first: Environment variables are essentially a table at the memory level. When users log in to Huitong, this table forms their own environment variable table for specific users.

In our home directory there are two files called .bashrc .bash_profile In the root directory there is a bashrc file

Open these files to see!


6. Command line parameters of the main() function

In the previous discussion, we talked about the parameters of the main() function. We still have two parameters that we did not talk about int argc char *argv[]. Now let's discuss them!

Since the entire array cannot be passed in the C language, the number of array elements to be obtained in the function must be passed in advance when passing parameters, so int argc is char *argv [] The number of effective elements of the array pointed to by the array pointer, excluding NULL.

This char *argv[] is an array pointer, and the array it points to stores all the pointers of char *, these char * The content pointed to by the pointer needs to be set by using the command line.

Let's look at the following piece of code first:

#include<stdio.h>
int main(int argc, int *argv[])
{<!-- -->
     printf("argc = %d\
",argc);
     for(int i = 0; argv[i] != NULL; + + i)
    {<!-- -->
        printf("argv[%d]-->%s\
", i, argv[i]);
    }
    return 0;
}

operation result:

The ls command in our Linux has many parameters such as -a -l -d -n, ls is essentially a program written in C language. The reason why it can perform different functions according to different parameters is because it uses the command line parameters of the mian() function!

syntaxbug.com © 2021 All Rights Reserved.