Basics of Computer Operating System (5)---Linux Process Management

introduction

This article is the fifth chapter, Linux process management . This article mainly introduces the concepts of Linux process and related commands for operating Linux process.

1. Related concepts of Linux process

1. The type of process

(1) Foreground process

The foreground process is a process that has a terminal and can interact with the user

Below is a program that calculates pi in an endless loop

Then run the program. Although the program does not enter anything, it occupies the entire terminal shell. At this time, it is useless to enter any commands. This process is a foreground process.

Now change the code just now to make it print hello world continuously

Run the code at this time, it will output hello world in an endless loop. This is a foreground process with output. The output of this foreground process with output occupies the entire terminal shell , even if any command is entered, it is useless. Because this foreground process occupies the terminal shell

(2) Background process

  • Compared with the foreground process, the background process is the one that does not occupy the terminal
  • Background process basically does not interact with the user , the foreground process priority than low (because the foreground process to interact with the user, it is necessary in response speed or elsewhere to keep up, making it more smoothly when interacting with the user)
  • Background process we generally end with the & symbol, so that we can start a background process

Example:
Now change the previous code, let it output a hello world every 5 seconds, and then let it run as a background process

At this point, we can see that when we run it as a background process, it will output a process ID. Moreover, this background process does not occupy the shell. Although its content will be printed to the terminal, we can still Normally use the command. At the same time, if we use Ctrl+C, this background process will not be stopped.

(3) Daemon

  • The daemon is a special background process
  • Many daemons are started when the system boots and run until the system is shut down
  • Linux has many typical daemons

For example, crond, this is the timed task daemon in Linux, httpd is the daemon of the http service in Linux, sshd is the daemon when we log in using ssh, and mysqld is the daemon of the database (process names ending in d are generally Daemon)

2. Process marking

(1) Process ID

  • The ID of the process is the unique mark of the process, each process has a different ID
  • The process ID is represented as a non-negative integer , and the maximum value is limited by the operating system
  • We can use the top command to view all processes in Linux, the PID column on the left is the process ID

The operating system provides the fork() function to create a process, so the following situation may occur

The above is the hierarchical relationship of processes. This middle-level relationship is called the parent-child process relationship . Process A creates process B through the fork function, so process A is the parent process of process B. The parent-child relationship of the process can be viewed through the pstree command

Introduce a few special processes

The process with ID 0 is the idle process, which is the first process created by the system

The process with ID 1 is the init process, which is a child process of process 0 and is responsible for completing the initialization of the system (init process is the ancestor process of all user processes)

(2) Process status mark

In Linux, you can use the man ps command to understand the flags of various process states. Here are just a few more important ones for introduction

Now run the code of the infinite loop calculation of pi just written above as a background process, and run the code of a dead loop sleep as a background process, and then look at the status flags of the two processes

At this time, the two processes are running and sleeping respectively

2. Relevant commands for operating Linux processes

ps command (mainly used to view the process)

top command (mainly to view the memory used by the Linux process or some other status)

kill command (mainly used to send signals to the process)

1. The ps command

(1) List the current process: ps

(2) View process details: ps -aux

(3) View the processes of the specified user: ps -u username
View the processes of all root users: ps -u root

(4) View the specified process: ps -aux|grep specified process information

(5) View the process tree (view the parent-child relationship of the process): ps -ef --forest

(6) Sort the processes according to the frequency of using the cpu: ps -aux --sort=pcpu

(7) Sort the processes according to the size of the used memory: ps -aux --sort=pmem

2. The top command
(1) View the status of all processes: top

(PR is the priority of the process, VIRT is the virtual memory of the process, TIME+ is the time the process is running, and COMMAND is the command of the process)

3. Kill command

(1) Send a signal to the specified process: kill-signal process ID

Send 9 signal to the specified process ID: kill -9 23

9 This signal indicates that the process is terminated unconditionally. For more signals, you can use the kill -l process to view

It is the core competitiveness of a technical person to find the constant in the rapidly changing technology. Unity of knowledge and action, combining theory with practice

Standing on the shoulders of giants and learning, paying tribute to the predecessors

Reference: https://coding.imooc.com/class/355.html

Guess you like

Origin blog.csdn.net/self_realian/article/details/106997677
Recommended