[Linux process (6)] Re-understanding of environment variables & initial understanding of program address space

Blogger CSDN homepage:Hangdian Code Farmer-NEO
?
?Column classification:Linux from entry to proficiency?
?
Code Warehouse: NEO’s Learning Diary
?
Follow meTeach you more operating system knowledge
?

Program address space

  • 1 Introduction
  • 2. View all environment variables in bash
  • 3. How to obtain environment variables in the program
  • 4. Analysis of the third parameter of the main function
  • 5. Local and env environment variables
  • 6. Globalness of configuration files and environment variables
  • 7. Built-in commands and regular commands
  • 8. Initial understanding of program address space
  • 9. Deep dive into address space and physical memory
  • 10. Summary

1. Preface

If you don’t know what the environment variable PATH is
Please read the following article first:

First understanding of environment variables

Key points of this chapter:

This article focuses on improving the legacy of the previous article
Some environment variable problems, know a few others
Common environment variables and viewing them under bash
Environment variables and ways to add environment variables, and
Expand the third parameter of the main function: env.
Finally, let’s start with the contents of the program address space!

2. View all environment variables in bash

View all environment variables in bash:

Use directive: env


You can see densely packed environment variables printed out
We don’t need to remember everything, just simple
Just know a few common environment variables!

  1. Environment variable PWD:

Record the current path, what do you think you usually execute?
How does the pwd instruction know the current path?
A little thought will reveal that pwd actually calls
PWD environment variable!

  1. Environment variable HOME

The command we usually use: cd ~enter
Home directory, where is the home directory? Environment variables
The content in HOME is the home directory!

  1. Briefly describe what you need to do when logging into Xshell

Based on the above understanding of environment variables, we found that
When logging in to xshell, we log in with the root account
will run to the root’s home directory. This is because the
When booting Linux, the OS does some things for us

  • Enter username, password

  • Is the authentication information correct?

  • Form environment variables (PATH, PWD, etc.)

  • Initialize HOME environment variable based on username
    HOME=root or HOME=XXX

  • cd $HOME (enter home directory)

3. How to obtain environment variables in the program

We know that Linux is written in C language
So the program here is a C program by default.

Use function: getenv()

How to use it:

char* p = NULL;
p = getenv("PATH");

p can obtain the contents of the environment variable PATH

That is, the parameters of the function are the names of environment variables.
The return value is the content of the environment variable!

4. Analysis of the third parameter of the main function

Don’t sell it, just write it directly:

int main(int argc,char* argv[],char* env[])

The third parameter is env, does it sound familiar?
That’s right, the third parameter is the environment variable

It is an array of pointers like the second parameter
It points to the environment variable table, in the environment variable table
The content is the same as printing using env in bash!

So far we have learned that the system is starting
The program will provide two tables to the main function:

  • Command line parameter list
  • Environment variable table

View the environment variable table through the following code:

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

5. Local and env environment variables

We can define environment variables directly in bash

Usage command: Environment variable name=content

For example:

User-defined environment variables are local
env cannot view local environment variables
So use env to find the environment variable just defined
, it will actually not be found!

If you want the environment variables we defined
What should I do if it is placed in the system’s environment variable table?

Use command: export environment variable name

You can also use export to define environment variables:

6. Globalness of configuration files and environment variables

Based on some of the above information we can deduce:

When modifying/defining environment variables, in fact
What is modified/defined is inside the bash process
Environment variable information will be displayed every time you log in again
Form a new bash interpreter, and newly form
The bash interpreter will automatically start from the home directory
The information is read from the bash_profile file to form
A new environment variable table!

So why every time I restart bash?
Environment variables that we define/modify ourselves
Just disappeared? Because it was not saved in
Configuration file bash_profile in!

View the profile bash_profile:

When we add what we want to the configuration file
When the environment variable has always existed, bash will then
After the first startup, this environment variable will be placed in
in the environment variable table!

and when creating a child process
This table will be passed through the parameters of the main function
to the child process, so the child process has and parent process
The same environment variable table, and the child process creates a grandchild
When the process is running, the table of the child process will be given to the grandchild process.
Therefore, environment variables are global!

Local variables VS environment variables
1. Local variables are only valid within the bash process and will not be inherited by child processes.
2. Environment variables realize their global nature by allowing all child processes to inherit them.

7. Built-in commands and common commands

We know for a fact that the instructions in bash
It can be used directly without adding ./ because of its
The path is in the environment variable PATH, so we
After setting PATH to blank, these commands cannot be run!

We will find a problem, after PATH is left blank
It is true that some commands cannot be run. But some
The command can still be run, for example, the pwd command can still run normally.
Output, why is this?

Parent category of commands under Linux:

  • General commands: bash creates subprocesses for execution
  • Built-in command: executed by bash itself (similar to a function)

Obviously pwd is a built-in command, and so is echo.
Don’t you have any doubts about built-in commands?

Why do we define environment variables in
Cannot be found in the environment variable table env, but echo
But the command can print out its contents?
The echo program is also a process of bash
Why can it get the environment variables we defined?
The answer is that echo is a built-in command and the parent process
Executed internally, so it can see
The parent process changes, but other child processes cannot
Accept local variables of the parent process, so they are invisible!

8. First understanding of program address space

C/C++ programmers believe that the memory distribution of the program is like this:

The heap grows upward and the stack grows downward.
We have learned about this in previous studies
I understand, so why do I need to mention it here?

has two purposes:

  1. Review previous knowledge
  2. Expand on the basis of original knowledge

There are two things to note:

Constant string defined when writing C program
In fact, in the text code area of the above area
Because it is next to the text code area, so
The strings in the constant area are not allowed to be modified!
The second point is that although the stack is said to grow downward
But arrays, structures and other structures in the stack
The address grows upward, for example, opening an array
When, ten spaces are opened, then the first one in the array
The element is at the bottom of the space, that is, the address is the lowest
, and then put the following elements up one by one

9. In-depth exploration of address space and physical memory

Now write this code to explore the address in depth:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include<stdlib.h>
int main()
{<!-- -->
    int id=fork();
    int tmp=10;
    if(id==0)//Code executed by the child process
    {<!-- -->
        tmp=20;
        while(1)
        {<!-- -->
            printf("Subprocess,tmp: %d, & amp;tmp: %p\\
",tmp, & amp;tmp);
            sleep(1);
        }
    }
    if(id>1)//Code executed by the parent process
    {<!-- -->
        while(1)
        {<!-- -->
            printf("parent process,tmp: %d, & amp;tmp: %p\\
",tmp, & amp;tmp);
            sleep(1);
        }
    }
    return 0;
}
</code><img class="look-more-preCode contentImg-no-view" src="//i2.wp.com/csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreBlack. png" alt="" title="">

We found that the value of tmp is in the parent and child processes
is different, we can understand it for the time being,
It’s because copy-on-write, but the same address
Why are there different values?
This is a point
Challenge the knowledge we learned before!

It is impossible to figure out this problem with the current knowledge base
But we can draw a small conclusion:

The heap area, stack area and static area we usually write
The waiting area is not the real physical address!
Because a physical address cannot have two values
So what is the real physical address?
If this is not a real physical address then
What is this address? Let’s print it
What is the address that comes out? The above content
Will explain in the next article!

10. Summary

Understanding environment variables can help you better
Understand what the operating system does at startup,
And can better understand the process running
Where does the data we need come from?

This article will no longer be expanded, more content
Will explain in next article

Next issue preview: Program address space