linuxday14--Process Management

Source: Internet
Author: User
Tags echo command network function save file terminates

Process-related concepts

function of kernel : Process management, file system, network function, memory management, driver, security function, etc.

Process: A copy of a running program that is a collection of instructions that is loaded into memory

Process id,pid number is used to mark individual processes

UID, GID, and selinux contexts determine access and access to the file system,

Typically inherit from the user who executes the process

Existence life cycle

Task Struct:linux data structure format for kernel store process information

Task List: A list of task structs consisting of multiple tasks

Process Creation :

Init: First Process

Parent-child Relationship

Process: all created by its parent process, CoW

Fork (), Clone ()

Process Memory :

Page frame: Pages box, storing page data, saving pages 4k

Lru:least Recently used uses least recent algorithms to free up memory

Physical address space and linear address space

Mmu:memory Management Unit is responsible for converting linear and physical addresses

Tlb:translation lookaside Buffer translation fallback buffer for storing cache of virtual address and physical address mapping relationships

IPC: Inter Process Communication

Same host: signal: Signal

Shm:shared Memory

Semaphore: Signal volume, a counter

Different hosts: Socket:ip and port numbers

Rpc:remote Procedure Call

MQ: Message Queuing, KAFKA,ACTIVEMQ

The basic state of the process

Create state: When the process is created, it needs to request a blank PCB (Process Control block), fill in the information of the control and management process, and complete the resource allocation. If the creation of the work cannot be completed, such as the resources can not be satisfied, it can not be scheduled to run, the status of the process is called the creation State

Ready state: The process is ready, assigned to the required resources, and can be run as soon as it is assigned to the CPU

Execution state: After the process is ready, the process enters the execution state

Blocking status: The process being executed is temporarily unable to run due to certain events (I/O requests, failed request buffers), and processes are blocked. Enter ready state to wait for system calls when the request is satisfied

Termination Status: The process ends, or an error occurs, or is terminated by the system and enters the terminating state. No more execution

Transitions between states six cases

Run - ready : 1, mainly the process consumes CPU time is too long, and the system allocated to the process CPU time is limited, 2, in the system using preemptive priority scheduling algorithm, when a higher priority process to run, The process is forced to give up the CPU, and the process transitions from execution state to ready state.

ready -to- run : The running process runs out of time slices and the scheduler goes to the ready queue to select the appropriate process to allocate the CPU

run -to- block : The executing process cannot execute due to a wait event, the process is changed from execution state to blocked state, such as an I/O request occurred

blocked - ready : The event that the process waits for has already occurred and enters the ready queue

The following two states are unlikely to occur:

Block-to-run: even if the blocking process is allocated CPU and cannot be executed, the operating system does not pick from the blocking queue when scheduling, but chooses from the ready queue

Ready-to-block: The ready state is not executed at all, not into the blocking state

Process priority

System priority: The smaller the number, the higher the priority

0-139 (centos4,5)

Each has 140 run queues and an expiration queue

0-98,99 (CENTOS6)

Real-time Priority: 99-0 Value Maximum priority

Nice value: 20 to 19, corresponding to system priority 100-139 or 99

Big O: The relationship between time complexity, timing, and scale

O (1), O (Logn), O (n) linear, O (n^2) parabolic, O (2^n)

LRU algorithm

Process status

Linux kernel: Preemptive multi-tasking

Process Type:

Daemon: daemon, process initiated during system boot, and terminal-independent process

Foreground process: terminal-related, process initiated through terminal

Note: The two can be converted to each other

Process Status:

Operating state: Running

Ready state: Readiness

Sleep state:

Interruptible: interruptable

Non-disruptive: uninterruptable

Stop state: Stopped, paused in memory, but not dispatched unless manually started

Zombie State: Zombie, end process, child process does not close before parent process ends

System Administration Tools

PS Command: Options: Default display of processes in the current terminal

Three options are supported:

GNU options such as--help

BSD options such as a

A option includes all processes in the terminal

The x option includes a process that does not link the terminal

The U option displays information about the process owner

The F option shows the process tree, which is equivalent to--forest

K|--sort Property Sort property, pre-attribute plus-Indicates reverse order

O Properties ... Options display custom (need to work with) default current terminal process

L display a list of supported properties

UNIX options such as-a-e

-c cmdlist Specify commands, multiple commands with, separate

-L Display Thread

-e: Show all processes, equivalent to-a

-F: Display full format program information

-F: Show process information in a more complete format

-H: Show process-related information in process-level format

-u userlist Specify a valid user ID or name Euser

-U userlist Specify a true user ID or name Ruser

-G gid or groupname specify a valid GID or group name

-G gid or GroupName specify a true GID or group name

-P PID display refers to the process of PID

