目录1.进程状态1.R状态2.S状态3.T/t状态4.D状态5.X死亡状态6.Z僵尸状态/进程如何模拟验证Z状态孤儿进程1.进程状态1.R状态运行状态如果要改成运行状态不要让其io[user1iZ5waahoxw3q2bZ 26-4-22]$ cat myprocess.c #includestdio.h int main() { while(1) { } return 0; }[user1iZ5waahoxw3q2bZ ~]$ while :; do ps ajx | head -1 ; ps ajx | grep myprocess; sleep 1; done PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 20928 21157 21157 20928 pts/0 21157 R 1001 0:04 ./myprocess 20979 21162 21161 20979 pts/1 21161 R 1001 0:00 grep --colorauto myprocess将当前myprocess放在后台运行[user1iZ5waahoxw3q2bZ 26-4-22]$ ./myprocess [1] 21277[user1iZ5waahoxw3q2bZ ~]$ while :; do ps ajx | head -1 ; ps ajx | grep myprocess; sleep 1; done PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 20928 21277 21277 20928 pts/0 20928 R 1001 0:03 ./myprocess 20979 21281 21280 20979 pts/1 21280 R 1001 0:00 grep --colorauto myprocess删除[user1iZ5waahoxw3q2bZ 26-4-22]$ kill -9 21277 [1] Killed ./myprocess2.S状态阻塞状态[user1iZ5waahoxw3q2bZ 26-4-22]$ cat myprocess.c #includestdio.h #includesys/types.h #includeunistd.h int main() { printf(我是一个进程pid:%d\n,getpid()); int x; scanf(%d,x); return 0; } [user1iZ5waahoxw3q2bZ 26-4-22]$ ./myprocess 我是一个进程pid:21610[user1iZ5waahoxw3q2bZ ~]$ while :; do ps ajx | head -1 ; ps ajx | grep myprocess; sleep 1; done PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 20979 21413 21412 20979 pts/1 21412 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 20928 21445 21445 20928 pts/0 21445 S 1001 0:00 ./myprocess 20979 21449 21448 20979 pts/1 21448 S 1001 0:00 grep --colorauto myprocess之前不是讲进程状态就是一个整数吗那么R跟S是几呢R是0S是13.T/t状态t状态 追踪状态[user1iZ5waahoxw3q2bZ 26-4-22]$ gcc myprocess.c -o myprocess -g [user1iZ5waahoxw3q2bZ 26-4-22]$ gdb myprocess GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type show copying and show warranty for details. This GDB was configured as x86_64-redhat-linux-gnu. For bug reporting instructions, please see: http://www.gnu.org/software/gdb/bugs/... Reading symbols from /home/user1/linux-learning/linux/26-4-22/myprocess...done. (gdb) l 1 #includestdio.h 2 #includesys/types.h 3 #includeunistd.h 4 5 int main() 6 { 7 printf(我是一个进程pid:%d\n,getpid()); 8 9 int x; 10 scanf(%d,x); (gdb) l 11 //while(1) 12 //{ 13 //printf(hello world!\n); 14 //} 15 return 0; 16 } (gdb) b 9 Breakpoint 1 at 0x4005eb: file myprocess.c, line 9. (gdb) r Starting program: /home/user1/linux-learning/linux/26-4-22/myprocess 我是一个进程pid:22154 Breakpoint 1, main () at myprocess.c:10 10 scanf(%d,x); Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_9.3.x86_64[user1iZ5waahoxw3q2bZ ~]$ while :; do ps ajx | head -1 ; ps ajx | grep myprocess; sleep 1; done 20979 22236 22235 20979 pts/1 22235 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 20928 22077 22077 20928 pts/0 22077 S 1001 0:00 gdb myprocess 22077 22154 22154 20928 pts/0 22077 t 1001 0:00 /home/user1/linux-learning/linux/26-4-22/myprocess被debug断点进程被暂停了T状态 暂停状态ctrlz[user1iZ5waahoxw3q2bZ 26-4-22]$ cat myprocess.c #includestdio.h #includesys/types.h #includeunistd.h int main() { printf(我是一个进程pid:%d\n,getpid()); //int x; //scanf(%d,x); while(1) { printf(hello world!\n); } return 0; }[user1iZ5waahoxw3q2bZ 26-4-22]$ gcc myprocess.c -o myprocess -g [user1iZ5waahoxw3q2bZ 26-4-22]$ ./myprocess hello world! hello world! hello world! ^Zhello world! hello world! hello world! [1] Stopped ./myprocess[user1iZ5waahoxw3q2bZ ~]$ while :; do ps ajx | head -1 ; ps ajx | grep myprocess; sleep 1; done PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 20928 22513 22513 20928 pts/0 20928 T 1001 0:00 ./myprocess 20979 22530 22529 20979 pts/1 22529 S 1001 0:00 grep --colorauto myprocess暂停状态一般是用来做止损的但是为什么不删掉呢问题感觉又不严重由用户来决定kill -19 297259----这样也能暂停4.D状态阻塞状态两种S、D之前S被称为可中断休眠浅睡眠意思就是一个进程如果处于S状态我们可以直接把他们杀死。它的进程会响应我们杀它的动作。D状态被称为不可中断睡眠深度睡眠当系统资源严重不足,把进程做挂起也无法满足操作系统(OS)可能在极端情况下杀进程获取空间。从而进程无法响应磁盘的交互。所以规定了一个D状态防止重要数据丢失进程丢失dd命令向指定的文件中进行写入数据块dd if/dev/zero of~/test.txt bs4096 count1000005.X死亡状态---结束状态6.Z僵尸状态/进程---在死亡之后退出之前为了获取退出信息!目前接触的所有进程都是某一种进程的子进程我们创建子进程的目的是为了让子进程完成某种事情的---结果相关的信息父进程得获得所以说一个子进程退出不能立即释放所有资源将退出信息暂时维持住让父进程读取这个过程叫做Z状态1》哪些信息2》信息保存在哪里task_struct如何模拟验证Z状态[user1iZ5waahoxw3q2bZ 26-4-22]$ cat myprocess.c #includestdio.h #includesys/types.h #includeunistd.h int main() { pid_t idfork(); if(id0) { //child int count 5; while(count) { printf(我是子进程我正在运行:%d\n,count); sleep(1); count--; } } else { //parent while(1) { printf(我是父进程,我正在运行...\n); sleep(1); } } return 0; } [user1iZ5waahoxw3q2bZ 26-4-22]$ gcc myprocess.c -o myprocess[user1iZ5waahoxw3q2bZ 26-4-22]$ ./myprocess 我是父进程,我正在运行... 我是子进程我正在运行:5 我是父进程,我正在运行... 我是子进程我正在运行:4 我是父进程,我正在运行... 我是子进程我正在运行:3 我是父进程,我正在运行... 我是子进程我正在运行:2 我是父进程,我正在运行... 我是子进程我正在运行:1 我是父进程,我正在运行... 我是父进程,我正在运行... 我是父进程,我正在运行... 我是父进程,我正在运行... ^C[user1iZ5waahoxw3q2bZ ~]$ while :; do ps ajx | head -1 ; ps ajx | grep myprocess; sleep 1; done PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 20979 22684 22683 20979 pts/1 22683 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 20928 22686 22686 20928 pts/0 22686 S 1001 0:00 ./myprocess 22686 22687 22686 20928 pts/0 22686 S 1001 0:00 ./myprocess 20979 22691 22690 20979 pts/1 22690 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 20928 22686 22686 20928 pts/0 22686 S 1001 0:00 ./myprocess 22686 22687 22686 20928 pts/0 22686 S 1001 0:00 ./myprocess 20979 22696 22695 20979 pts/1 22695 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 20928 22686 22686 20928 pts/0 22686 S 1001 0:00 ./myprocess 22686 22687 22686 20928 pts/0 22686 S 1001 0:00 ./myprocess 20979 22701 22700 20979 pts/1 22700 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 20928 22686 22686 20928 pts/0 22686 S 1001 0:00 ./myprocess 22686 22687 22686 20928 pts/0 22686 S 1001 0:00 ./myprocess 20979 22706 22705 20979 pts/1 22705 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 20928 22686 22686 20928 pts/0 22686 S 1001 0:00 ./myprocess 22686 22687 22686 20928 pts/0 22686 S 1001 0:00 ./myprocess 20979 22711 22710 20979 pts/1 22710 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 20928 22686 22686 20928 pts/0 22686 S 1001 0:00 ./myprocess 22686 22687 22686 20928 pts/0 22686 Z 1001 0:00 [myprocess] defunct 20979 22716 22715 20979 pts/1 22715 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 20928 22686 22686 20928 pts/0 22686 S 1001 0:00 ./myprocess 22686 22687 22686 20928 pts/0 22686 Z 1001 0:00 [myprocess] defunct 20979 22722 22721 20979 pts/1 22721 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 20928 22686 22686 20928 pts/0 22686 S 1001 0:00 ./myprocess 22686 22687 22686 20928 pts/0 22686 Z 1001 0:00 [myprocess] defunct 20979 22727 22726 20979 pts/1 22726 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 20928 22686 22686 20928 pts/0 22686 S 1001 0:00 ./myprocess 22686 22687 22686 20928 pts/0 22686 Z 1001 0:00 [myprocess] defunct 20979 22732 22731 20979 pts/1 22731 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 20979 22737 22736 20979 pts/1 22736 S 1001 0:00 grep --colorauto myprocess ^C如果父进程一直不管不回收不获取子进程的推出信息那么Z会一直存在---会有内存泄漏问题知识点进程退出了内存泄漏问题还在不在--不在什么样的进程具有内存泄漏问题是比较麻烦的---常驻内存的进程知识点关于内核结构申请的可不可以在系统中维护一张废弃的task_struct的列表--unuse---类似于数据结构对象的缓存slab孤儿进程[user1iZ5waahoxw3q2bZ 26-4-22.2]$ cat myprocess.c #include stdio.h #include sys/types.h #include unistd.h int main() { pid_t id fork(); if(id 0) { // child while(1) { printf(我是一个子进程 pid: %d, ppid: %d\n, getpid(), getppid()); sleep(1); } } else { // father int cnt 5; while(cnt) { printf(我是一个父进程 pid: %d, ppid: %d\n, getpid(), getppid()); cnt--; sleep(1); } } return 0; }[user1iZ5waahoxw3q2bZ 26-4-22.2]$ ./myprocess 我是一个父进程 pid: 24124, ppid: 23053 我是一个子进程 pid: 24125, ppid: 24124 我是一个父进程 pid: 24124, ppid: 23053 我是一个子进程 pid: 24125, ppid: 24124 我是一个父进程 pid: 24124, ppid: 23053 我是一个子进程 pid: 24125, ppid: 24124 我是一个父进程 pid: 24124, ppid: 23053 我是一个子进程 pid: 24125, ppid: 24124 我是一个子进程 pid: 24125, ppid: 24124 我是一个父进程 pid: 24124, ppid: 23053 我是一个子进程 pid: 24125, ppid: 24124 [user1iZ5waahoxw3q2bZ 26-4-22.2]$ 我是一个子进程 pid: 24125, ppid: 1 我是一个子进程 pid: 24125, ppid: 1[user1iZ5waahoxw3q2bZ 26-4-22.2]$ while :; do ps ajx | head -1 ps ajx | grep myprocess; sleep 1 ; done PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 23104 24112 24111 23104 pts/1 24111 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 23104 24117 24116 23104 pts/1 24116 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 23104 24122 24121 23104 pts/1 24121 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 23053 24124 24124 23053 pts/0 24124 S 1001 0:00 ./myprocess 24124 24125 24124 23053 pts/0 24124 S 1001 0:00 ./myprocess 23104 24129 24128 23104 pts/1 24128 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 23053 24124 24124 23053 pts/0 24124 S 1001 0:00 ./myprocess 24124 24125 24124 23053 pts/0 24124 S 1001 0:00 ./myprocess 23104 24134 24133 23104 pts/1 24133 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 23053 24124 24124 23053 pts/0 24124 S 1001 0:00 ./myprocess 24124 24125 24124 23053 pts/0 24124 S 1001 0:00 ./myprocess 23104 24139 24138 23104 pts/1 24138 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 23053 24124 24124 23053 pts/0 24124 S 1001 0:00 ./myprocess 24124 24125 24124 23053 pts/0 24124 S 1001 0:00 ./myprocess 23104 24144 24143 23104 pts/1 24143 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 23053 24124 24124 23053 pts/0 24124 S 1001 0:00 ./myprocess 24124 24125 24124 23053 pts/0 24124 S 1001 0:00 ./myprocess 23104 24150 24149 23104 pts/1 24149 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 1 24125 24124 23053 pts/0 23053 S 1001 0:00 ./myprocess 23104 24155 24154 23104 pts/1 24154 S 1001 0:00 grep --colorauto myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 1 24125 24124 23053 pts/0 23053 S 1001 0:00 ./myprocess 23104 24190 24189 23104 pts/1 24189 S 1001 0:00 grep --colorauto myprocess父子进程关系中如果父进程先退出子进程要被1号进程领养这个被领养的过程子进程叫做孤儿进程---变成后台进程 |(会发现ctrlc无法退出) 操作系统为什么要领养如果不领养子进程进入僵尸之后父进程随便退被bash回收孤儿进程被1号init/systemd进程领养当然要有init/systemd进程回收。[user1iZ5waahoxw3q2bZ 26-4-22.2]$ ps ajx | head -1 ps ajx | grep systemd PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 0 1 1 1 ? -1 Ss 0 0:09 /usr/lib/systemd/systemd --system --deserialize 23 1 358 358 358 ? -1 Ss 0 0:03 /usr/lib/systemd/systemd-journald 1 379 379 379 ? -1 Ss 0 0:00 /usr/lib/systemd/systemd-udevd 1 522 522 522 ? -1 Ss 81 0:06 /usr/bin/dbus-daemon --system --addresssystemd: --nofork --nopidfile --systemd-activation 1 527 527 527 ? -1 Ss 0 0:03 /usr/lib/systemd/systemd-logind 23104 24227 24226 23104 pts/1 24226 S 1001 0:00 grep --colorauto systemd今天我们的学习就到此结束期待我们下次再见