Linux Chapter 6-Process Management

Introduction to Linux

1. Basic overview of the process

1. Description

  • A process is a running instance of an executable program that has been started.
  • The working mechanism of cpu is time-sharing, scheduling is carried out through the kernel, and working time is measured by nanoseconds.

A directory named by a number in the /proc directory. Each directory represents a process and stores the attribute information of the process. The PID of each process is unique. Even if the process exits, the next process will not use this PID.
Insert picture description here

2. The components of the process

  • Address space of allocated memory
  • Security attributes, including ownership credentials and privileges
  • One or more threads of execution of program code
  • Process status

3. Process environment

  • Local and global variables
  • Current scheduling context
  • Allocated system resources, such as file descriptors and network ports

4. The production of the process

  • /proc//pidfd: (fd-file describe; file descriptor)
  • Parent process-child process:

The existing parent process copies the byte address space (fork) to create a new child process structure.
Each new process is assigned a unique process ID (PID) to meet the needs of tracking and security, PID and parent process ID ( PPID) is an element of the new process environment.

  • Any process can create child processes, all processes are descendants of the first system process, the first system process is systemd

Insert picture description here

Through the fork process, the child process inherits security identities, past and current file descriptors, port and resource privileges, environment variables, and program code. Subsequently, the child process may exec its own program code.
Usually, the parent process is in a sleep state while the child process is running, and a request (wait) is set to signal when the child process is completed. Upon exit, the child process may have closed or discarded its resources and environment, and the remaining part is called a zombie (zombie). The parent process is awakened by receiving a signal when the child process exits, cleans up the remaining structure, and then continues to execute its own program code.

5. Classification of processes

  • Foreground process: the process related to the terminal, the process started through the terminal:

You can also send the process started in the foreground to the background for processing, run in daemon mode, and suspend the process through &.

  • Daemon: daemon, a process that has nothing to do with the terminal (such as the kernel), a process started during system boot
  • zombie (zombie process) destroys zombie process by restarting the system

Second, the process status

Insert picture description here

  • Excuting: running status
  • ** Ready: Ready state, also called sleep state **

a Uninterruptible sleep //Sleep that cannot be terminal, and cannot be awakened at any time. It can only be awakened when the IO resource is successfully loaded.
b, interruptible //The terminal can sleep and wake up at any time.

  • Zombie: Zombie process, the normal operation is over, but the temporary memory is not released.

  • Stopped: //Stopped state, paused in memory, but will not be scheduled unless manually started

  • Reasons for process sleep:

When an executing process needs to load additional IO resources, because the speed of the IO device is too slow, it will go to sleep and wait, and hand over the CPU to other processes to avoid wasting the remaining execution time
in the multitasking operating system In, each CPU (or CPU core) processes a process at a point in time. While the process is running, its direct requirements for CPU time and resource allocation will change. The process is assigned a state, which changes with environmental requirements.

  • Linux process status
Sign State name and description defined by the kernel
R TASK_RUNNING: The process is executing on the CPU or is waiting to run. When in a running (or runnable) state, the process may be executing user routines or kernel routines (system calls), or it may be queued and ready
S TASK_INTERRUPTIBLE: The process is in a sleep state and is waiting for a certain condition: hardware request, system resource access or signal. When the event or signal meets the condition, the process will return to running
D TASK_UNINTERRUPTIBLE: This process is also sleeping, but unlike the S state, it will not respond to the transmitted signal. Use only under certain conditions, where process interruption may cause unexpected device status
K TASK_KILLABLE: The process is in a sleep state, which is the same as the non-interruptible D state, but has been modified to allow the waiting task to be interrupted (completely exit) by responding to the signal. Utilities usually display interruptible processes as D status
T TASK_STOPPED: The process has been stopped (suspended), usually a signal sent by the user or other processes. The process can return to the running state through another signal and continue execution (recovery)
T TASK_TRACED: The process being debugged will also be temporarily stopped, and share the same T status flag
with EXIT_ZOMBIE: The child process sends a signal to the parent process when it exits. All resources except the process identity (PID) have been released
X EXIT_DEAD: When the parent process cleans up (acquires) the remaining child process structure, the process is now completely released. This status is never seen in the process listing utility
< High priority process
N Low priority process
+ Processes in the foreground process group
| Multithreaded process
s Session process first process