--ppid PID display of sub-processes belonging to PID

-M displays selinux information, equivalent to Z

PS Output Properties

Vsz:virtual Memories SiZe, virtual memory set, linear memory

Rss:resident Size, resident memory set

STAT: Process Status

R:running

S:interruptable sleeping

D:uninterruptable sleeping

t:stopped

Z:zombie

+: Foreground process

L: Multithreaded Process

L: Memory Paging with lock

N: Low-priority process

<: high-priority process

S:session leader, session (child process) initiator

Ni:nice value

Pri:priority Priority level

Psr:processor CPU Number

Rtprio: Real-time priority

Common combinations :

Aux

-ef

-efh

-eo Pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,comm

Axo Stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm

Process Priority

Process Priority tuning:

Static priority: 100-139

The nice value is 0 when the process is started by default, and the priority level is 120

Only the root user can reduce the nice value (increase priority)

Nice command:

Nice [OPTION] [COMMAND [ARG] ...]

renice Command:

Renice [-n] Priority pid ...

View:

PS Axo Pid,comm,ni

Search Process

Most flexible: PS Options | Other commands

By pre-defined mode:pgrep

pgrep [Options] Pattern

-U uid:effective user, effective by

-U uid:real user, who actually initiates the run of the command

-T terminal: processes associated with the specified terminal

-L: Show process name

-A: Displays the process name in full format

-P PID: Displays the child processes of the specified process

By exact program name:/SBIN/PIDOF

View process Number

PIDOF Bash

System Tools

Uptime

Displays the current time, the system has started, the current number of people on the line, the average system load (1, 5, 10 minutes average load, generally no more than 1)

Average system load:

Average number of processes running in a queue during a specific time interval

Typically, the current number of active processes per CPU core is not greater than 3, so the system performs well. If the number of tasks per CPU core is greater than 5, then there is a serious problem with this host's performance

If the Linux host is a 1 dual-core CPU, the machine is fully used when load Average is 6

Top:

Sort by:

P: As percentage of CPU occupied,%cpu

M: Occupy memory percentage,%MEM

T: Cumulative CPU Duration, time+

The first message shows :

Uptime information: l command

Tasks and CPU information: T command

CPU Display: 1 (digital)

Memory Information: M command

Exit Command: Q

Modify Refresh time interval: s

Terminates the specified process: K

Save File: W

Introduction to field information

US: User space

SY: Kernel space

NI: adjust nice time

ID: Idle

WA: Waiting for IO time

Hi: Hard Interrupt

Si: Soft interrupt (mode switch)

ST: time the virtual machine stole

Options:

-D #: Specifies the refresh interval, which defaults to 3 seconds

-B: Show All Processes all

-N #: Refresh many times after exiting

htop command: Epel source

Options:

-D #: Specify the delay time;

-U UserName: Displays only the processes of the specified user

-S Colume: Sorting in the specified field

Sub-command:

S: Track system calls for selected processes

L: Displays the list of files opened by the selected process

A: Binds the selected process to a specified CPU core

T: Show Process Tree

Memory space

Memory Space usage Status:

Free [OPTION]

-B in bytes

-M in megabytes

-G in GB

-H easy-to-read format

-O does not display-/+buffers/cache lines

-T shows the sum of RAM + swap

-s n flush interval n seconds

-c n is exited after Flushing N times

vmstat Command: Virtual memory information

Vmstat [Options] [delay [count]]

Vmstat 2 5

procs:

R: Number of processes that can run (running or waiting to run), and the number of cores

B: Number of processes in non-interruptible sleep state (length of blocked queue)

Memory:

SWPD: Total amount of swap memory used

Free: Total Idle physical memory

Buffer: The total amount of memory used for buffer

Cache: The total amount of memory used for the cache

Swap:

SI: Data rate for swapping into memory from disk (KB/S)

So: Data rate from memory swap to disk (KB/S)

io:

BI: The rate at which data is read from the block device to the system (KB/S)

Bo: The rate at which data is saved to a block device

System:

In:interrupts interrupt rate, including clock

Cs:context Switch Process Switching rate

CPU:

Us:time spent running Non-kernel code

Sy:time spent running kernel code

Id:time spent idle. Before Linux 2.5.41, including io-wait time.

Wa:time spent waiting for IO. 2.5.41 before, including in Idle.

St:time stolen from a virtual machine. 2.6.11 ago, unknown.

Options:

-S: Show memory statistics

Iostat: Statistics CPU and device IO information

pmap Command: Memory mappings for processes

PMAP [options] pid [...]

-X: Display detailed format information

Another implementation:

Cat/proc/pid/maps

glances command: Epel source

glances [-BDEHMNRSVYZ1] [-B bind] [-C Server] [-C conffile] [-P port] [-p password] [--password] [-t refresh] [-f file] [- O Output]

