How are processes created? How did he end it? How does the process wait? Can the replacement process also be replaced? If you have these questions then this article will answer you
Article directory
process creation
To create a process there are the following ways
- Run directly (./run under Linux, click and run under windows and mac)
- Call the fork() function in the program to create a child process
So let's see how the fork() function creates a process at the bottom.
Knowledge review:
The function of the fork() function is to create a child process. The return value of the parent process is the pid of the created process, and the child process is 0.
ask
Why does the same variable have two different values?
answer
This involves the underlying layer, so let's look at the underlying logic of fork()
The underlying logic of fork
ask questions
Is the process (child process) created when fork() is executed to return pid? ?
answer
Of course it is created, otherwise where did the pid come from, then when he returns the pid, then there will be a problem, then it is the parent-child process, because, , so the return value
数据,代码是共有
here进程之间是相互独立的
is那么这里就会发生写时拷贝
different,且子进程是看不到(执行不到fork()前包括fork()的代码)
as the picture shows:
ask
The role of realistic copy? ? ?
answer
- Guarantee the independence of parent and child processes
- Save system memory resources (
子进程不一定会修改父进程的数据
) - Improve the success rate of fork
explain
Maybe 1 and 2 are easy to understand, but why does 3 improve efficiency? Because
fork是向os申请资源的创建进程
, if there is no realistic copy, then to ensure the independence of the process,为子进程开辟一块空间
the more resources you apply for, the greater the chance of failure.
process termination
How a process exits
- Run normally to return, or exit, _exit
- There is a problem with the code logic, and the operation crashes
- The external signal terminates the process (the signal blog later says that it is kill -9 at present)
Known as one of the many mysteries when learning C and C++ (return 0 is returned to whom)
retuen 0, it is actually there
程序退出码
, and this is one父进程看的
, the father process needs to know the exit status of the child process (you don’t need to know it)
ask
What if I also want an exit code?
answer
You need to enter on the command
echo $?
to get the latest exit code
knowledge supplement
Computers are good at dealing with numbers, but people are good at dealing with character strings. The corresponding relationship between numbers and characters (artificial regulations) is shown in the figure:
normal exit
- return
- exit
- _exit
The difference between return and eixt
return is used at the end of the function, and the program will exit only when the main function is executed, but exit and _exit are different
管在程序的那个位置只要执行到这个语句程序就会退出
The difference between exit and _exit
exit 会做后续工作而_exit不会
, the follow-up work (calling the destructor, refreshing the stream to the display...) is shown in the code:
The role of the exit code
In fact, it can be seen as
assert 当作一个预判
, where the logic is wrong, it can be terminated directly
process waiting
If the parent process of the child process does not reclaim resources, the child process is a zombie process, which will occupy system resources, so waiting is to deal with the zombie process
process wait function
- wait
- waitpid
Interface introduction
blocking and non-blocking
There is no need to introduce too much about blocking. After all, it often happens in life (
堵车
), but non-blocking is somewhat different. It can be understood that regular spot checks will not get stuck
Non-blocking running graph:
It is not difficult to see that this is one
循环的过程
, the parent process定期
checks the status of the child process, and when it is completed, it continues to execute its own code or end the process (类似你爸爸叫你好好写作业,害怕你玩游戏就定期过来看一下
)
code:
The result is obvious,
阻塞式等待不会执行waitpid后面的代码
non-blocking will execute concurrently with the child process
Get the exit code (status)
status is of int type, but only the lower 16 bits are effective bits as shown in the figure:
How to get the exit code
//位运算
cout<<(status>>8)& 0xff
//宏
WEXITSTATUS(status)
How to get the termination signal
//位运算
cout<<status & 0x7f;
//宏
WIFEXITED(status);
ask:
Does the exit code of the process matter if it is killed by a signal?
answer
It doesn't matter, because the program has not been executed normally, so what is the meaning of his exit code? right
process replacement
Generally, after fork(), you want the child process to execute the code of the parent process together. In fact, it does not make much sense. There is another way to execute the code of other processes, then you need to use the exec function to replace the program.
Interface of exec series
- l=list (list)
- v=vector (array)
- p=path (environment variable)
Replacement case:
ask:
Why haha~~ didn't print it? ? ?
answer
The functions of the exec series are program replacements, so they are directly replaced
数据和代码全部替换
, so of course the previous codes cannot be seen
as the picture shows:
memory pool
At the language level, such as c, c++, if it is to apply for a few bytes, it is okay if you do not apply frequently. But there will always be a situation where you need to apply for huge memory and frequent applications at once, so there will be a memory pool.
Example in life (bank loan)
If you want to start a business and need a loan, will you apply for the corresponding amount of money when you need it, or will you apply for a large sum of money first, just in case you need it? In fact, people find it troublesome. If you can do it once, you won't choose multiple times.
ask
After the parent process waits for the success of the child process, is the data of the child process really erased?
answer
In a sense, yes, but not, if
把这一块数据抹除会消耗很多系统资源
, so in general, the data is set as useless, and then linked with a data structure,
as the picture shows
end
That's all for this chapter, thank you for watching