日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux进程的环境变量,LINUX系统学习一(进程、MMU,环境变量、getenv、fork、getpid/ge...

發布時間:2025/3/20 linux 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。