嵌入式Linux并发程序设计,进程,查看进程信息ps/top/"/proc",修改进程优先级nice/renice,后台进程相关命令jobs/fg/bg

1,查看进程信息,ps查看系统进程快照

a,ps -ef|more

linux@linux:~$ ps -ef|more
  1. "-ef"显示系统中所有的进程信息
  2. 管道符’|'加"more",让进程信息一屏一屏显示
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 11:36 ?        00:00:01 /sbin/init
root         2     0  0 11:36 ?        00:00:00 [kthreadd]
root         3     2  0 11:36 ?        00:00:00 [ksoftirqd/0]
root         4     2  0 11:36 ?        00:00:00 [kworker/0:0]
root         5     2  0 11:36 ?        00:00:00 [kworker/0:0H]
--More--
  1. UID创建进程的用户的ID
  2. PID进程的进程号
  3. PPID进程的父进程号
  4. C CPU占用率
  5. STIME进程的开始时间
  6. TTY进程所关联的终端
  7. TIME进程执行时所占用的时间
  8. CMD进程所对应的程序的名称

b,ps -ef|grep

编写程序test.c

#include <stdio.h> 

int main(int argc, const char *argv[])
{
	while(1);
	return 0;
}

编译并运行

linux@linux:~/test/process$ gcc test.c -o test.out
linux@linux:~/test/process$ ./test.out 

在另一终端中

linux@linux:~$ ps -ef|grep test
linux     3472  2764 89 11:57 pts/11   00:00:05 ./test.out
linux     3475  3407  0 11:58 pts/1    00:00:00 grep --color=auto test

c,ps -aux|more

linux@linux:~$ ps -ef|more
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 11:36 ?        00:00:01 /sbin/init
root         2     0  0 11:36 ?        00:00:00 [kthreadd]
...
...
linux@linux:~$ ps -aux|more
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1   4460  1956 ?        Ss   11:36   0:01 /sbin/init
root         2  0.0  0.0      0     0 ?        S    11:36   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    11:36   0:00 [ksoftirqd/0]
root         4  0.0  0.0      0     0 ?        S    11:36   0:00 [kworker/0:0]
--More--
  1. STAT当前进程状态
  2. man ps命令可以查看相关的状态说明

d,ps -aux|grep

linux@linux:~$ ps -ef|grep test
linux     3529  2764 99 12:00 pts/11   01:24:33 ./test.out
linux     3577  3407  0 13:25 pts/1    00:00:00 grep --color=auto test
linux@linux:~$ ps -aux|grep test
linux     3529 99.2  0.0   2024   280 pts/11   R+   12:00  89:46 ./test.out
linux     3593  0.0  0.0   6108   856 pts/1    S+   13:31   0:00 grep --color=auto test

2,查看进程信息,top查看进程动态信息,q退出

linux@linux:~$ top

top - 13:52:49 up  2:16,  3 users,  load average: 0.78, 0.49, 0.73
Tasks: 187 total,   2 running, 185 sleeping,   0 stopped,   0 zombie
%Cpu(s): 94.3 us,  5.7 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   1025492 total,   720900 used,   304592 free,     2320 buffers
KiB Swap:  1951740 total,   173664 used,  1778076 free.   159884 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                    
 3604 linux     20   0    2024    276    224 R 82.6  0.0   1:16.48 test.out                   
 1390 root      20   0  282816  26976   8500 S  8.7  2.6   0:21.62 Xorg                       
 2537 linux     20   0  319316  44736  17704 S  4.0  4.4   0:38.76 compiz                     
 2758 linux     20   0  128916  14236   7472 S  4.0  1.4   0:05.86 gnome-terminal             
 1664 root      20   0   20584   2632   1908 S  0.3  0.3   0:07.32 vmtoolsd                   
 3605 linux     20   0    6856   1428   1036 R  0.3  0.1   0:00.08 top                        
    1 root      20   0    4460   1956   1124 S  0.0  0.2   0:01.83 init                       
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd  
  1. NI进程优先级[-20,19],值越小优先级越高,默认是0

3,查看进程信息,/proc目录查看进程详细信息

a,运行程序

linux@linux:~/test/process$ ./test.out 
    

b,在另一终端中查看进程号

linux@linux:~$ ps -aux|grep test
linux     3604 99.0  0.0   2024   276 pts/11   R+   13:51   8:20 ./test.out
linux     3608  0.0  0.0   6108   856 pts/1    S+   13:59   0:00 grep --color=auto test
  1. 可得知test.out进程号是3604

c,进入/proc目录下的/3604目录