Three, process priority

1. Linux process scheduling and multitasking.

  • a. Modern computer systems include both low-end processors that can only execute one instruction at a time, and high-performance supercomputers. Each of these supercomputers is equipped with hundreds of CPUs and can execute hundreds of instructions in parallel. But all these systems often have one thing in common: the number of processes they need to run always exceeds the actual number of cores.

  • b. Through time slicing technology, the number of processes (and threads) that Linux (and other operating systems) can actually run can exceed the number of actual processing units available, and the operating system processes quickly switch, thus giving users a kind of The impression of multiple processes running at the same time,

  • c. The part of the linux kernel that performs this switch is called the process scheduler.

2. Process priority

  • Process priority range: 0-139, the smaller the number, the higher the priority.

0-99: real-time priority, kernel adjustment
100-139: static priority, user controllable

  • Features of high process priority

Get more CPU operation actually
give priority to the opportunity to get CPU operation

  • To modify the priority of the process can be achieved by adjusting the nice value of the process, the smaller the nice value, the higher the priority;

The range of nice value is (-20, 19), -20 corresponds to 100, 19 corresponds to 139 (the maximum priority is -20)

3. Relative priority:

  • Since not every process is as important as other processes, you can tell the scheduler to use different scheduling strategies for different processes. The scheduling strategy used by most processes running on conventional systems is called SCHED_OTHER (also called SCHED_NORMAL), but there are some other strategies that can be used for different purposes.

  • Since not all processes are created in the same way, you can specify a relative priority for the processes running with the SCHED_NORMAL strategy. This priority is called the nice value of the process. A process can have 40 different levels of nice values.

  • These nice levels range from -20 to 19. By default, the process will inherit the nice level of its parent process, usually 0

  • The higher the nice level, the lower the priority (the process is easy to give up its CPU usage to other processes)

  • The lower the nice level, the higher the priority (the process is less inclined to give up the CPU)

  • If there is no resource contention (for example, when the number of active processes is less than the number of available CPU cores), even high-level nice processes will still use all the available CPU resources that they can use. But when the number of processes requesting CPU time exceeds the number of available cores, processes with a higher level of nice will receive less CPU time than processes with a lower level of nice

4 nice level and permissions

  • Setting a lower nice level for a process that takes up CPU resources may have a negative impact on the performance of other processes running on the same system,
    so only the root user is allowed to set a negative nice level and lower the nice level of existing processes. Root can be adjusted at will, ordinary users can only lower the priority.

  • Ordinary non-privileged users are only allowed to set a positive nice level. Only the nice level can be raised to the existing process, but the nice level cannot be lowered.

5. Process priority adjustment: adjust the nice value

  • Adjust the nice value of the started process: renice NI PID (such as renice 3 3704)
  • Specify the nice value at startup: (-20, 19) nice -n NI COMMAND

Four, process management commands

1,ps

  • The ps (process state) command is used to list the current processes. Can display detailed process information,
  • Include the following

-User Identifier (UID), which determines the privileges of the process
-Unique Process Identifier (PID) Parent Process ID (PPID)
-CPU and real-time time spent-the amount
of memory allocated by the process in various locations-the location of the
process STDOUT, called the control terminal
-current process status

  • Three option formats supported by os
a UNIX (POSIX) options, which can be grouped but must start with a hyphen; such as ps -aux
b BSD option, can be grouped but not used with hyphen
c GNU long options, start with a double hyphen
  • ps (process state), displays process information. Precautions:

    The ones with [] brackets indicate the kernel thread, usually at the top
    exiting or defunct indicate the zombie process

  • Common options

A //Display all processes related to the terminal
U //Show which user started the process
X //Display all processes that have nothing to do with the terminal
-e //Display all processes, the same as -A
-l //Display in long format
-F //Display more detailed complete format process information
-f //Display more detailed complete format process information
-H //Display process related information in process level format
-The //Select the field to be displayed according to your needs

Insert picture description here

  • //aux result analysis

