一个程序启动之后就是一个进程,进程的数据在内存中,包括正文段和数据段,内存中的一些数据也会写入到proc文件系统中。


$ ll /proc/
total 4
dr-xr-xr-x 447 root     root        0 Mar  8 06:31 ./
drwxr-xr-x   1 root     root     4096 Mar  1 07:45 ../
dr-xr-xr-x   9 root     root        0 Mar  8 11:43 1/
dr-xr-xr-x   9 root     root        0 Mar  8 11:47 12/
dr-xr-xr-x   9 root     root        0 Mar  8 11:47 21/
dr-xr-xr-x   9 root     root        0 Mar  8 11:47 22/
dr-xr-xr-x   9 root     root        0 Mar  8 11:47 23/
dr-xr-xr-x   9 root     root        0 Mar  8 11:47 24/
dr-xr-xr-x   9 root     root        0 Mar  8 11:47 3252/
dr-xr-xr-x   9 laradock laradock    0 Mar  8 11:47 499/
dr-xr-xr-x   9 root     root        0 Mar  8 11:47 983/


top

top - display Linux processes.

The top program provides a dynamic real-time view of a running system.

It can display system summary information as well as a list of processes or threads currently being managed by the Linuxkernel.

The types of system summary information shown and the types, order and size of information displayed for processes are all user configurable and that configuration can be made persistent across restarts.

The program provides a limited interactive interface for process manipulation as well as a much more extensive interface for personal configuration – encompassing every aspect of its operation.

And while top is referred to throughout thisdocument, you are free to name the program anything you wish.

That new name, possibly an alias, will then be reflected on top’s display and used when reading and writing a configuration file.

top - 23:28:59 up 13:13,  1 user,  load average: 0.30, 1.20, 1.61
任务: 386 total,   1 running, 385 sleeping,   0 stopped,   0 zombie
%Cpu(s):  2.8 us,  1.7 sy,  0.1 ni, 95.3 id,  0.0 wa,  0.0 hi,  0.2 si,  0.0 st
MiB Mem :   5855.6 total,    664.6 free,   3875.4 used,   1315.7 buff/cache
MiB Swap:    976.0 total,    364.7 free,    611.3 used.   1379.6 avail Mem 

进程号 USER      PR  NI    VIRT    RES    SHR    %CPU  %MEM     TIME+ COMMAND                                                                        
7305  wanger    20   0  4956504   454380 52432 S  4.6   7.6  28:24.62 gnome-shell

...

pstree 查看 Linux 进程间的关系,显示进程树。

pstree shows running processes as a tree.

The tree is rooted at either pid or init if pid is omitted.

If a user name is specified, all process trees rooted at processes owned by that user are shown.

pstree -ap

    ├─sshd,9408
    │   └─sshd,444836    
    │       └─bash,444875
    │           └─pstree,444893 -ap

ps

ps displays information about a selection of the active processes.

If you want a repetitive update of the selection and the displayed information, use top instead.


$ ps ejx
   PPID     PID    PGID     SID TTY        TPGID STAT   UID   TIME COMMAND
      0       1       1       1 pts/0          1 Ss+      0   0:00 /usr/bin/python3 -u /sbin/my_init PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/us
      1      12      12       1 pts/0          1 S        0   0:01 /usr/sbin/syslog-ng --pidfile /var/run/syslog-ng.pid -F --no-caps LC_ALL=en_US.UTF-
      1      21       1       1 pts/0          1 S+       0   0:00 /usr/bin/runsvdir -P /etc/service AST_VERSION=1.0.10 BLACKFIRE_CLIENT_ID="<client_i
     21      22      22      22 ?             -1 Ss       0   0:00 runsv cron AST_VERSION=1.0.10 BLACKFIRE_CLIENT_ID="<client_id>" BLACKFIRE_CLIENT_TO
     21      23      23      23 ?             -1 Ss       0   0:00 runsv sshd AST_VERSION=1.0.10 BLACKFIRE_CLIENT_ID="<client_id>" BLACKFIRE_CLIENT_TO
     22      24      22      22 ?             -1 S        0   0:00 /usr/sbin/cron -f NPM_FETCH_RETRY_MINTIMEOUT=10000 LANGUAGE=en_US.UTF-8 NPM_FETCH_R
      0     983     983     983 pts/1       3354 Ss       0   0:00 bash PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/larado
    983    3354    3354     983 pts/1       3354 R+       0   0:00 ps ejx LC_ALL=en_US.UTF-8 NVM_DIR=/home/laradock/.nvm LS_COLORS=no=00:fi=00:di=01;3


Linux 系统中一般会把进程/线程称为任务Task。 TTY 是一个物理终端|伪终端pts/0 ssh / telnet

