In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail what the fork function is, and the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Fork function
In linux, there is only one function that can create a child process: fork.
# include
# include
Pid_t fork (void)
A new process created by f o r k is called a child process. The function is called once but returns twice. The difference between the two returns is that the return value of the child process is 0, while the return value of the parent process is the process I of the new child process
D . The reason for returning child process ID to the parent process is that because a process can have more than one child process, there is no function so that a process can get the process ID of all its child processes. The reason why f o r k makes a child process get a return value of 0 is that a process will only have one parent process, so the child process can always call g
E t p p i d to get the process ID of its parent process (process ID 0 is always used by the swap process, so the process ID of a child process cannot be 0
).
The child process and the parent process share a lot of resources, and in addition to opening the file, many other properties of the parent process are also inherited by the child process:
Actual user ID, actual group ID, valid user ID, valid group ID.
Add group I D.
Process group I D.
Dialogue period I D.
Control terminal.
Settings-user-ID flag and setting-group-ID flag.
Current working directory.
Root directory.
Create shielded words in the form of files.
Signal shielding and arrangement.
The close flag at execution time for any open file descriptor.
Environment.
The connected shared storage segment.
Resource constraints.
The differences between parent and child processes are:
The return value of fork.
Process I D.
Different parent processes I D.
The child process's t m s _ u t i m e, t m s _ s t i m e, t m s _ c u t i m e, and t m s _ u s t i m e are set to 0.
The lock set by the parent process, which the child process does not inherit.
Pending alarms for child processes are cleared.
The pending signal set of the child process is set to an empty set.
The two main reasons for the failure of f o r k are: (a) there are already too many processes in the system (which usually means that something is wrong), or (
B) the total number of processes for the actual user ID exceeds the system limit. Recall tables 2-7, where C H I L D _ M A X specifies each actual user I
The maximum number of processes that D can have at any one time.
F o r k can be used in two ways:
(1)
A parent process wants to copy itself so that the parent and child processes execute different code snippets at the same time. This is common in network service processes-the parent process waits for a service request from the delegator. When such a request arrives, the parent process calls f o r k, causing the child process to process the request. The parent process continues to wait for the next service request.
(2)
A process executes a different program. This is a common situation for s h e l l. In this case, the child process calls e x e c as soon as it returns from f o r k.
From an example program, we can see the role of the fork function and the resource sharing between the child process and the parent process.
About what the fork function is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.