VSZ //Virtual memory SiZe, virtual memory set
RSS //ReSident Size, resident memory set
STAT //process status
TIME //accumulated duration of runtime

  • //ps command result analysis

NI //nice value
PRI //priority
PSR //which CPU core the process is running on
RTPTRIO //real-time priority
C //running CPU number
STIME //starting time of the process
VSZ //Virtual memory SiZe, virtual memory set
RSS //ReSident Size, resident memory set
STAT //Process status
TIME //The cumulative duration of runtime

2 pstree

  • pstree: used to display the process tree on the current system
  • Such asInsert picture description here

3,pgrep

  • Specify only those processes in the style of grep. Find processes that meet certain characteristics in the current system and only display the process number.
  • Insert picture description here

4,pidof

  • pidof looks up its PID number based on the process name.
  • Such asInsert picture description here

5,vmstat

  • Command for viewing virtual memory status: Syntax: vmstat [option] [delay{conunt}]

For example:
vmstat 2 // means refreshing every 2 seconds
vmstat 2 5 // means refreshing every 2 seconds, and exit after refreshing 5 times.
//Commonly used options:
-s //Display memory statistics

Insert picture description here

  • 1, procs:
    r (running) // indicates the length of the queue waiting to run, that is, the number of processes waiting to run
    b (block) // indicates the length of the blocking queue, that is, the number of processes in an uninterruptible sleep state

  • 2, memory: storage usage records

    swpd //Total usage of swap memory (total space)
    free //Total amount of free physical memory (available space)
    buffer //Total amount of memory used for buffer (buffer)
    cache //Total amount of memory used for cache (cache)
  • 3. swap: swap space, the size is generally 1 to 1.5 times the real memory.
    The larger the value, it indicates that there are constant calls in the memory, reflecting the problem of insufficient memory.

    si(swap in) //Indicates how many pages are swapped from physical memory into swap, that is, the data rate at which data enters swap (kb/s)
    so(swap out) //Indicates how many pages are swapped into physical memory from swap, that is, the data rate at which data leaves swap (kb/s)
    • 4. io: write/output (in/out)
bi(block in) //Indicates how many disk blocks are transferred into memory, that is, the rate at which data is read from the block device to the system (kb/s)
bo(block out) //Indicates how many disk blocks are synchronized from the memory to the hard disk, that is, the rate of saving data to the block device (kb/s)
  • 5,system:

    in( interrupts) //Indicates the number of interrupts, that is, the interrupt rate (kb/s)
    cs(context switch) //Indicates the number of context switching, that is, the process switching rate (kb/s)
  • 6,CPU:

us //Indicates user space
his //Indicates the kernel space
id //Indicates free percentage
wa //Indicates the percentage of time spent waiting for IO to complete
st //Indicates steal, the time stolen by virtualization technology (such as running a virtual machine)

Five, control operations

1. Homework and session

  • Job control is a function of the shell, which allows a single shell instance to run and manage multiple commands.

  • The job is associated with each pipe entered in the shell prompt. All processes in the pipeline are part of the job and are members of the same process group. (If only one command is entered in the shell prompt, this command can be regarded as the smallest pipeline of commands. This command will be the only member of the job)

  • Only one job can read input and keyboard-generated signals from a specific terminal window at a time. The process belonging to the job is the foreground process of the controlling terminal.

  • 该控制终端的后台进程是与该终端相关联的任何其他作业的成员。终端的后台进程无法从终端读取输入或接收键盘生成的中断,但可以写入终端。后台中的作业可能已停止(暂停),也可能正在运行。如果某个正在运行的后台作业尝试从终端读取内容,则该作业将自动暂停。

  • 每个终端是其自身的会话,并且可以具有一个前台进程和多个独立的后台进程。一个作业只能属于一个会话,也就是属于其控制终端的会话。

2,作业分类:

  • Linux分为前台作业和后台作业

前台作业:通过终端启动,且启动后一直占据了命令提示符
后台作业:可以通过终端启动,但启动后,释放命令提示符,后续的操作在后台完成========此类作业虽然被送往后台运行,但其依然与终端相关,当终端关闭时改后台也随之停止。

  • 可以通过nohup command & 剥离与终端的关系。当终端停止运行时,作业依旧在后台进行。

