Introduction to Linux environment variables and process address space

Introduction to Linux environment variables and process address space

Linux environment variables and process address space

//View the process pid and parent process [dy@VM-12-10-centos jincheng_12_5]$ ps ajx | head -1 && ps axj | grep 3669470
   PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
3669469 3669470 3669470 3669439 pts/0 3681645 S 1001 0:00 -bash
3669470 3681645 3681645 3669439 pts/0 3681645 R+ 1001 0:00 ps axj
3669470 3681646 3681645 3669439 pts/0 3681645 R+ 1001 0:00 grep --color=auto 3669470


//The first method to run the program without adding a path [dy@VM-12-10-centos jincheng_12_5]$ ll
total 28
-rw-rw-r-- 1 dy dy 73 Dec 5 16:46 Makefile
-rwxrwxr-x 1 dy dy 17432 Dec 5 16:47 proc
-rw-rw-r-- 1 dy dy 75 Dec 5 16:47 proc.c
[dy@VM-12-10-centos jincheng_12_5]$ sudo cp proc /usr/bin/                  
//Add your own executable program to the system path [sudo] password for dy: 
[dy@VM-12-10-centos jincheng_12_5]$ proc //Add your own executable program to the system path, and you don't need to add the ./ path. Hello world!
[dy@VM-12-10-centos jincheng_12_5]$ ./proc
hello world!
[dy@VM-12-10-centos jincheng_12_5]$ sudo rm /usr/bin/proc //Delete from the system path [dy@VM-12-10-centos jincheng_12_5]$ proc
-bash: /usr/bin/proc: No such file or directory

//The second type [dy@VM-12-10-centos jincheng_12_5]$ $PATH
-bash: /home/dy/.local/bin:/home/dy/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin: No such file or directory
[dy@VM-12-10-centos jincheng_12_5]$ pwd
/home/dy/jincheng_12_5
//Add the current path to the environment variable PATH [dy@VM-12-10-centos jincheng_12_5]$ PATH=$PATH:/home/dy/jincheng_12_5
[dy@VM-12-10-centos jincheng_12_5]$ proc
hello world!

//Only save 3000 commands [dy@VM-12-10-centos jincheng_12_5]$ echo $HISTSIZE
3000
//Show how many commands were run before [dy@VM-12-10-centos jincheng_12_5]$ history |wc -l
636
[dy@VM-12-10-centos jincheng_12_5]$ history
// Output all previous commands to the display // Display all environment variables [dy@VM-12-10-centos jincheng_12_5]$ env
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.m4a=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.oga=01;36:*.opus=01;36:*.spx=01;36:*.xspf=01;36:
LANG=en_US.utf8
HISTTIMEFORMAT=%F %T 
HOSTNAME=VM-12-10-centos
OLDPWD=/home/dy
USER=dy
PWD=/home/dy/jincheng_12_5
HOME=/home/dy
MAIL=/var/spool/mail/dy
SHELL=/bin/bash
TERM=xterm
SHLVL=1
PROMPT_COMMAND=history -a; history -a; printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}"
LOGNAME=dy
PATH=/home/dy/.local/bin:/home/dy/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/dy/jincheng_12_5
HISTSIZE=3000
LESSOPEN=||/usr/bin/lesspipe.sh %s
_=/usr/bin/env


echo: Display the value of an environment variable

export: Set a new environment variable

[dy@VM-12-10-centos jincheng_12_5]$ ./proc //Local variables do not exist I am a proc : pid:3700079 ppid:3669470
Segmentation fault (core dumped)
[dy@VM-12-10-centos jincheng_12_5]$ export MY_VAL
[dy@VM-12-10-centos jincheng_12_5]$ ./proc
I am a proc : pid:3700163 ppid:3669470
hello dy
/home/dy/.local/bin:/home/dy/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/dy/jincheng_12_5
/home/dy
/bin/bash

env: Display all environment variables

unset: clear environment variables

env environment variable

set: Display locally defined shell variables and environment variables

//Set environment variables [dy@VM-12-10-centos jincheng_12_5]$ hello=1234
//Remember to add $ to output environment variables [dy@VM-12-10-centos jincheng_12_5]$ echo $hello
1234
[dy@VM-12-10-centos jincheng_12_5]$ set | grep hello
hello=1234

Most of the commands run on the command line have bash as their parent process. bash creates child processes that execute your commands.

Get environment variables through code

