Article Catalog
I. Preface
Hello, everyone, in this article we are going to introduce about the process state under Linux
- In the previous post, we focused on the topic ofBasic Concepts of Processes under Linux, learned what a process is, how to describe and organize a process, and create a process.
- In this article, we will first understand the operating system discipline under the state of the process, the state of the process has a basic concept, and then it will go to learn Linux under the seven kinds of process state, to learn the PCB structure in the second member of the variable
- Identifier: A unique identifier that describes the process and is used to distinguish it from other processes.
- Status: Task status, exit code, exit signal, etc.
- Priority: Priority relative to other processes.
- Program Counter: The address of the next instruction to be executed in the program.
- Memory pointers: pointers to program code and process-related data, as well as pointers to blocks of memory shared with other processes
- Context data: data in the processor’s registers while the process is executing [hiatus example, to add map CPU, registers].
- I/O status information: Includes a list of displayed I/O requests, I/O devices assigned to the process, and files used by the process.
- Bookkeeping information: may include total processor time, total number of clocks used, time limit, bookkeeping number, etc.
- Other information
II. Process status under the operating system discipline
- For the process, it is the concept of the operating system, if you have studied the “operating system” of this discipline, it should be very clear for the process, then there are many states, if the beginning of the new contact partners must feel so many states to distinguish ah!
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/91b795475c1d4e0682471b06db7a9c0c.jpeg)
1、Operation status
The first thing we’ll talk about is the [running state], which is the most common
- First of all for a process, we know that it is composed of theKernel data structures + corresponding code and data So when there are multiple processes in the system, there are bound to be multiple structures; of course, we need to link and organize these processes into
- Then these processes are in a running queue, if we want to find a process in this queue, we only need to find the head of the process.Then we can dispatch a process accordingly, and put the code and data corresponding to this process to the CPU for execution.
- So each CPU goes about maintaining a run queue with a pointer to the head of the queue inside
head
is the [task_struct] of the first process, and the pointer to the end of the queue istail
It points to the last task_struct. So all we need to do to run a process is to put the process pointed to by head on the CPU.
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/84f43bad15d44b96a4540186631d4065.jpeg)
- But the CPU can not directly find a need to schedule the process to run, at this point it will need something called [scheduler], the CPU by finding the run queue, run queue to find the scheduler, and then the scheduler and then go to one by one scheduling need to run the process, then at this point all thescheduled、in the run queueThese processes are in a state we callOperational status R
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/829f4db59f0342a7a6b75bade17c016c.jpeg)
Q: As soon as a process puts itself on the CPU and starts running, does it keep putting itself down until it finishes executing?
- No, every process has one called:The concept of time slices! Its time is probably around 10 ms. So it’s not that a process is executing all the time, it’s that thisMultiple processes where all code is executed in a single time period — It’s called concurrent execution.
- So there must be a lot of processes.Action of being put up and taken down by the CPU — It’s called process switching.
2. Blocking state
After introducing the [running state], let’s talk about the [blocking state].
- For the blocking state, it is actuallyThe process of waiting for some resource to become readyFor example, if your current process initiates an IO request to the operating system, then the process will be suspended and will not be able to continue to run, but this is still too difficult to understand, so let’s take two examples to illustrate
- If your network suddenly breaks down, the process can not continue to carry out the task of downloading, at this time it can only be sidelined, the CPU will go to run other processes, and wait for this process is ready to continue to run the process!
- Going to the bank’s counter to make a deposit, but when it comes to you suddenly realize that the slip has not yet been printed, so you can only wait on the sidelines.
- Earlier we were talking aboutBasic Operating System Concepts When the operating system is to manage the underlying hardware, it is said that it needs to perform the action of [first describe, then organize], that is, to abstract this cold hardware into a structure type, the internal
type
Represents what type of hardware this is,status
represents what state the hardware is currently in within the operating system.wait_queue
This is a pointer to the waiting queue.
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/1e996b00d0964d8bbfd819163da1fb73.jpeg)
- Then we continue to talk about the operating system in the [process], now there is a process want to read the data in the keyboard, then it needs to hook up with the structure we have just described, because the operating system according to the structure along the architecture to access to the lowest level of hardware needs to be implemented, so the process will need to be in a waiting state, we can chain it into the [wait queue] We can chain it into a wait queue, which is what we just described as a
wait_queue
- A process can not be considered a queue, if there is another process to read the data in the keyboard, we need to chain it to the waiting queue. At this point, the state of the two processes isblocking state
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/02aebcbf06e84660a07128f0e8f9dbf9.jpeg)
- Then when the keyboard once there is data, we only need to put this process into the [run queue] we said above can be, then the CPU in the scheduling time can automatically go to the underlying device to read; if there is no input when the keyboard, and ultimately our process can only be in each of their respective waiting queues to wait, the process is in the waiting queue is called theblocking state
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/7f4e2393e03e4241bb6951908893eb33.jpeg)
3、Suspended state
Finally, let’s talk about a state called [hung state]
- For the operating system, we know that it is necessary to allocate memory resources for all current processes, in the above I only drew a waiting queue, and only two processes, if a waiting queue exists in more than one process, and through more than one waiting queue in the queue, then the operating system in the memory resources will appear [depleted] the issue of
- Then the operating system has to ensure that memory resources are saved under normal circumstances
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/9ab42e5142724f20b84196cac016345b.jpeg)
How are we going to save?
- As long as a process is not scheduled (running), then the current processCode and Data It’s just idle and unused. So the operating system finds a way to reserve the PCB for the kernel of this process, and then it willCode and Data Re-exchange to the peripheral among the [change out], then at this time there is only one PCB in the queue here, we call the above state of affairspending
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/72b118806e5b448cb648c8987aba2862.jpeg)
- When the process is ready, put the process into the run queue, and at that point consider putting theCode and Data Put it back in the process. [Switching in]
- A: When the code and data corresponding to a process are exchanged to a peripheral, the OS frees up the corresponding space internally, at which time the OS can re-give this extra space to other processes that need it, thus achieving the purpose of recycling system resources and saving system overhead.
After looking at the three basic process states above we can summarize, if we want to see what state the process is in then generally look at thisWhere processes are queued
- If it’s in the run queue, it’s in the
operational state
;- If it is in the blocking queue, it is in the
blocking state
;
III. 7 process states under Linux
After the introduction of the three main process states under the operating system discipline, we have a basic concept of the process state, then let us formally learn about the seven process states under the Linux system.Let’s start by summarizing and reviewing what you’ve learned above:
- If the current [operational state], then it needs to be scheduled to run next
- If the current [blocking state], then wait for the condition to be ready, and when the device is ready drop the current process into the run queue before it is scheduled to run by the CPU
- If the current [pending], all that has to be done is to replace the code and data that was swapped out at that time and re-swap it in, and then include the corresponding process in the run queue
static const char * const task_state_array[] = {
"R (running)", /* 0 */
"S (sleeping)", /* 1 */
"D (disk sleep)", /* 2 */
"T (stopped)", /* 4 */
"t (tracing stop)", /* 8 */
"X (dead)", /* 16 */
"Z (zombie)", /* 32 */
};
1. Operational status R
The first thing we’re going to talk about is [Running State R].
- Take a look at this code below, it’s a dead loop going
printf
Print Loop Statements
1 #include <stdio.h>
2 #include <unistd.h>
3
4 int main(void)
5 {
6 while(1);
7 {
8 printf("hello bit\n");
9 }
10
11 return 0;
12 }
- Then we run the following code, and when we observe the state of the process, we see that it reads
S+
But then what the reader wants to see should beR
(coll.) be the right thing to do
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/a7e5d3012bf94774b0dbc2602b4555fb.jpeg)
- Next, let’s modify the code a little bit and take a look at it again without using the
printf
print statement now, and it’s straightforward to use awhile(1)
Go for the loop.
1 #include <stdio.h>
2 #include <unistd.h>
3
4 int main(void)
5 {
6 while(1);
7 //{
8 // printf("hello bit\n");
9 //}
10
11 return 0;
12 }
- Then we see that the state of the process changes when we run it again at this point, and it becomes
R+
That’s the state of the process we want.
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/22bf1182e79d463ea6cd26b0d14f12c7.jpeg)
printf
What happens when the print statement is removed?
- Here’s why.
printf
Print statement it is one of the IO streams, for the first time because it is a loop, it keeps waiting for the IO device to be ready, so its process state is alwaysS+
which corresponds to the [blocking state] in the operating system; but when we remove theprintf
What happens after this IO flow, it’s just running pure and simple with no IO, and that also becomes theR
state of affairs
Here’s an additional note on thisS
andR
back+
- Here.
R+
This means that the process is running in the foreground, so it won’t be affected in any way by any commands we type.
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/9323b3348dc54516a3f2e17e40a83b47.jpeg)
- Then if we don’t start the process in the normal way, the state of the process will be different, as you can see I’ve done in the
./myproc
has been added to the end of the&
; then its state becomesR
This means that the process is running in the background.
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/34186b22eab24839908d4dfc56c3676d.jpeg)
However, the R state does not mean that the process is running, but that it is in the run queue.
2. Light sleep state S
Let’s move on to the sleep state under Linux S
- The code we’re going to run below prints a sentence every second
1 #include <stdio.h>
2 #include <unistd.h>
3
4 int main(void)
5 {
6 while(1)
7 {
8 sleep(1);
9 printf("hello bit\n");
10 }
11
12 return 0;
13 }
- After running, you can see that the process is in the state of
S+
sleep state
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/e2f529cd67c94abe8f4b68419c7ec6c6.jpeg)
R
What about the status?
- We can look at the moving image to see that every time we go to check the status of this process, it is in the
S+
This sleep state. In fact, it has something to do with the difference in speed between the peripherals and the CPU, the CPU that runs extremely fast! - Although we can see that the process on the right has been running every second, when we check the status of the process we see only
S+
; because there are99.99%
of the time is spent waiting, and only0.01%
of time in operation, there is literally an order of magnitude difference between them
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/568c054e14214f8699e73654660bd350.gif)
- However, this does not tell us whether a process is actually asleep, so let’s look at it again with a piece of code.
1 #include <stdio.h>
2 #include <unistd.h>
3
4 int main(void)
5 {
6 int a = 0;
7 printf("Enter# ");
8 scanf("%d", &a);
9
10 printf("echo : %d\n", a);
11 return 0;
12 }
- Running the process up we can see that it is motivated by the
S+
state, because [shell] is waiting for user input at this point, and this corresponds to what we talked about above with theblocking state
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/4486d738be094b54b24996e663c93d3b.jpeg)
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/588b2cdbc58b41fd9fdab34c098e5d3d.jpeg)
- Not only that, but the likes of the ones we’ve been using
bash
, which can also be considered a [blocking state] that keeps waiting for us to enter a command line, and once there it does the reading
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/114e9ba2e7bd4391acfc0ebe92c1c463.png)
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/15f7d7ab5b1d468087d4a8d3aa7ebf57.jpeg)
3. Deep sleep state D
In addition to [light sleep], there is also a kind of sleep called [deep sleep], and both of them, both areblocking state
- For light sleep, the reason why it is called“Shallow.”There is a reason for this: processes in this state are easy to wake up. For example, let’s say that the process we talked about above is in a blocking state, and we use the
kill -9 8664
Send [signal 9] to this process, then this process is killed, you can also think of it as being woken up
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/2093154b298a43a682bdea3a4deac3ca.jpeg)
Okay, I’m going to tell you a story to describe what kind of state this “deep sleep” is.
- Now there is a process in the operating system, it wants to write data to the disk, the disk this time heard its request, it began to write data, but we know that peripheral devices, especially such devices as the disk is relatively slow, so this time the process will be waiting for a long time!
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/747ab3f046f542a09c7ae54aa54fc368.jpeg)
- However, it is not possible to wait indefinitely. Because at this point in time there are already very many processes in the operating system, then the OS it can not see.While passing by, I saw that the current process was waiting and not running, so I terminated it without saying a word
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/76a5f531820146cebecd8f4f9e9212d2.jpeg)
- Then when the process is killed, the disk says: I had a problem writing the data. It goes back to the process that was scheduling it, but it finds that the process is gone? So the disk is embarrassed: what should I do? Write this data should not still put here [the general device will choose to lose]
- It’s fine if the data is ordinary, but what if it’s the kind of data that’s very important in a bank? What should we do?
An interesting lawsuit.
Because with the loss of this important data, a lawsuit was started.
- When we get to the courtroom, the judge starts asking questions, interrogating the three suspects, [operating system], [process], and [disk]… Whose problem do the readers think is at fault? Who is the pot in?
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/817aa3c0c75f436aac1f16c9eb4d7569.jpeg)
Here are the arguments of the three men.[OS]:
- Does it give me the right to manage its hardware and software resources? Did I kill a process under extreme circumstances? Am I fulfilling my role as an operating system? Is it my job to make sure the system doesn’t crash? What does data loss have to do with me?
- I am doing what my operating system should do, if you convicted me, then ask me the next time I encounter a similar situation, in the end I still do not do, if I do not kill, and ultimately lead to the operating system hang: ① data should be lost or have to lose; ② may also affect other processes. Who will take the responsibility for this?
The judge listened to this statement and thought about it, and he did. So this time he pointed his finger at the disk that had lost the data and asked: Why did you lose the data?[Disk]:
- Your Honor, you can’t blame me for this ╮(╯▽╰)╭ In this case I’m an errand boy, people let me do what I do, I’ve told the other side I’m going to fail when I write, I’m going to let him go and wait, I’m going to give it the results of my work pattern has always been like this, and other disks are like this as well
- If you think I’m guilty, isn’t there something wrong with the other disks as well? I lost it for a reason, other processes had to tell me to write data as well, so I went ahead and wrote it and reported it back, so how come the other disks didn’t have problems?
The operating system listens: Eh, what the bastard said does seem to be no problem. Then the perspective shifted to the side of the victim, i.e., the [process]. Anyway, the operating system is right, the disk is right, then it is your problem![Process]:
- Judge, I’m the victim /(ÊÊÊÊÊÊÊÊÊÊÊ)/~~ I just sat there quietly, “I’m sitting at home, the pot comes from the sky.” I was killed, how can I be at fault?
Then at this time the judge thought about it, and all three of them seem to have a pretty good point, am I wrong?
- In fact, all three are correct, the operating system fulfilled its duty to kill the process at the critical moment; while the disk played its instinctive duty to lose the data that no one wanted; the process was inadvertently killed by the operating system during the waiting period.
- When the process is waiting for the disk to write data, it should be protected from being killed by anyone.
Then we have mentioned above that processes that are in [light sleep] can bekill
off, then we have to make the state of this process change to [deep sleep] to be right, i.e.[D]
- Then since the state of this process is set to
D
Afterwards, while it was waiting for the disk to be written to, the operating system passed by and saw that the process was not running and waiting, so when it wanted to kill it, it was surprised by this “second to die gold medal”.
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/e123ffe0a72a4f06ae8353e1003689c0.jpeg)
- Then the process will not be killed, when the disk writes the data to inform the process, then it can put itself into the [run queue] to run, at this time its state becomes
R
the
- This student you ask a good question, indeed, this D state then the operating system is no way to kill it, but to wait until the disk writing is complete or what things to perform after the end of its automatically end the process, or in the external power outage, directly unplugged the power supply can be
- Generally speaking, if there is a D state in a system, the disk pressure in the system is already very big; if there are more than one D state, it means that the system is already very dangerous, and it is better to reinstall the system immediately.
But this one.
[D]
There’s no way to demonstrate this to the reader here, because D-state processes can only be demonstrated if they are in high IO. If you are interested, you can study the Linux commanddd
4. Stop state T
Okay, let’s talk about [stop state T].
- First of all, we have to check the corresponding process signals with the following command
kill -l
- Here we’re going to use signals 18 and 19.
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/9bad65cd4cc645cd81f54fd9da98734d.jpeg)
- Next, let’s try to see what it would look like to pause and restart the process.
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/a782a919fbbf44e8ab578f4140564251.gif)
- So let’s recap.
- Suspension of the process
kill -19 PID
- priming process
kill -18 PID
So now I have to ask, what is the difference between this T-stop state and S-sleep state?
- Stopped processesIt’s completely suspended.It won’t receive any more signals.
- One process can control another through the stopped state.
- S and D must be waiting for some kind of resourceThe T state may be waiting for some resource, or it may be controlled by another process.
5. Process tracking status t
Next, let’s talk about the tracking state of the process t. Remember what we learned in the Basics chapter of theGDB Debugging What? – I don’t know.
- First we look at the state of this process in the normal state, which is
S
Sleep state. Next I did [gdb] debugging, and after hitting a breakpoint on the line, I enteredr
Once it’s running, when we go back to check the status of the process, we see that its status becomest
,The reason for this is that the process stopped during debugging
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/0fc57f712af547d9b52d351e1c322d91.jpeg)
6. State of death X
What about [Death Status X], this status is just a return status, you won’t see this status in the task list 🙅
- The first way to kill this process is to send a signal 9 to the process
kill -9 PID
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/423d5d57f6b7482b8b0fb53974dce065.jpeg)
- The second method is to kill this process by its name
killall process name
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/3f172ccfcfdb4f2f867b3910314df339.jpeg)
7. Deadlock Z — two special processes
Next we will introduce a state called [deadlock state Z], for which we are going to involve two special processes calledzombie process together withOrphan processBut before we talk about this deadlocked state, let’s take a look at the
return 0
This thing.
- For this I believe the reader learns C by writing
main
function must have been written, but then why return this0
What, you may not be very clear, then now that we have learned the process state under Linux, we can give you to talk about it, the0
It’s called an exit code.
36 int ret = 20;
37 if(ret == 20) return 0;
38 else return 3;
- We let the process run and then pass the
echo $?
Went to check the exit code of the current process and found no problem it was0
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/bf5246debc5e46198b599fa6651ec6bc.jpeg)
- But at this point if I change the initial value to
30
If you run it again afterward, you will find that the exit code of the process has changed.
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/094d897652c542a1b8ea62d6901af628.jpeg)
① Zombie process
The first thing we’re going to introduce is the zombie process, and here’s how to introduce it through a story- You, on the other hand, love to run in the morning 🏃, and this morning you got up again at 6:00 a.m. to run, and when you passed a park, you met a morning walker [programmer], losing his hair as he ran But then, running, at this point suddenly he was
"Snap."
Down. Then you are very frightened at this point and immediately call the110
and120
The phone, and then guarded his side waiting for rescue to come, the surrounding people see also have surrounded, which can not help but some people will be emergency rescue. But after waiting for a while this person did not breathe
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/4da2b0be398c41f486c9880e9004ebb9.jpeg)
- After ten minutes or so, the ambulance and police car came, and the police blocked it off first, letting the forensic pathologist come over to examine its condition, and then said, “This person is hopeless, hurry up and notify the family to prepare for the aftermath~.”
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/d21d368bc2eb40c088a6692760444532.jpeg)
Okay, let’s get back to the point. Let’s talk about this [zombie process].
- Since the person actually died before the ambulance came, but its state has not been detected yet, and the cause of its death is not currently known, our operating system may maintain this current state, which is the
Z-state
namely[Stasis]
- A: The parent process! When a process exits, it’s the [parent process] that cares the most about it. Because the father process took a lot of effort to the child process!
fork
If it suddenly hangs, then the parent process must be concerned about the reason why the corresponding child process exited. - But we can’t go through this just with what we’re learning now, and when we get to the later
wait()
The system call then goes on to make certain introductions
It’s not enough to describe the above in cold words, so let’s look at it through a real case.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4
5 int main(void)
6 {
7 pid_t id = fork();
8 if(id == 0)
9 {
10 // child
11 int cnt = 5;
12 while(cnt)
13 {
14 printf("I am child, pid: %d, ppid: %d, cnt: %d\n", getpid(), getppid(), cnt);
15 sleep(1);
16
17 cnt--;
18 }
19 exit(0);
20 }
21 else
22 {
23 // father
24 while(1)
25 {
26 printf("I am father, pid: %d, ppid: %d\n", getpid(), getppid());
27 sleep(1);
28 }
29 }
30 return 0;
31 }
- If you run it, you can see that in the first 5s, the parent and child processes are still running at the same time, but when the child process exits at the end of the loop, the parent process is still running and has not exited. However, at this point the parent process is still running and has not exited. So when we look at the state of the two processes on the right hand side, the time has changed somewhat.
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/833ddad1fc62480597cbe16755e3e35b.gif)
- Take a closer look at this change of state, where [PID] is
5486
The parent process is5485
At the beginning, they were allS sleep state
But then the state of the process at the back end becomesZ-stasis
This means that the child process is dead, but the parent process doesn’t know it yet. This means that the child process is dead, but the parent process doesn’t know it yet. - This can also be accessed here via this right-hand side
<defunct>
This one, it means [invalid, no longer exists].
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/3b260b7080cc42038d600988f34002c9.jpeg)
For the above sub-processes, we will call it [zombie process], but this process is there is a certain harm!
- So a parent process that creates a lot of child processes and just doesn’t recycle them, isn’t that a waste of memory resources? Yes! Because data structure objects themselves take up memory, think about defining a structure variable (object) in C, it is necessary to open up space somewhere in memory!
- So for the above harm we call it [memory leak], and how to go about avoiding it, will be explained in a later article~
② Orphan process
- As we mentioned above, when a child process suddenly exits but the parent process does not take the initiative to reclaim it, then the child process will become a [zombie process].
- Then you can see below that after we terminate the process abruptly, both the parent and child processes are gone
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/891324c4c34e43df95ec8fbc3ea33774.jpeg)
Z
What about]?
- Because this process is currently
bash
child processes, when one of them exits, thebash
And it recycled it. But why is the child process gone? In this case, we need to talk about [orphan processes].
Next, let’s modify the code above to test the zombie process so that the parent process exits before the child process
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4
5 int main(void)
6 {
7 pid_t id = fork();
8 if(id == 0)
9 {
10 // child
11 int cnt = 500;
12 while(cnt)
13 {
14 printf("I am child, pid: %d, ppid: %d, cnt: %d\n", getpid(), getppid(), cnt);
15 sleep(1);
16
17 cnt--;
18 }
19 exit(0);
20 }
21 else
22 {
23 int cnt = 5;
24 // father
25 while(cnt--)
26 {
27 printf("I am father, pid: %d, ppid: %d\n", getpid(), getppid());
28 sleep(1);
29 }
30 }
31 return 0;
32 }
- Then through the process state tracking to see, we observed that at the beginning of the two parent processes and the child process is synchronized in the walk, but then after a while the parent process terminated, only the child process is still running, at this time we need to pay attention to not the process state, but the child process of the
PPID
i.e., the parent process’sPID
It has changed from [19956] to [1], why is that?
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/2a4bb427a92b4ba0b95b8260c72f82eb.gif)
- The words here should be popularized for the readers, for the
PID
A process with a value of 1 is generally referred to as a [system process]. We can use this command to find out
ps ajx | head -1 && ps ajx | grep systemd
- Then we see this [system process] of
PID
i.e. 1
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/eaf208c033a74db9b074c9947d92b66c.jpeg)
So why does the above happen? Did the child process suddenly switch parents?
- For the parent-child process, if the parent process ends before the child process, then the child process will be called [orphan process], for the orphan process, it will have the No. 1 process that is [the system process] adopted, because at this time the process does not have a parent process, so someone needs to lead it!
- In fact, it is very simple, since it is a process, it will eventually have to exit, but without the father it will have to be maintained by the system process.
Then we can actually explain the above.
- When this parent process exited, it was exited by the
bash
For the children of the parent process, as we have just mentioned, if the parent process of a process ends before the child process, then at this moment the child process immediately becomes an [orphan process], adopted and recycled by the system, so we can’t see the
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/61898a985ac443f392481070042b8985.jpeg)
IV. Summarizing and refining
Finally to summarize what we have learned in this article
- In this article, we have covered two major points, one is some of the process states that are common in operating systems, they are:Running, Blocked, Hanging status。
- The state of the scheduled processes that are in the run queue is called the
Operational status R
- The state of a process that is in a wait queue while waiting for the corresponding process of a peripheral is called a
Blocking state R
- When the data and code of a process are exchanged back to the peripheral, the process is in a state we call
Suspended state R
- The state of the scheduled processes that are in the run queue is called the
- Moving on, we covered the seven process states under Linux, which are:Running state R, light sleep state S, deep sleep state D, stopped state T, process tracking state t, dead state X, frozen state Z
- When a process is not waiting for an IO stream, it is in the
Operational status R
- utilization
sleep()
function works well to keep a process inLight sleep state S
- If you don’t want a process to be killed by the operating system while waiting for a disk operation, you can leave it in the
Deep sleep state D
- A [19] signal can be sent to a process to pause it, in a
Stop state T
Continuing to send the [18] signal will restart it. - Running a breakpoint in the [gdb] environment puts it in the
Process tracking status t
- utilization
kill -9 PID
It is possible to kill a process that is inDeath state X
- If a child process is allowed to exit when the parent process is not aware of it, it will be in the
Rigor mortis Z
If the parent process exits before the child process, the child process will become an orphan process.
- When a process is not waiting for an IO stream, it is in the
This is all that is going to be presented in this article, thank you for reading!
![Seven process states under Linux Seven process states under Linux](https://imgs.developpile.com/imgs/4c21f83ed0b541b3977b97402c439759.gif)