Linux process status [zombie process, orphan process] + process priority
1. Process status
1. Process queuing
First, we need to explain the concept of queuing memory
2. Description of the process status in the textbook:
First we need to make two points clear:
The running queue is the running status:
Don’t use human feelings to evaluate the speed of the CPU, and the CPU is scheduled in round robin
Among them, the blocking state is like this:
At this time, the process is in a blocking state
After I input data from the keyboard a>
can continue to run downward
1. Blocking and suspending
3. Specific process status under Linux:
1. Process status array under Linux
This is the description of the process status from the Linux kernel source code
static const char * const task_state_array[] = {
"R (running)",
"S (sleeping)",
"D (disk sleep)",
"T (stopped)",
"t (tracing stop)",
"X (dead)",
"Z (zombie)",
};
Among them:
R: Running status
There are four blocking states:
S: Sleep state:
D: Sleep state:
T :Pause status:
t: Pause status:
There are two more states:
Z: Zombie state
X: Exit state
Let’s take a look at the various states in the Linux environment
2.R
Let’s take a look at the R status first:
Here we wrote a while(1) infinite loop code to look at the R status
When we Before exiting the process corresponding to mycmd
This process displays R+ status
3.S: Can terminate sleep - light sleep
What is sleep state?
The sleep state is a kind of blocking state
is a relatively common blocking state
The process needs to wait for certain software and hardware resources. Provided to enter the sleep state
It is the same as the description of the process state in the textbook we just mentioned
Why is it said that S can terminate sleep?
Because this sleep state can be exited by using ctrl+c
The following is for everyone Take a look:
sleep(1) is to let the program sleep for 1s
During this period, the process is in sleep state and waits for the 1s to pass
This process is always in S state. When we press ctrl+c, the process exits normally
What you said seems to be of no use. Is it still unable to exit under normal circumstances?
1. Supplement: foreground process and background process
Sometimes it’s really impossible to exit
I’ll give you a look earlier
Let’s introduce two concepts first
Foreground process: The process status is displayed as + and can be exited by ctrl+c
At this time, the command cannot be executed
Background process: There is no + when the process status is displayed, and it cannot be exited by ctrl+c
At this time, commands can be executed
Normally./可执行程序名字
we run it as a foreground process by default
but ./可执行程序名字 &
it will run as a background process Process mode to run the process
./mycmd 前台进程
./mycmd & 后台进程
For better demonstration, let's add a line of printf
This is running as a foreground process:
At this time, I entered the ls and pwd commands, but they were useless
and ctrl+c successfully exited.
This is run as a background process:
At this time, I entered ls and pwd commands and they all worked
However, ctrl+c cannot exit
You can only use kill -9 process PID To kill the process to exit
We will introduce the foreground process and background process in detail in the future
For now, you can learn about it in this blog
2. A "strange" phenomenon
Some attentive friends may have discovered a phenomenon:
Why did the process we printf print just now clearly print
but still display the S status? What?
Shouldn’t it display the R state?
Because:
The essence of printf is to print on the monitor and only a very low probability of seeing R So most of the time what we see is S, And most of the time it is interactive
printf causes the process to frequently interact with the monitor. When interacting, it is in S because it has to wait for the monitor.
It is for this reason that when we first introduce the R state, we only use a while(1) infinite loop.
4.D: Unterminating sleep—deep sleep
First let me explain something:
When the operating system is in a hurry (when resources are particularly tight), it will kill the process (at least this is the case under Linux)
Therefore, when our computer resources are particularly scarce, the operating system may kill some processes.
At this time, if a particularly important process is killed, it will have a very big impact on users. Therefore, the D state appears.
5.T
T: Paused state
Why is there a paused state?
Sometimes certain processes have dangerous operations. If you insist on executing these processes, the operating system will suspend the process to protect the computer.
First we need to introduce a few commands
kill -l:查看进程信号
kill -19 进程PID:暂停进程
kill -18 进程PID:让暂停进程继续运行
Let's demonstrate it below
Once the process is suspended, the process will automatically switch from the foreground process to the background process. Even if it continues to run later, it will still be a background process.
6.t
Why is there t state?
"t (tracing stop)"
Tracing stops
In fact, when we use gdb to debug the program, the process is in t state
Don’t forget, if you want to use gdb to debug an executable program
When compiling with gcc, you must add the -g option, that is, use debug mode to generate an executable program
Let’s demonstrate it now
When I set a breakpoint and start running, it enters the t state
7.Z
Z is a zombie state,
A process in a zombie state is a zombie process
We will explain it in detail below
8.X
Death state (exit status)
When a process wants to exit, first set it to the X state, and then exit the process
2. Zombie process
1. What is a zombie process?
2. Why do zombie processes exist?
2.Examples
The meaning of this code is:
Create a child process, and then the child process will exit after running for 5 seconds
The parent process keeps running, but The parent process does not receive the termination information of the child process
We know
A normal process in Linux has its parent process
Unless the process is created by the process we created
otherwise the process The father is bash
And bash will automatically receive the exit information of its child process
Can the parent process receive the exit information of the child process?
Of course it can
使用
wait(NULL);
等待子进程 会接收子进程的退出信息
头文件:
#include <sys/wait.h>
Let's demonstrate it below
The meaning of this code is:
The child process will exit after running for 5 seconds
The parent process will exit after running for 8 seconds, and the child process will receive the exit immediately after exiting. Information
then sleep for 5 seconds and then exit
After the child process exits, before the parent process receives the child process exit information
The child process has been in the zombie state
The parent process receives the child process exit information After
the child process exits successfully
3. The harm of zombie processes
3. Orphan process
1. The concept of orphan process
2.Examples
The meaning of this code is:
The child process keeps running in an infinite loop
The parent process exits after running for 5 seconds
After exiting, the child process becomes an orphan process
4. Process priority
1. Concept
In Linux
the priority is represented by an integer variable
and the default priority is 80
The entire priority range is [60,99] (including 60 and 99)
A total of 40 integers
The lower the number, the higher the priority
The higher the number, the lower the priority
2. Modify process priority
Linux system allows users to adjust the priority, but does not allow users to modify the priority directly
Instead, the priority is modified by modifying the nice value
ps -l 查看进程信息
Next we will modify the process priority
NOTE: We do not recommend adjusting priorities
The method of modifying the process priority is introduced here so that we can understand
if others modify the process priority in the future
One thing is to deepen our understanding
1.top command
top命令 修改进程优先级
按q键退出
新PRI=PRI(old)+nice
注意:
1.PRI(old)一直都默认是80
这样规定是为了让我们以后修改进程优先级的时候可以不用管上一次的优先级是多少
修改起来更加方便
2.因为PRI的范围是[60,99]
而PRI(old)一直都是80
所以nice的范围是[-20,19]
1.ps -la 查看进程信息
2.top
进入top之后,我们按r
然后输入我们想要修改的进程的PID
然后输入nice值
最后退出即可
Here I want to change the priority to 90
Modified successfully
2. Why does Linux restrict priority adjustment?
Any time-sharing operating system needs to perform fair scheduling in process scheduling!
3. Test process priority
Let’s test the upper and lower limits of the process priority
Adjust the priority of the mycmd process twice respectively
Adjust it to 0 for the first time (Input -80)
The second time it was adjusted to 200 (Input 120)
I was not allowed to adjust it to 0
Me I want to adjust it to 200, but the result can only be adjusted to 99
5. Process Scheduling and Switching
1. Switch
1. Concept preparation
2. Method of process switching
First of all, let’s give a small example in daily life
In fact, process switching is also done in this way
Process switching works like this:
3. Summary
Now look back at the library example
and you will have a deeper understanding of process switching
2. Scheduling
1. Scheduling features under Linux system
So how is the scheduling feature of the Linux operating system achieved?
This is closely related to the structure of the Linux run queue
Next we Let’s take a look at the structure of the Linux run queue
2. Run queue structure
At first glance, this structure seems troublesome
Don’t worry, we only need to care about a few important structural data
Next we will An introduction to how the structure of this run queue takes into account
priority, efficiency and hunger issues
3. Solve the priority issue
4. Solve the problem of hunger
The operating system quickly determines the number of processes in the queue array based on the value of nr_active
Based on this, it determines when the active pointer and expired pointer can be exchanged
When the time slice of a process is exhausted, the process has expired and will enter the expiration queue.
When a process finishes running, it exits the running queue and no longer enters the expiration queue. Instead, it becomes a zombie process, waiting for the parent process to read the end information.
5. Solve efficiency issues
But in this case, you have to traverse the queue array every time
And because your CPU process switches very frequently, can the efficiency be better< a i=2> This is the role of int bitmap[5];
The above is the entire content of Linux process status (zombie process, orphan process) + process priority + process scheduling and switching. I hope it can be helpful to everyone!