Article directory
- 1. Process priority
- 2. Environment variables
-
- basic concept
- View environment variable method
- common environment variables
- test-PATH
- Commands related to environment variables
- How environment variables are organized
- How to get environment variables through code
- Get or set environment variables through system calls
- Environment variables usually have global properties
- 3. Program address space
1. Process priority
basic concept
The order of cpu resource allocation refers to the priority of the process.
Processes with higher priority have priority execution rights. Configuring process priorities is very useful for Linux in a multi-tasking environment and can improve system performance.
You can also run processes on designated CPUs. In this way, unimportant processes can be assigned to a certain CPU, which can greatly improve the overall performance of the system.
View system processes
In a linux or unix system, using the ps -l command will output something similar to the following:
We can easily notice several important information, including the following:
-
UID: represents the identity of the executor
-
PID: represents the codename 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 at which this process can be executed. The smaller the value, the earlier it will be executed.
-
NI: represents the nice value of this process
PRI and NI
PRI is also relatively easy to understand, that is, the priority of the process, or in layman's terms, the order in which programs are executed by the CPU. The smaller the value, the higher the priority of the process.
What about NI? It is the nice value we want to talk about, which means that the modified value PRI of the priority of the process that can be executed is smaller and faster to be executed. After adding the nice value, the PRI will become: PRI(new )=PRI(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, so adjust the process Priority, under Linux, is to adjust the nice value of the process. The value range of nice is -20 to 19, a total of 40 levels
PRI vs NI
It needs to be emphasized that the nice value of a process is not the priority of the process. They are not a concept, but the nice value of the process will affect the priority change of the process.
It can be understood that the nice value is the correction data of the process priority.
Command to modify process priority
Use the top command to change the niceness of an existing process:
(1)top
(2) Press “r” after entering top –> enter the process PID –> enter the nice value
other concepts
-
Competition: There are many system processes, but there are only a small number of CPU resources, or even one, so there is competition between 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 multi-process operations do not interfere with each other.
-
Parallelism: Multiple processes run on multiple CPUs at the same time. This is called parallelism.
-
Concurrency: Multiple processes use process switching under one CPU to advance multiple processes within a period of time, which is called concurrency.
2. Environment variables
basic concept
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 write C/C++ code, when we link, we never know the dynamic and static state of our link. Where is the library, but it can still be linked successfully to generate an executable program, because there are relevant environment variables to help the compiler find it.Environment variables usually have some special purpose, and they usually have global properties in the system.
View environment variable method
echo $NAME //NAME:你的环境变量名称
common environment variables
PATH: Specify the search path of the command
HOME: Specify the user's main working directory (that is, the default directory when the user logs in to the Linux system)
SHELL: The current Shell, its value is usually /bin/bash.
test-PATH
- Create hello.c file
- Compare ./hello execution and hello execution between
It is obvious that when executing the hello program, it cannot run without adding ./
Why some instructions can be executed directly without a path, but our binary program needs a path to execute?
This is because in the operating system, the system has configured some default environment variables and paths, so that some commonly used instructions can be directly executed without specifying a complete path. These instructions are usually core functions or common tools provided by the operating system, such as
ls
,cd
,mkdir
and so on.For binary programs, they are not in the default path, so the full path needs to be provided to execute. If you want a binary program to be executed in any location, you can add its path to the environment variable of the system, so that you can directly use the program name to execute.
In addition, if there is an executable file in the directory you are currently in, you can also use a relative path to execute it, for example
./program
, where it./
indicates the current directory. However, relative paths are only applicable to files in the current directory. If you want to execute files in other directories, you still need to provide a complete path.Add the path of our program to the environment variable PATH, export PATH=$PATH: the path of the hello program
comparison test
Is there any other way to run it directly without adding a path?
If we use root privileges to copy the program we wrote to the /bin directory, then we can directly execute the program we wrote. This method is generally not very ideal, so I will not demonstrate it here.
Commands related to environment variables
echo: display the value of an environment variable
export: Set a new environment variable
env: displays all environment variables
unset: clear environment variables
set: display locally defined shell variables and environment variables
How environment variables are organized
How to get environment variables through code
- The third parameter of the command line
Obtained through the third-party variable environ
The global variable environ defined in libc points to the environment variable table, and environ is not included in any header file, so it must be declared with extern when using it.
Get or set environment variables through system calls
Getenv is commonly used to access specific environment variables
Environment variables usually have global properties
Environment variables usually have global attributes and can be inherited by child processes
(1) Add environment variable MYENV=1
(2) Create a subprocess to verify whether MYENV exists
If we only execute MYENV="helloworld" without calling export, what will be the result if we use our program to view it? Why?
Without export, it is a normal variable and cannot be inherited by the child process.
The plus
export
expression is a global variable, which is not only valid for the current shell but also for sub-processes. Otherwise, it is a local variable, which is only valid for the current shell and has no effect on sub-processes.
3. Program address space
Research Background
Linux centos7 3.10.0-1160.90.1.el7.x86_64
Program Address Space Review
The program address space diagram below must be the space layout diagram that most people have come into contact with when they first started learning C language - we didn't understand it at that time.
Test it from a code perspective
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int g_val = 0; int main(){ pid_t id = fork(); if(id < 0){ perror("fork"); return 0; }else if(id == 0){ //子进程 printf("child[%d]: %d : %p\n", getpid(), g_val, &g_val); }else{ //父进程 printf("parent[%d]: %d : %p\n", getpid(), g_val, &g_val); } return 0; }
The output results are related to the environment, just observe the phenomenon
We found that the output variable values and addresses are exactly the same, which is easy to understand, because the child process follows the parent process as the template, and the father and son did not make any modifications to the variables. But change the code slightly
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int g_val = 0; int main(){ pid_t id = fork(); if(id < 0){ perror("fork"); return 0; }else if(id == 0){ //子进程 g_val=100; //现在这个场景子进程肯定先跑完,也就是子进程先修改,完成之后,父进程再读取 printf("child[%d]: %d : %p\n", getpid(), g_val, &g_val); }else{ //父进程 printf("parent[%d]: %d : %p\n", getpid(), g_val, &g_val); } return 0; }
We found that the output addresses of the father and son processes are consistent, but the variable contents are different! The following conclusions can be drawn
- The contents of the variables are different, so the variables output by the parent and child processes are definitely not the same variable.
- The address values are the same, which means that the address is definitely not a physical address!
- Under Linux address, this kind of address is called virtual address
- So the addresses we see in C/C++ language are all virtual addresses! The physical address cannot be seen by the user. It is managed by the OS. The OS must be responsible for converting the virtual address into a physical address.
process address space
So it is not accurate to say 'the address space of the program' before. It should be accurately said to be the process address space. So how should we understand it?
Explanation: The same variable with the same address is actually the same virtual address, but different contents are actually mapped to different physical addresses.