Built-in commands:

A Sort processes automatically l Show/hide logs

C Sort processes by CPU% b Bytes or bits for network I/O

M Sort processes by mem% W Delete warning logs

P Sort processes by name X Delete Warning and critical logs

I Sort processes by I/O rate 1 Global CPU or PER-CPU stats

d show/hide disk I/o stats h show/hide

F show/hide File system stats T View network I/O as combination

n show/hide Network stats u View Cumulative network I/O

s show/hide Sensors stats Q Quit (ESC and ctrl-c also work)

Y show/hide hddtemp Stats

Common options:

-B: Display nic data rate in bytes

-D: Turn off the disk I/O module

-f/path/to/somefile: Setting the input file location

-O {html| CSV}: Output format

-M: Disable Mount Module

-N: Disable network module

-T #: Delay time interval

-1: Each CPU related data is displayed separately

Run the glances command in C/s mode

Server mode:

Glances-s-B ipaddr

IPADDR: Indicates which address of the listening machine

Client mode:

Glances-c ipaddr

IPADDR: Server-side address to connect to

dstat Command: System resource statistics instead of Vmstat,iostat

Dstat [-AFV] [options:] [Delay [Count]]

-C: Display CPU-related information

-C #,#,..., Total

-D: Show disk-related information

-D Total,sda,sdb,...

-G: Display page related statistics

-M: Display Memory related statistics

-N: Display network-related statistics

-P: Show process-related statistics

-R: Displays statistics related to IO requests

-S: Show swapped related statistics

--tcp

--udp

--unix

--raw

--socket

--ipc

--TOP-CPU: Show CPU-intensive processes

--top-io: Show most io-intensive processes

--top-mem: Show the most memory-intensive processes

--top-latency: Show the process with the most delay

Iotop

The Iotop command is a top class tool to monitor disk I/O usage iotop has a UI similar to top, including PID, user, I/o, process, etc. to see how each process uses IO

Iotop output

First line: Read and Write rate totals

Second line: actual read and write rates

The third line: The parameters are as follows:

Thread ID (switch to process ID by P)

Priority level

User

Disk Read rate

Disk Write rate

Swap swap percent

Percentage of Io Waits

Thread/Process Commands

Iotop Common parameters

-O,--only only shows the process or thread that is generating I/O, and in addition to the parameters, you can press O during the run

-B,--batch non-interactive mode, typically used to log logs

-N NUM,--iter=num set the number of monitoring, the default is unlimited. Useful in non-interactive mode

-D SEC,--delay=sec sets the interval for each monitor, defaults to 1 seconds, accepts non-shaping data such as 1.1

-P PID,--pid=pid specifies the monitored process/thread

-u user,--user=user specifies to monitor the I/O generated by a user

-P,--processes show only processes, default Iotop show All Threads

-A,--accumulated shows cumulative I/O, not bandwidth

-K,--kilobytes uses KB units instead of human-friendly units. Script programming is useful in non-interactive mode

Iotop Common parameters and shortcut keys

-T,--time plus timestamp, non-interactive non-modal

-Q,--quiet prohibit the first few lines, non-interactive mode, there are three ways to specify

-q Displays column names only on first monitoring

-QQ never display column names

-QQQ never show I/O totals

Interactive Buttons

Left and RIGHT ARROW keys: Changing the sort

R: Reverse Sort

o: Switch to option--only

P: Switch to--processes option

A: Switch to--accumulated option

Q: Exit

I: Change the priority of a thread

Process management Tools

Kill Command:

Send control signals to the process for process management, one number per signal, signal name beginning with sig (can be omitted), case insensitive

Show current system available signals: Kill–l,trap-l

Common signal: Man 7 signal

1) SIGHUP: No need to close the process and let it reread the configuration file

2) SIGINT: Abort the running process, equivalent to CTRL + C

3) Sigquit: equivalent to Ctrl+\

9) SIGKILL: Force kills a running process

SIGTERM: Terminating a running process

Sigcont: Continue Running

SIGSTOP: Background hibernation

How to specify the signal :

(1) Digital identification of the signal: 1, 2, 9

(2) Signal complete name: SIGHUP (3) The shorthand name of the signal: HUP

By PID:kill [-signal] pid ...

Kill–n SIGNAL pid;kill–s SIGNAL pid

By name: Killall [-signal] comm ...

by mode: pkill [options] Pattern

-signal

-U uid:effective user, effective by

-U uid:real user, who actually initiates the run of the command

-T terminal: processes associated with the specified terminal

-L: Show process name (Pgrep available)

-A: Displays the full format of the process name (Pgrep available)

-P PID: Displays the child processes of the specified process

  pidof Search Instruction process number

