当前位置:网站首页>Role of wait function
Role of wait function
2022-06-24 21:13:00 【The smell of tobacco】
Preface
Writing C When it comes to programming , adopt fork Function to create a new process , wait Function to wait for the child process to end .
So there's a problem , Under what circumstances does the parent process need to wait for the child process to finish before continuing execution ? If you need to wait for the child process to finish , If you put the operation directly into the parent process for execution, you will wake up ? Anyway, waiting is also waiting .
Of course , There is another situation , Mission A And tasks B Is a prerequisite for subsequent operations , And the two tasks can be carried out in parallel , At this point, the parent process can indeed execute a , The subprocess executes a , Then the parent process waits for the child process to complete before continuing . however , I still think this explanation is not so perfect . This can be achieved through process communication .
therefore , I think wait Functions must have other uses .
This question was answered after I rerouted the operating system .
Why?
as everyone knows , Processes exist in the process of running in the operating system PCB( Process management block ) Modular . It is the unique identifier of the process , It is generally kept PID、 Running state 、 Resource information wait .
The process is calling exit When the function exits , Will recycle the occupied resources . Be careful , stay exit Function to reclaim resources , The process is still running . therefore , There is a resource that cannot be recycled by the process itself , Namely PCB. To run a process, you need PCB, and PCB The premise of recycling is that the process is no longer running . in other words , The process itself cannot recycle itself .
that PCB You can't recycle it ? Of course not. , The wait The function came out . Since the process itself cannot reclaim itself , Then the parent process is responsible for recycling . therefore , Such a function that waits for the child process to end has a place to play .
By the way , In the subprocess exit end of execution , PCB The time before recycling , The state of a child process is called Zombie state , It's very vivid .
mac Process in :

debian Process in :

You can see , PID by 0 The process of is the parent process of all processes . stay mac in , All processes are controlled by /sbin/launchd Create , debian Medium is kthreadd. Their role is to manage and schedule processes . Parent process passed fork Create child process , After that, the subprocess passes exec Change to the process to be executed .
And this PID by 0 The process of , It is also the only way that the system does not run through fork Process created .
however , There's another problem , That is, if the parent process does not wait for the child process to end ? At the end of the subprocess , It is found that the parent process has long ended , Then it's PCB Can't recycle ? Of course not , This PID by 0 Ancestral process of , Otherwise known as root The process will regularly scan all zombie processes , And recycle it instead of its parent process .
After knowing it, I suddenly realized , そうですね!!!
Original address : https://hujingnb.com/archives/733
边栏推荐
- Simpledateformat thread unsafe
- Splicing audio files with ffmpeg-4.3
- Network flow 24 questions (round table questions)
- database/sql
- Geek University cloud native training camp
- Sleep revolution - find the right length of rest
- JMeter response assertion
- The JS method parameter passed a number beginning with 0. A magical problem occurred and bothered me for a long time
- JMeter installation plug-in, adding [email protected] -Perfmon metric collector listener steps
- Static routing job
猜你喜欢
![[普通物理] 光栅衍射](/img/f3/965ff7cd3bb76b4f71b69b9d12ece3.png)
[普通物理] 光栅衍射

Intermediary model -- collaboration among departments

Grating diffraction

Background of master data construction

Network security review office starts network security review on HowNet

Simple analysis of WordPress architecture

The Google File System (GFS) learning notes

Leetcode(135)——分发糖果

Rip/ospf protocol notes sorting

Appium introduction and environment installation
随机推荐
Undo log and redo log must be clear this time
Realization of truth table assignment by discrete mathematical programming
伯克利、MIT、劍橋、DeepMind等業內大佬線上講座:邁向安全可靠可控的AI
Open function
Time standard and format
Visitor model -- generation gap between young and middle-aged people
NPM download speed is slow
Builder mode -- Master asked me to refine pills
Leetcode(455)——分发饼干
Basic concepts and definitions of Graphs
What are the problems with traditional IO? Why is zero copy introduced?
Wechat applet custom tabbar
Splicing audio files with ffmpeg-4.3
[performance tuning basics] performance tuning strategy
Combination mode -- stock speculation has been cut into leeks? Come and try this investment strategy!
The difference between RPC and restful
Common data model (updating)
Leetcode(146)——LRU 缓存
Image panr
Where is 5g really powerful? What is the difference with 4G?