一 代码
#include<iostream>
#include<unistd.h>
using namespace std;
int main(){
for(int i=0;i<100;i++){
cout<<"i:"<<i<<endl;
sleep(5);
}
return 0;
}
二 编译运行
[root@localhost charpter05]# vi 0506.cpp
[root@localhost charpter05]# g++ 0506.cpp -o test
[root@localhost charpter05]# ./test
i:0
三 ps命令实战
1 显示指定用户
[root@localhost charpter05]# ps -u root
PID TTY TIME CMD
1 ? 00:00:01 systemd
2 ? 00:00:00 kthreadd
3 ? 00:00:00 ksoftirqd/0
......
890 ? 00:00:00 sshd
992 ? 00:00:00 master
1001 ? 00:00:00 sshd
1005 pts/0 00:00:00 bash
1026 ? 00:00:00 kworker/0:0
1032 pts/0 00:00:00 test
1033 ? 00:00:00 sshd
1037 pts/1 00:00:00 bash
1054 pts/1 00:00:00 ps
其中的test进程,正是刚刚手动运行的那个
2 显示所有进程信息
[root@localhost charpter05]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 21:34 ? 00:00:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root 2 0 0 21:34 ? 00:00:00 [kthreadd]
root 3 2 0 21:34 ? 00:00:00 [ksoftirqd/0]
root 5 2 0 21:34 ? 00:00:00 [kworker/0:0H]
root 6 2 0 21:34 ? 00:00:00 [kworker/u2:0]
root 7 2 0 21:34 ? 00:00:00 [migration/0]
root 8 2 0 21:34 ? 00:00:00 [rcu_bh]
root 9 2 0 21:34 ? 00:00:00 [rcu_sched]
......
root 1001 890 0 21:37 ? 00:00:00 sshd: root@pts/0
root 1005 1001 0 21:37 pts/0 00:00:00 -bash
root 1026 2 0 21:39 ? 00:00:00 [kworker/0:0]
root 1032 1005 0 21:40 pts/0 00:00:00 ./test
root 1033 890 0 21:41 ? 00:00:00 sshd: root@pts/1
root 1037 1033 0 21:41 pts/1 00:00:00 -bash
root 1055 1037 0 21:42 pts/1 00:00:00 ps -ef
显示所有进程信息,连同命令行
3 ps与grep常用组合用法
[root@localhost charpter05]# ps -ef|grep test
root 1032 1005 0 21:40 pts/0 00:00:00 ./test
root 1057 1037 0 21:43 pts/1 00:00:00 grep --color=auto test
ps与grep常用组合用法,查找特定进程
4 将目前登入的PID与相关信息列式出来
[root@localhost charpter05]# ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 0 1037 1033 0 80 0 - 28880 do_wai pts/1 00:00:00 bash
0 R 0 1058 1037 0 80 0 - 37235 - pts/1 00:00:00 ps
在预设的情况下,ps仅会列出与目前所在的bash shell有关的PID,所以,当使用ps -l的时候,只有2个PID
5 列出目前所有的正在内存当中的程序
[root@localhost charpter05]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.3 0.6 128168 6816 ? Ss 21:34 0:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root 2 0.0 0.0 0 0 ? S 21:34 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 21:34 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< 21:34 0:00 [kworker/0:0H]
root 6 0.0 0.0 0 0 ? S 21:34 0:00 [kworker/u2:0]
root 7 0.0 0.0 0 0 ? S 21:34 0:00 [migration/0]
root 8 0.0 0.0 0 0 ? S 21:34 0:00 [rcu_bh]
......
postfix 994 0.0 0.3 89716 4012 ? S 21:35 0:00 qmgr -l -t unix -u
root 1001 0.0 0.5 145784 5340 ? Ss 21:37 0:00 sshd: root@pts/0
root 1005 0.0 0.2 115524 2184 pts/0 Ss 21:37 0:00 -bash
root 1026 0.0 0.0 0 0 ? S 21:39 0:00 [kworker/0:0]
root 1032 0.0 0.1 12496 1060 pts/0 S+ 21:40 0:00 ./test
root 1033 0.0 0.5 145784 5336 ? Ss 21:41 0:00 sshd: root@pts/1
root 1037 0.0 0.2 115520 2156 pts/1 Ss 21:41 0:00 -bash
root 1059 0.0 0.1 151064 1804 pts/1 R+ 21:44 0:00 ps aux