linux@linux:~$ cd /proc/
linux@linux:/proc$ ls
1     129   146   20    2364  2561  2850  425  9            ipmi           scsi
10    13    147   21    2365  2564  2858  43   905          irq            self
1007  130   148   2101  2370  2581  2883  45   911          kallsyms       slabinfo
1093  131   149   2184  2384  2591  29    46   acpi         kcore          softirqs
1099  132   1497  2187  2408  26    2906  5    asound       key-users      stat
11    133   15    22    2410  2638  2939  514  buddyinfo    kmsg           swaps
1105  1332  150   2259  2414  2641  3     567  bus          kpagecount     sys
1106  134   151   2270  2416  2655  30    595  cgroups      kpageflags     sysrq-trigger
1109  135   152   2278  2419  2660  3020  599  cmdline      latency_stats  sysvipc
112   136   153   2285  2422  2670  3022  601  consoles     loadavg        timer_list
114   137   16    2301  2424  2672  31    602  cpuinfo      locks          timer_stats
116   138   1627  2303  2428  2691  3119  603  crypto       mdstat         tty
1162  139   1664  2305  2440  27    3285  613  devices      meminfo        uptime
1165  1390  17    2309  2480  2733  3288  619  diskstats    misc           version
1178  14    1745  2311  2494  2758  337   620  dma          modules        version_signature
119   140   1762  2331  2496  2763  3407  621  driver       mounts         vmallocinfo
12    1403  1795  2340  25    2764  351   633  execdomains  mpt            vmstat
1211  141   18    2349  2504  2779  3604  69   fb           mtrr           zoneinfo
122   1411  181   2350  2509  28    3613  7    filesystems  net
125   142   19    2354  2531  2817  4     70   fs           pagetypeinfo
127   143   193   2355  2537  2824  415   794  interrupts   partitions
128   144   194   2361  2554  2838  418   8    iomem        sched_debug
1287  145   2     2363  2558  2843  424   844  ioports      schedstat
  1. /3604目录下就是test.out进程的详细信息
linux@linux:/proc$ cd 3604/
linux@linux:/proc/3604$ ls
attr        coredump_filter  gid_map    mem         oom_score      schedstat  syscall
autogroup   cpuset           io         mountinfo   oom_score_adj  sessionid  task
auxv        cwd              latency    mounts      pagemap        smaps      timers
cgroup      environ          limits     mountstats  personality    stack      uid_map
clear_refs  exe              loginuid   net         projid_map     stat       wchan
cmdline     fd               map_files  ns          root           statm
comm        fdinfo           maps       oom_adj     sched          status
  1. status文件中存放了进程的详细信息
linux@linux:/proc/3604$ cat status
Name:	test.out
State:	R (running)
Tgid:	3604
Ngid:	0
Pid:	3604
PPid:	2764
TracerPid:	0
Uid:	1000	1000	1000	1000
Gid:	1000	1000	1000	1000
FDSize:	256
Groups:	4 24 27 30 46 108 124 1000 
VmPeak:	    2112 kB
VmSize:	    2024 kB
VmLck:	       0 kB
VmPin:	       0 kB
VmHWM:	     276 kB
VmRSS:	     276 kB
VmData:	      28 kB
VmStk:	     136 kB
VmExe:	       4 kB
VmLib:	    1828 kB
VmPTE:	      16 kB
VmSwap:	       0 kB
Threads:	1
SigQ:	0/7861
SigPnd:	0000000000000000
ShdPnd:	0000000000000000
SigBlk:	0000000000000000
SigIgn:	0000000000000000
SigCgt:	0000000000000000
CapInh:	0000000000000000
CapPrm:	0000000000000000
CapEff:	0000000000000000
CapBnd:	0000001fffffffff
Seccomp:	0
Cpus_allowed:	ff
Cpus_allowed_list:	0-7
Mems_allowed:	1
Mems_allowed_list:	0
voluntary_ctxt_switches:	1
nonvoluntary_ctxt_switches:	42261
  1. /fd目录中存放了进程打开的所有文件的信息
linux@linux:/proc/3604$ ll fd
total 0
dr-x------ 2 linux linux  0 11月  6 13:59 ./
dr-xr-xr-x 9 linux linux  0 11月  6 13:51 ../
lrwx------ 1 linux linux 64 11月  6 14:10 0 -> /dev/pts/11
lrwx------ 1 linux linux 64 11月  6 14:10 1 -> /dev/pts/11
lrwx------ 1 linux linux 64 11月  6 13:59 2 -> /dev/pts/11

4,修改进程优先级,nice按用户指定的优先级运行进程

a,运行程序test.out,使用top命令先查看一下优先级NI

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                    
 3604 linux     20   0    2024    276    224 R 93.4  0.0  33:01.93 test.out                   
 1390 root      20   0  283460  27132   8504 R  3.3  2.6   0:32.40 Xorg                       
 2758 linux     20   0  133492  15904   8976 S  2.0  1.6   0:10.35 gnome-terminal             
 2537 linux     20   0  319296  44796  17760 S  1.0  4.4   0:46.36 compiz                     
    1 root      20   0    4460   1956   1124 S  0.0  0.2   0:01.83 init                       
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd                   

b,可以看到test.out运行优先级是默认的0,“Ctrl+C”终止程序,以nice -n 3 ./test.out运行