#include <stdio.h>
#include<stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[], char *env[])
{
        for(int i = 0; argv[i]; ++i)//Interpret the command line parameters//for(int i = 0; i < argc; ++i)
        {
            printf("argv[%d]:%s\n",i,argv[i]);
        }
        for(int i = 0; env[i]; ++i) //You can get the environment variable through the code {
           printf("env[%d]:%s\n",i,env[i]);
        }
   return 0;
}

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-7v2pxCt8-1639035758639)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20211208141656121.png)]

int x = atoi(argv[2]);
int y = atoi(argv[3]);
if (strcmp(argv[1],"-a") == 0)
{
    printf("%d + %d = %d\n",x,y,x+y);
}
else
{
    printf("%d - %d = %d\n",x,y,xy);
}

insert image description here

Each program receives an environment table, which is an array of character pointers, each of which points to an environment string ending with '\0'.

insert image description here

You can also get the environment through the third-party variable environ

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

insert image description here

Process address space

#include <stdio.h>
#include <unistd.h>
int g_val = 0;
int main()
{
   pid_t id = fork();
   if(id < 0)
   {
       perror("fork() fail\n");
   }
   else if(id == 0)
   {
       printf("I am a child:%d,%d,%p\n",getpid(),g_val,&g_val);
   }else
   {
       printf("I am a fatcher: %d, %d, %p\n",getppid(),g_val,&g_val);
   }
   return 0;
}

insert image description here

From the running results, we can see that the values ​​and addresses of the variables of the two processes are the same. No changes are made to the code here. The child process uses the parent process as a template, so the addresses are the same.

Let's change the code slightly

#include <stdio.h>
#include <unistd.h>
int g_val;
int main()
{
   pid_t id = fork();
   if(id < 0)
   {
       perror("fork() fail");
   }
   else if(id == 0)
   {
       g_val = 100;
       printf("I am a child:%d,%d,%p\n",getpid(),g_val,&g_val);
   }else
   {
       sleep(3);
       printf("I am a fatcher: %d, %d, %p\n",getppid(),g_val,&g_val);
   }
   return 0;
}

insert image description here

At this point we find that the value of the variable has changed, the parent process is 0, the child process is 100, but the address of the variable is still the same.

From this running result, we can know the following points.

The values ​​of the variables of the two processes are different, so the variables of the parent and child processes are definitely not the same variable address value, and definitely not the physical address! ! ! All the addresses we see are virtual addresses! ! ! The real physical address is invisible to the user and is managed by the OS (OS is responsible for converting virtual address to physical address). The code is shared, but each has its own private copy of the data (copy-on-write).

Why is it a virtual address, not a physical address?

What is process address space?

Process address space: It is a way of looking at memory, an abstract concept, kernel struct mm struct, so each process thinks that it has exclusive system memory resources (you think your father's property belongs to you alone) Region division: The address space is linear and is divided into one region after another, [start, end] Virtual address: Each address between [start, end] is called a virtual address

insert image description here

A mapping of virtual addresses to physical addresses

insert image description here

Why does process address space exist?

Protect physical memory and prevent direct access to addresses within any process, making it easier to verify legitimacy. Decouple memory management from process management so that each process can view code and data in the same way.

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-wu0C4V57-1639035758649) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20211209150456785.png)]

From now on we can directly tell the difference between process and program.

insert image description here

This is the end of this article about Linux environment variables and process address space. For more relevant Linux environment variables and process space content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to configure environment variables in Linux environment
  • A brief introduction to Linux environment variable files
  • Linux operation and maintenance basic process management real-time monitoring and control

<<:  HTML 5.1 learning: 14 new features and application examples

>>:  Will CSS3 really replace SCSS?

Recommend

React Fiber structure creation steps

Table of contents React Fiber Creation 1. Before ...

CSS to implement sprites and font icons

Sprites: In the past, each image resource was an ...

Pure CSS to achieve the effect of picture blinds display example

First, let me show you the finished effect Main i...

MySQL multi-table join query example explanation

In actual projects, there are relationships betwe...

Solution to "Specialized key was too long" in MySQL

Table of contents Solution 1 Solution 2 When crea...

Mysql table creation foreign key error solution

Database Table A: CREATE TABLE task_desc_tab ( id...

How to install JDK 13 in Linux environment using compressed package

What is JDK? Well, if you don't know this que...

WeChat applet implements SMS login in action

Table of contents 1. Interface effect preview 2.u...

Solution to the problem that the image name is none after Docker load

Recently, I found that after using the docker loa...

5 ways to determine whether an object is an empty object in JS

1. Convert the json object into a json string, an...

Common pitfalls of using React Hooks

React Hooks is a new feature introduced in React ...

HTML is something that web page creators must learn and master.

What are the benefits of learning HTML? 1: Easily...

Common structural tags in XHTML

structure body, head, html, title text abbr, acro...