proc文件系统

process information pseudo-filesystem.(进程信息伪文件系统)

The proc filesystem is a pseudo-filesystem which provides an interface to kernel data structures. It is commonly mounted at /proc.

Typically, it is mounted automatically by the system, but it can also be mounted manually using a command such as:

mount -t proc proc /proc

Most of the files in the proc filesystem are read-only, but some files are writable, allowing kernel variables to be changed.


/proc/983# ll
total 0
dr-xr-xr-x   9 root root 0 Mar  8 11:56 ./
dr-xr-xr-x 460 root root 0 Mar  8 06:31 ../
-r--r--r--   1 root root 0 Mar  8 11:56 arch_status
dr-xr-xr-x   2 root root 0 Mar  8 11:56 attr/
-rw-r--r--   1 root root 0 Mar  8 11:56 autogroup
-r--------   1 root root 0 Mar  8 11:56 auxv
-r--r--r--   1 root root 0 Mar  8 11:56 cgroup
--w-------   1 root root 0 Mar  8 11:56 clear_refs
-r--r--r--   1 root root 0 Mar  8 11:56 cmdline
-rw-r--r--   1 root root 0 Mar  8 11:56 comm
-rw-r--r--   1 root root 0 Mar  8 11:56 coredump_filter
-r--r--r--   1 root root 0 Mar  8 11:56 cpu_resctrl_groups
-r--r--r--   1 root root 0 Mar  8 11:56 cpuset
lrwxrwxrwx   1 root root 0 Mar  8 11:56 cwd -> /proc/983/
-r--------   1 root root 0 Mar  8 11:56 environ
lrwxrwxrwx   1 root root 0 Mar  8 11:56 exe -> /bin/bash*
dr-x------   2 root root 0 Mar  8 11:56 fd/
dr-x------   2 root root 0 Mar  8 11:56 fdinfo/
-rw-r--r--   1 root root 0 Mar  8 11:56 gid_map
-r--------   1 root root 0 Mar  8 11:56 io
-r--r--r--   1 root root 0 Mar  8 11:56 limits
-rw-r--r--   1 root root 0 Mar  8 11:56 loginuid
dr-x------   2 root root 0 Mar  8 11:56 map_files/
-r--r--r--   1 root root 0 Mar  8 11:56 maps
-rw-------   1 root root 0 Mar  8 11:56 mem
-r--r--r--   1 root root 0 Mar  8 11:56 mountinfo
-r--r--r--   1 root root 0 Mar  8 11:56 mounts
-r--------   1 root root 0 Mar  8 11:56 mountstats
dr-xr-xr-x  53 root root 0 Mar  8 11:56 net/
dr-x--x--x   2 root root 0 Mar  8 11:56 ns/
-r--r--r--   1 root root 0 Mar  8 11:56 numa_maps
-rw-r--r--   1 root root 0 Mar  8 11:56 oom_adj
-r--r--r--   1 root root 0 Mar  8 11:56 oom_score
-rw-r--r--   1 root root 0 Mar  8 11:56 oom_score_adj
-r--------   1 root root 0 Mar  8 11:56 pagemap
-r--------   1 root root 0 Mar  8 11:56 patch_state
-r--------   1 root root 0 Mar  8 11:56 personality
-rw-r--r--   1 root root 0 Mar  8 11:56 projid_map
lrwxrwxrwx   1 root root 0 Mar  8 11:56 root -> //
-rw-r--r--   1 root root 0 Mar  8 11:56 sched
-r--r--r--   1 root root 0 Mar  8 11:56 schedstat
-r--r--r--   1 root root 0 Mar  8 11:56 sessionid
-rw-r--r--   1 root root 0 Mar  8 11:56 setgroups
-r--r--r--   1 root root 0 Mar  8 11:56 smaps
-r--r--r--   1 root root 0 Mar  8 11:56 smaps_rollup
-r--------   1 root root 0 Mar  8 11:56 stack
-r--r--r--   1 root root 0 Mar  8 11:56 stat
-r--r--r--   1 root root 0 Mar  8 11:56 statm
-r--r--r--   1 root root 0 Mar  8 11:56 status
-r--------   1 root root 0 Mar  8 11:56 syscall
dr-xr-xr-x   3 root root 0 Mar  8 11:56 task/
-rw-r--r--   1 root root 0 Mar  8 11:56 timens_offsets
-r--r--r--   1 root root 0 Mar  8 11:56 timers
-rw-rw-rw-   1 root root 0 Mar  8 11:56 timerslack_ns
-rw-r--r--   1 root root 0 Mar  8 11:56 uid_map
-r--r--r--   1 root root 0 Mar  8 11:56 wchan