^C
linux@linux:~/test/process$ nice -n 3 ./test.out

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                    
 3682 linux     23   3    2024    280    224 R 84.2  0.0   2:13.46 test.out                   
 1390 root      20   0  283460  26940   8504 R 10.0  2.6   0:34.95 Xorg                       
 2758 linux     20   0  133492  15904   8976 S  3.3  1.6   0:11.55 gnome-terminal             
 2537 linux     20   0  319500  44968  17764 S  2.0  4.4   0:47.58 compiz                     
    1 root      20   0    4460   1956   1124 S  0.0  0.2   0:01.83 init                       
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd                   
  1. 可以看到test.out进程优先级变为了3
  2. 使用nice时,普通用户更改优先级智能在[0,19]范围内,管理员可以操作[-20,19]

5,修改进程优先级,renice改变正在运行进程的优先级

a,在终端一内运行test.out

linux@linux:~/test/process$ ./test.out 

b,在终端二内使用top查看进程状态

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                    
 3687 linux     20   0    2024    276    224 R 90.3  0.0   2:10.60 test.out                                                
 2364 linux     20   0  116764  11228   5516 S  0.3  1.1   0:00.87 ibus-ui-gtk3               
 2581 linux     20   0   61724   5572   3700 S  0.3  0.5   0:12.14 vmtoolsd                   
    1 root      20   0    4460   1956   1124 S  0.0  0.2   0:01.83 init                       
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd                                 

c,在终端三内使用renice -n 4 3687,改变进程优先级

linux@linux:~/test/process$ renice -n 4 3687
3687 (process ID) old priority 0, new priority 4                              

d,在终端二内看到进程优先级已经改变

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                    
 3687 linux     24   4    2024    276    224 R 94.7  0.0   7:30.79 test.out                   
 1390 root      20   0  288356  27084   8516 S  3.0  2.6   0:40.57 Xorg                       
 2758 linux     20   0  134152  17436   9600 S  1.3  1.7   0:14.40 gnome-terminal                             
    1 root      20   0    4460   1956   1124 S  0.0  0.2   0:01.83 init                       
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd                                            
  1. 普通用户只能降低一个已有进程的优先级,即使NI值增大
  2. 管理员可以降低或提高进程优先级

6,后台进程相关命令,jobs查看某个终端上的后台进程

a,"Ctrl+C"用于结束一个进程,此时用jobs看不到后台有进程

linux@linux:~/test/process$ ./test.out 
^C
linux@linux:~/test/process$ jobs

b,"Ctrl+Z"用于暂停一个进程,此时用jobs可以看到有一个进程在后台挂起,作业号为1

linux@linux:~/test/process$ ./test.out 
^Z
[1]+  Stopped                 ./test.out
linux@linux:~/test/process$ jobs
[1]+  Stopped                 ./test.out

c,运行程序时,在后面加一个取地址符"&",代表该程序要在后台运行,用jobs可以看到又多了一个后台进程,作业号为2

linux@linux:~/test/process$ ./test.out &
[2] 3808
linux@linux:~/test/process$ jobs
[1]+  Stopped                 ./test.out
[2]-  Running                 ./test.out &

7,后台进程相关命令,fg把后台运行的进程放到前台运行

linux@linux:~/test/process$ jobs
[1]+  Stopped                 ./test.out
[2]   Running                 ./test.out &
[3]-  Running                 ./test.out &
linux@linux:~/test/process$ fg 2
./test.out
^C
linux@linux:~/test/process$ jobs
[1]+  Stopped                 ./test.out
[3]-  Running                 ./test.out &
linux@linux:~/test/process$ fg 3
./test.out
^Z
[3]+  Stopped                 ./test.out
linux@linux:~/test/process$ jobs
[1]-  Stopped                 ./test.out
[3]+  Stopped                 ./test.out

8,后台进程相关命令,bg将挂起的进程在后台运行

linux@linux:~/test/process$ jobs
[1]-  Stopped                 ./test.out
[3]+  Stopped                 ./test.out
linux@linux:~/test/process$ bg 3#将挂起的进程,作业3,在后台运行
[3]+ ./test.out &
linux@linux:~/test/process$ jobs
[1]+  Stopped                 ./test.out
[3]-  Running                 ./test.out &
linux@linux:~/test/process$ fg 3#将后台进程,作业3,放到前台运行
./test.out
^C
linux@linux:~/test/process$ jobs
[1]+  Stopped                 ./test.out
linux@linux:~/test/process$ jobs
[1]+  Stopped                 ./test.out
linux@linux:~/test/process$ fg 1#将挂起的进程,作业1,放到前台运行
./test.out
^Z
[1]+  Stopped                 ./test.out
linux@linux:~/test/process$ jobs
[1]+  Stopped                 ./test.out
linux@linux:~/test/process$ bg 1#将挂起的进程,作业1,在后台运行
[1]+ ./test.out &
linux@linux:~/test/process$ jobs
[1]+  Running                 ./test.out &
linux@linux:~/test/process$ fg 1#将挂起的进程,作业1,放到前台运行
./test.out
^C
linux@linux:~/test/process$ jobs
  1. fg可以将后台运行的进程放到前台运行
  2. fg也可以将挂起的进程放到前台运行

猜你喜欢

转载自blog.csdn.net/m0_37542524/article/details/83788922