linux进程的环境变量,LINUX系统学习一(进程、MMU,环境变量、getenv、fork、getpid/ge...
LINUX系統學習一(進程、MMU,環境變量、getenv、fork、getpid/ge
LINUX系統學習一(進程、MMU,環境變量、getenv、fork、getpid/getppid、ps、kill)
1、進程的概念
編譯好的二進制文件叫程序
進行是運行起來的程序
站在程序員的角度:運行一系列指令的過程
站在操作系統角度:分配系統資源的基本單位
區別:
程序占用磁盤,不占用系統資源
進程占用系統資源
一個程序對應多個進程,可以同時運行多個,一個進程對應一個程序
程序沒有生命周期,進程有生命周期
2、進程的過程
3、進程的狀態
4、MMU的作用
1、虛擬內存和物理內存的映射
2、修改內存訪問級別
3、用戶空間映射到物理內在是獨立的
5、進程控制塊PCB
[email?protected]:~# grep -rn "struct task_struct {" /usr/ 查找進程控制塊代碼
/usr/share/systemtap/tapset/linux/signal.stp:837:// struct task_struct {
/usr/share/systemtap/tapset/linux/signal.stp:876:// struct task_struct {
/usr/src/linux-headers-4.4.0-148/include/linux/sched.h:1391:struct task_struct {
/usr/src/linux-headers-4.4.0-31/include/linux/sched.h:1380:struct task_struct {
/usr/src/linux-headers-4.4.0-93/include/linux/sched.h:1380:struct task_struct {
[email?protected]:/usr/src/linux-headers-4.4.0-31/include/linux# pwd
/usr/src/linux-headers-4.4.0-31/include/linux
[email?protected]:/usr/src/linux-headers-4.4.0-31/include/linux# ls sched.h
sched.h
6、環境變量
查看所有環境變量
[email?protected]:/usr/src/linux-headers-4.4.0-31/include/linux# env
查看某個環境變量
[email?protected]:~# echo $PATH
/usr/java/jdk1.8.0_181/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
PATH可執行文件搜索路徑
SHELL當前使用的shell,通常是/bin/bash
TERM當前終端類型,圖形界面終端下的值通常是xterm
LANG語言與locale,決定了字符編碼以及時間,貨幣等信息的顯示式
HOME當前用戶主目標錄的路徑
7、getenv獲取環境變量
char *getenv(const char *name);
[[email?protected]_0_5_centos test3]# more getenv.c
#include
#include
int main()
{
printf("homepath is [%s]\n",getenv("HOME"));
return 0;
}
[[email?protected]_0_5_centos test3]# gcc ?getenv.c
[[email?protected]_0_5_centos test3]# ./a.out
homepath is [/root]
8、fork創建一個新的進程
pid_t fork(void);
返回值:
失敗:-1
成功,兩次返回
父進程返回子進程的id
子進程返回0
9、getpid和getppid
獲得pid,進程id,獲得當前進程的
pid_t getpid(void);
獲得當前進程的父進程的id
pid_t getppid(void);
代碼示例
[[email?protected]_0_5_centos test3]# more fork.c
#include
#include
#include
int main(){
printf("begin...\n");
pid_t pid =fork();
if(pid<0){
perror("fork err");
exit(1);
}
if(pid==0){
//子進程
printf("I am child,pid=%d,ppid=%d\n",getpid(),getppid());
}
else if(pid>0){
//父進程
printf("childpid=%d,self=%d,ppid=%d\n",pid,getpid(),getppid());
sleep(1);
}
printf("end...\n");
return 0;
}
[[email?protected]_0_5_centos test3]# gcc fork.c
[[email?protected]_0_5_centos test3]# ./a.out
begin...
childpid=19741,self=19740,ppid=18132
I am child,pid=19741,ppid=19740
end...
end...
10、ps ?aux查看進程 ps ?ajx還能查到父進程
[[email?protected]_0_5_centos test3]# ps aux
USER ??????PID %CPU %MEM ???VSZ ??RSS TTY ?????STAT START ??TIME COMMAND
root ????????1 ?0.0 ?0.0 ?41096 ?3412 ? ???????Ss ???2019 ?32:06 /usr/lib/system
root ????????2 ?0.0 ?0.0 ?????0 ????0 ? ???????S ????2019 ??0:01 [kthreadd]
root ????????3 ?0.0 ?0.0 ?????0 ????0 ? ???????S ????2019 ??0:26 [ksoftirqd/0]
root ????????5 ?0.0 ?0.0 ?????0 ????0 ? ???????S< ???2019 ??0:00 [kworker/0:0H]
root ????????7 ?0.0 ?0.0 ?????0 ????0 ? ???????S ????2019 ??1:57 [migration/0]
[[email?protected]_0_5_centos test3]# ps ajx ??---可以追述進程之間的血緣關系
PPID ??PID ?PGID ??SID TTY ?????TPGID STAT ??UID ??TIME COMMAND
0 ????1 ????1 ????1 ? ??????????-1 Ss ??????0 ?32:06 /usr/lib/systemd/system
0 ????2 ????0 ????0 ? ??????????-1 S ???????0 ??0:01 [kthreadd]
2 ????3 ????0 ????0 ? ??????????-1 S ???????0 ??0:26 [ksoftirqd/0]
2 ????5 ????0 ????0 ? ??????????-1 S< ??????0 ??0:00 [kworker/0:0H]
2 ????7 ????0 ????0 ? ??????????-1 S ???????0 ??1:57 [migration/0]
11、kill 進程ID 殺進程
[[email?protected]_0_5_centos test3]#kill ?2898
12、創建n個子進程
父進程創建5個子進程示例
[[email?protected]_0_5_centos test3]# more fork_five.c
#include
#include
#include
int main(){
int i=0;
pid_t pid=0;
for(i=0;i<5;i++){ //父進程結束循環
pid=fork();
if(pid==0){ //子進程
printf("I am child,pid=%d,ppid=%d\n",getpid(),getppid());
break;//子進程退出循環
}
else if(pid>0){//父進程
printf("I am fatcher,pid=%d,ppid=%d\n",getpid(),getppid(
));
sleep(1);
}
}
sleep(i);
//If(i<5){
//printf("I am child,pid=%d,ppid=%d\n",getpid(),getppid());
//}else{
// printf("I am fatcher,pid=%d,ppid=%d\n",getpid(),getppid());
//}
while(1){
sleep(1);
}
return 0;
}
[[email?protected]_0_5_centos test3]# gcc fork_five.c
[[email?protected]_0_5_centos test3]# ./a.out
I am fatcher,pid=23907,ppid=18132
I am child,pid=23908,ppid=23907
I am fatcher,pid=23907,ppid=18132
I am child,pid=23917,ppid=23907
I am fatcher,pid=23907,ppid=18132
I am child,pid=23923,ppid=23907
I am fatcher,pid=23907,ppid=18132
I am child,pid=23924,ppid=23907
I am fatcher,pid=23907,ppid=18132
I am child,pid=23926,ppid=23907
[[email?protected]_0_5_centos ~]# ps ajx|grep a.out
18132 23907 23907 18132 pts/2 ???23907 S+ ??????0 ??0:00 ./a.out
23907 23908 23907 18132 pts/2 ???23907 S+ ??????0 ??0:00 ./a.out
23907 23917 23907 18132 pts/2 ???23907 S+ ??????0 ??0:00 ./a.out
23907 23923 23907 18132 pts/2 ???23907 S+ ??????0 ??0:00 ./a.out
23907 23924 23907 18132 pts/2 ???23907 S+ ??????0 ??0:00 ./a.out
23907 23926 23907 18132 pts/2 ???23907 S+ ??????0 ??0:00 ./a.out
23943 24045 24044 23943 pts/0 ???24044 R+ ??????0 ??0:00 grep --color=auto a.out
LINUX系統學習一(進程、MMU,環境變量、getenv、fork、getpid/ge相關教程
總結
以上是生活随笔為你收集整理的linux进程的环境变量,LINUX系统学习一(进程、MMU,环境变量、getenv、fork、getpid/ge...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux ie 插件目录在哪个文件夹里
- 下一篇: linux 其他常用命令