3,后台作业

  • &挂起作业,后台运行,不占用前台bash窗口。

[root@localhost ~]# sleep 1000 &
[1] 1819

  • 1,//jobs命令用于显示当前所有的后台作业

[root@localhost ~]# jobs
[1]+ Running sleep 1000 &
//jobs命令的结果中
+ //命令将默认操作的作业
- //命令将第二个默认操作的作业

  • 2,//fg命令用于将”后台作业调至前台运行“

[root@localhost ~]# fg
//当只有一个后台作业时,直接使用fg命令,不跟任何参数即可将后台作业调至前台运行,但是当有多个作业时则必须跟上%+作业号,也就是上面命令执行结果中以[]括起来的数字。
[root@localhost ~]# jobs
[1]- Running sleep 1000 &
[2]+ Running sleep 500 &
[root@localhost ~]# fg %1
//使用ctrl+z可将前台进程发送到后台,此时作业将处于停止状态
[root@localhost ~]# fg %1
sleep 1000
^z
[1]+ Stopped sleep 1000

  • 3,//使用bg命令+作业号可使后台”已停止的作业重新运行“

[root@localhost ~]# bg %1
[1]+ sleep 1000 &
[root@localhost ~]# jobs
[1]- Running sleep 1000 &
[2]+ Running sleep 500 &

  • 4,//kill加上作业号可以手动杀死(终止)指定作业。

[root@localhost ~]# jobs
[1]- Running sleep 1000 &
[2]+ Running sleep 500 &
[root@localhost ~]# kill %1
[1]- Terminated sleep 1000

  • 5,//Nuhup:将进程挂在后台运行:跟随系统进程:
  • 命令实例:Nuhup sleep 600 & Insert picture description here
    Insert picture description here

六,进程间通信(IPC:inter Process Communication)

  • 进程间通信方式:

同一主机:-共享内存;-信号:Signal
不同主机:-rpc:remote procecure call;-基于socket(套接字)实现进程间通信。
套接字socket:IP+port
在终端上可以通过netstat use查看运行的进程IP和端口。


七,使用信号控制进程

  • 信号是传递至进程的软件终端。信号向执行中的程序报告事件,生成信号的事件可以是错误或外部事件(如I/O请求或计时器过期,或者来自于明确请求(如使用信号发送命令),或者来自于明确请求(如使用信号发送命令))

  • –event事件:下面列出系统管理员用于日常进程管理的基本信号。

  • 通过短名称(HUP)或正确名称(SIGHUP)指代信号。
    指定一个信号的方法:
    -信号号码(数字标识):kill -1
    -信号完整名称:kill -SIGKILL(信号)
    -信号简写名称:kill -TERM或kill -KILL或kill -INT或kill -HUP

  • 基本进程管理信号

信号编号 ID 短名称 定义 用途
1 HUP 挂起 让一个进程不用重启就可以重读配置文件,并让新的配置信息生效
2 INT 键盘中断 中断一个前台进程。ctrl+c就是用的SIGINT信号
9 KILL 中断,无法拦截 导致立即终止程序。无法被拦截、忽略或处理
15 默认值 TERM 终止 导致程序终止。和SIGKILL不同,可以被拦截、忽略或处理。要求程序终止的友好方式,允许自我清理

用户可以中断自己的进程,但只有root才能终止由其他人拥有的进程。

  • kill命令根据ID向进程发送信号。虽其名称为kill,但该命令可用于发送任何信号,而不仅仅是终止程序的信号
    //语法: kill [-SIGNAL] PID …
    //显示所有可用的信号(可使用man 7 signal查看帮助信息)
    Insert picture description here
  • //killall COMMAND:killall后面跟进程名,表示将匹配到的以进程名为名的进程全部杀死
    也可以使用kill 加进程ID

    Insert picture description here

八,监控进程活动

