Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze Linux environment variables and process address space

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to analyze Linux environment variables and process address space, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

129.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 Hello world! [dy@VM-12-10-centos jincheng_12_5] $. / prochello world! [dy@VM-12-10-centos jincheng_12_5] $sudo rm / usr/bin/proc / / delete [dy@VM-12-10-centos jincheng_12_5] $proc-bash: / usr/bin/proc: No such file or directory// the second [dy@VM-12-10-centos] from the system path. 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// adds 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] $prochello Worldwide cosmopolitan / only save 3000 commands [dy@VM-12-10-centos jincheng_12_5] $echo $HISTSIZE3000// shows how many previous commands [dy@VM-12-10-centos jincheng_12_5] $history | wc-l636 [dy @ VM-12-10-centos jincheng_12_5] $history// all previous commands Output to the monitor / / display all environment variables [dy@VM-12-10-centos jincheng_12_5] $envLS_COLORS=rs=0:di=01 31portions. Txzboxes 01holders. Tzoographies 01holders. T7zboxes 01pairs. Zipsets 01th 31position. Zipsets 01th 31position. Dzboxes 01th 31sides. Gzboxes 01tens 31position. Lrzboxes 01th 31ups. Lzboxes 01holders 31monds. Lzstones 01holders. 31ambiguous, earring, 01, 31, riveting. Sartorium01lead.raraum01.alzy01.alzy01exiting 31purve.zooshi01it.cpioshi03bluff.7zposition01lead.rzpon01lead.cabbage 01betting 31purposes.wm01ut31bluff.swm01ut31purposes.dwm01b01blub .esd01move.jpg0135uuu.jpeg01uuuuuuu.mjpg01uuuuuuuuu.mjpeg01uuuuuuuu.mjpp01uuuuuuuuuuuuuu.mp01uuuuuuuu.pm01uf01uuuuuuuuuu.pm01uuf01uuuuuuuuuuuuu.pm01uuuuuuuuu.pm01uf01uuuuuuuuuuan.jpm01uuuuuuuuan.jpm01uanuan.mpm01uf01uf01uuuuuuuuuu.wm01uuuuuan.jppg01uf01uuuuuuan.mpeg01uf01uf01uuuuuuuuuan.wmf01uuuuuuuuuuuuuo. 35 PUBMY01Trache.tgaO1TGAY01True 35PUBMATE .xbmY01TEROTY .xpmLING 01it.TIFFLATING .tiff.TIFO1TOR .pngLING 01it35 position .svgZOLEX 35 purveyors .svgZOTING 01Trent35 purposes.mngex01X 35 purposes.pcx01ut35purposes.mov011035purposes.mg01position.mpegposite35purposes.m2v01leadership .mkv01sets .mkv01sets 35 machines. OGM035 sets .4m0351 sets .4v035sets. 4v03buds 350s. Rmvfang 01it.rmvfang 01it.rmvail01it.rmvbail.flcwort 01flux.flcwort 01flux.35flipping01flipping.flv01flipping.flv01bot 35fre.dlt01ut.xcf01bot 35fre.xwd01bot 35Mod .xcff01bot 35th. YUVO 035MOS .cgmmlv 035WF .emf010bot. MKANGO1TIMEFORMAT% F% T HOSTNAME=VM-12-10-centosOLDPWD=/home/dyUSER=dyPWD=/home/dy/jincheng_12_5HOME=/home/dyMAIL=/var/spool/mail/dySHELL=/bin/bashTERM=xtermSHLVL=1PROMPT_COMMAND=history a History-a; printf "\ 033] 0 s_=/usr/bin/env% s% s\ 007" ${USER} "" ${HOSTNAME%%.*} "" ${PWD/#$HOME/\ ~} "LOGNAME=dyPATH=/home/dy/.local/bin:/home/dy/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/dy/jincheng_12_5HISTSIZE=3000LESSOPEN= | | / usr/bin/lesspipe.sh% s_=/usr/bin/env

Echo: displays the value of an environment variable

Export: set a new environment variable

[dy@VM-12-10-centos jincheng_12_5] $. / proc / / Local variable does not have I am a proc: pid:3700079 ppid:3669470Segmentation fault (core dumped) [dy@VM-12-10-centos jincheng_12_5] $export MY_ Val [dy @ VM-12-10-centos jincheng_12_5] $. / procI am a proc: pid:3700163 ppid:3669470hello 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: show all environment variables

Unset: clearing environment variabl

Env environment variable

Set: displays locally defined shell variables and environment variables

/ / set the environment variable [dy@VM-12-10-centos jincheng_12_5] $hello=1234// output environment variable remember to add the $symbol [dy@VM-12-10-centos jincheng_12_5] $echo $hello1234 [dy @ VM-12-10-centos jincheng_12_5] $set | grep hellohello=1234

For most of the instructions that run on the command line, the parent process is the bash,bash creation child process, which executes your commands.

Get the environment variable # include#include#includeint main (int argc, char * argv [], char * env []) {for (int I = 0; argv [I]; + + I) / / interpret the command line arguments / / for (int I = 0; I) through the code

< argc; ++i) { printf("argv[%d]:%s\n",i,argv[i]); } for(int i = 0; env[i]; ++i)//可以通过代码获取环境变量 { printf("env[%d]:%s\n",i,env[i]); } return 0;}

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

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

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

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

Process address space # include#includeint g_val = 0poliint 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 (), gambivaljinghuval) } else {printf ("I am a fatcher:%d,%d,%p\ n", getppid (), gongqivaljinghuval);} return 0;}

By running the results, we see that the values and addresses of the variables of the two processes are the same, no changes have been made to the code, and the child process is modeled according to the parent process, so the address is the same.

Let's change the code a little bit

# include#includeint gallevaltint 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 (), gallevaljinthecommutal);} else {sleep (3) Printf ("I am a fatcher:%d,%d,%p\ n", getppid (), return return 0;}

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

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

The value of the variable of the two processes is different, so the variable of the parent and child process is definitely not the same address value of the same variable, absolutely not the physical address! The addresses we see are all virtual addresses! The real physical address, which is invisible to the user, is managed by OS (OS is responsible for sharing the virtual address-"physical address) code, while the data is privately owned (copied when writing) about why it is a virtual address, not a physical address?"

What is the process address space?

Process address space: is the way to look at memory, an abstract concept, kernel struct mm struct, such that each process thinks it has exclusive access to system memory resources (you think your father's property is your own) area division: the address space is linear and is divided into one area, [start,end] virtual address: the addresses between [start,end] are called virtual addresses.

A mapping from virtual address to physical address

Why does the process address space exist?

Protect physical memory from direct access to any in-process addresses, facilitate legitimacy verification, decouple memory management from process management and allow each process to look at code and data in the same way

In the future, we can directly tell the difference between the process and the program.

After reading the above, have you mastered how to analyze Linux environment variables and process address space? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report