Job Management

Job Control for Linux

Front-end operation: Through the terminal start, and after the start has occupied the terminal;

Background jobs: Can be started through the terminal, but after the boot into the background to run (release terminal)

Let the job run in the background

(1) Operations in operation: CTRL + Z

(2) Jobs not yet started: COMMAND &

Background jobs are sent to the background, but they are still relevant to the terminal; exiting the terminal will close the background job. If you want to send back to the background, peel off the relationship with the terminal

Nohup COMMAND &>/dev/null &

Screen COMMAND

View all jobs at the current terminal:jobs

Job Control :

FG [[%]job_num]: Returning the specified background job to the foreground

BG [[%]job_num]: Allow jobs sent to the background to continue running in the background

Kill [%job_num]: terminates the specified job

Write standard output to the Nohub.out file

Nohup Ping 172.1 &

Task Scheduler

Linux Task Scheduler, recurring task execution

Perform a task at some point in the future

At

Package: at

At command: at [option] Time

Common options:

-V Displays version information:

-L: Lists jobs waiting to be run in the specified queue; equivalent to ATQ

-D: Deletes the specified job; equivalent to ATRM

-C: View specific job Tasks

-f/path/from/somefile: Reading a task from a specified file

-M: When the task is completed, the user will be sent a message, even if there is no standard output

Note: standard output and errors in the results of the job execution command are notified to the relevant user by mail

Time: Define when to do at this task

hh:mm [YYYY-MM-DD]

Noon, Midnight, teatime (4pm)

Tomorrow

Now+#{minutes,hours,days, or weeks}

Execution mode:

1) Interactive 2) input redirection 3) at–f file

Dependency and ATD Service, need to start to implement at task

The at queue is stored in the/var/spool/at directory

/etc/at. {Allow,deny} Controls whether the user can perform an at task

Whitelist:/etc/at.allow does not exist by default, only users in that file can execute the AT command

Blacklist:/etc/at.deny By default, denies users in the file to execute the AT command, and no user in the At.deny file can execute

If none of the two files exist, only root can perform an at command

Recurring Task Scheduler Cron

Related packages:

Cronie: Main package, providing Crond daemon and related auxiliary tools

Cronie-anacron:cronie Supplement to monitor Cronie task execution status, such as when a task in Cronie has not run correctly in the past, Anacron will then initiate this task

Crontabs: Includes centos to provide system maintenance tasks

Scheduled Tasks

System Cron Task:/etc/crontab

Comment Lines start with #

For details see man 5 crontab

# Example of Job definition:

#.----------------Minute (0-59)

# |.-------------Hour (0-23)

# | |.----------DAY of Month (1-31)

# | | |.-------month (1-12) OR jan,feb,mar,apr ...

# | | | |.----DAY of Week (0-6) (sunday=0 or 7) or Sun,mon,tue,wed,thu,fri,sat

# | | | | |

# * * * * * * user-name command to be executed

Example: Run the echo command at 9:10

* * * * centos/bin/echo "howdy!"

Time notation:

(1) Specific values

A value in the range of valid values for a given point in time

(2) *

All values in the range of valid values at a given point in time

Say "every ..."

(3) Discrete value

#,#,#

(4) Continuous value

#-#

(5) in the specified time range, define the step size

/#: #即为步长

@reboot Run once after reboot

@yearly 0 0 1 1 *

@annually 0 0 1 1 *

@monthly 0 0 1 * *

@weekly 0 0 * 0

@daily 0 0 * * *

@hourly 0 * * * *

Anacron system

Tasks that Cron does not run when the computer is running shut down, CentOS6 later versions cancel Anacron service, managed by Crond service

Useful for laptops, desktops, workstations, servers that occasionally shut down, and other systems that don't always boot

Configuration file:/etc/anacrontab, responsible for performing system tasks in/etc/cron.daily/etc/cron.weekly/etc/cron.monthly.

? Field 1: If these tasks are not running in these days ...

? field 2: Wait so many minutes after rebooting to run it

? Field 3: Task recognizer, identifying in the log file

? field 4: The task to perform

Executed by/etc/cron.hourly/0anacron

Update the timestamp of the/var/spool/anacron/cron.daily file when the task is performed

Command:

Systemd-tmpfiles–clean|remove|create ConfigFile

crontab Command definition

Each user has a dedicated cron task file:/var/spool/cron/username

crontab command:

crontab [-u user] [-l |-r |-e] [-I.]

-L: List All Tasks

-E: Editing tasks

-R: Remove All Tasks

-I: Use with-r to remove specified tasks in interactive mode

-U User: Only root can run, specify User management cron task

To control user execution of Scheduled tasks:

/etc/cron. {Allow,deny}

linuxday14--Process Management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.