1,I/O负载

  • 负载平均值代表一段时间内感知的系统负载,Linux通过预期服务等待时间来表示实施平均负载计算

  • a,Linux不仅计算进程数,也作为独立的任务计算线程数。运行中线程和等待I/O资源的线程的CPU请求队列对应于R和D进程状态。等待I/O包括处于睡眠而等待预期磁盘和网络响应的任务。

  • b,负载数属于全局计数器计算,是所有CPU的总和数。由于从睡眠返回的任务可能会重新调度到不同的CPU,难以精确的每CPU计数,但累计数的准确度可以保障。显示的平均负载代表所有的CPU。

  • signal队列长度
    //查看cpu核心数
    [root@Eryuege ~]# grep ‘model name’ /proc/cpuinfo
    model name : Intel® Xeon® CPU E5-2650 v3 @ 2.30GHz
    [root@Eryuege ~]# grep ‘processor’ /proc/cpuinfo
    processor : 0

  • 一些UNIX系统仅考虑CPU使用率或运行队列长度来指示系统负载。由于具有空闲CPU的系统可能会因为磁盘或网络资源忙而遇到很长时间的等待,因此Linux负载平均值中包含了对I/O的考量。遇到负载平均值很高但CPU活动很低时,请检查磁盘和网络活动。(CPU使用率很低,但是负载平均值高,可能是磁盘或网络的读写过慢)

  • Linux中可以使用top、uptime显示负载平均值的情况。
    [root@Eryuege ~]# uptime
    09:14:12 up 4 days, 22:36, 4 users, load average: 0.00, 0.00, 0.09
    //此处的load average就表示负载平均值,这三个值代表最近1、5和15分钟的负载情况。

    • a,将显示的负载平均值除以系统中的逻辑CPU数。当值低于1表示资源利用率较好,等待时间很短。当值高于1表示资源饱和,而且有一定的服务等待时间。

    • b,空闲CPU队列的负载数为0。每个就绪和等待的线程使计数增加1。总队列数为1时,资源(CPU、磁盘或网络)正在使用中,但没有请求把时间花在等待上。增加的请求数会累积该计数,但由于许多请求可以在时限内处理,资源使用率会很高,而等待时间则不会。

    • c,因为磁盘或网络资源忙碌等待I/O而处于睡眠的进程包含在该计数内,而且使负载平均值增大。
      虽然不能表示CPU使用率,队列数依然表明用户和程序正在等待资源服务。

    • d,在资源饱和前,平均负载将保持在1以下,因为几乎不会在队列中发现等待的任务。只有资源饱和导致请求留在排队状态并且被负载计算例程计数时,负载平均才会增大。当资源使用率接近100%时,每个增加的请求将开始遭遇服务等待时间。

2,实时监控-TOP

  • top用于实现全屏动态显示系统信息(默认按照CPU的使用率排序)

  • //常用选项
    -d //设置延迟时长,top -d 1 表示每一秒刷新一次,默认每隔5秒刷新
    -b //批模式翻屏显示,默认只实时显示一屏,若要显示后面的进程信息则可使用-b选项,与-n #号合用

    Insert picture description here

  • load average:1分钟,5分钟,15分钟
    load average //CPU队列中等待运行的任务的个数

  • cpu(s):多颗CPU平均负载,按1键显示每颗CPU平均负载。

sy //表示内核空间;
us //表示用户空间;
Ni //表示调整nice值,CPU占用的比率;
id //表示空闲百分比;
wa //表示等待IO完成所占据的时间百分比;
hi //表示hard interrupt,硬件中断占据的时间百分比;
si //表示软中断占据的时间百分比;
st //表示steal,被虚拟化技术偷走的时间(比如运行虚拟机)
  • PR // 优先级

  • NI // nice值

  • VIRT // 虚拟内存集

  • RES // 常驻内存集

  • SHR // 共享内存大小

  • S // 进程状态

  • 进入top实时监控后的交互命令
    //top命令交互式子命令:

    M //根据驻留内存大小进行排序,默认根据CPU百分比排序
    P //根据CPU使用百分比进行排序
    T //根据累计时间(占据CPU时长)进行排序
    l //Whether to display average load and startup time
    t //Whether to display information about the process and CPU status
    m //Whether to display memory related information
    c //Whether to display complete command line information
    q //Exit the top command
    k //Terminate a process
    1 //Display all CPU information
    s //Modify the refresh interval

Guess you like

Origin blog.csdn.net/LBJ19224/article/details/109200590