The fork() function is a fork function. An existing process can call the fork() function to create a new process. How do you call the fork() function under Linux? Or how to implement the fork() function under Linux? Come with Xiaobian.
1. The traditional fork () system call directly copied to the newly created process .Linux all the resources of the fork () copy (copy-on-write) achieved when using the page write. Write-on-write is a technique that can defer or even eliminate copying data. The kernel does not copy the entire process address space at this time, but allows the parent and child processes to share the same copy.
Only when you need to write, the data will be copied, so that each process has its own copy, that is, the copy of the resource is only done when it needs to be written. Before that, Just share it as read-only. This technique causes the copy of the page on the address space to be deferred until the actual write occurs. In the case where the page is not written (for example, exec() is called immediately after fork()), they do not need to be copied.
2.linux implements fork() by calling clone() on the system. Then do_fork() is called by clone().
The implementation of fork() function under linux:
Linux implements fork() through the clone() system call. This call specifies the resources that the parent, child process needs to share, through a series of parameter flags. The fork(), vfork(), and __clone() library functions call clone() according to their respective parameter flags. Then call do_fork() from clone().
do_frok does most of the work in the creation, it is defined in the ker/frok.c file. This function calls the function of copy_process() and then causes the process to start running. The work done by the copy_process() function is very interesting:
1. Call dup_task_struct() to create a kernel stack, thread_info structure and task_struct for the new process. These values are the same as the current process. At this point, the descriptors of the child process and the parent process are identical.
2. After checking the newly created child process, the number of processes owned by the current user does not exceed the limit of the resources allocated to it.
3. Now, the child process sets out to distinguish itself from the parent process. Many members of the process descriptor are cleared or set to their initial values. The member values of the process descriptor are not inherited, but mainly statistical information. Most of the data in the process descriptor is shared.
4. Next, the state of the child process is set to TASK_UNINTERRUPTIBLE to ensure that it will not be put into operation.
5.copy_process() calls copy_flags() to update the flags member of the task_struct. The flag of PF_SUPERPRIV indicating whether the process has superuser privileges is cleared to 0. Indicates that the PF_FORKNOEXEC flag of the exec() function has not been set by the process.
6. Call get_pid() to get a valid PID for the new process.
7. According to the parameter flags passed to clone(), copy_process() copies or shares open files, file system information, signal handlers, process address spaces and namespaces. In the second half of the day, these resources are shared by all threads of a given process; otherwise, these resources are different for each process and are therefore copied here.
8. Let the parent and child processes split the remaining time slices.
9. Finally, copy_process() does the finishing work and returns a pointer to the child process.
Go back to the do_fork() function. If the copy_process() function returns successfully, the newly created child process is awakened and put into operation. The kernel intentionally selects the child process to execute first. Because half of the child processes will immediately call the exec() function, which avoids the extra overhead of copy-on-write. If the parent process executes first, it may start writing to the address space.
The above is the introduction of the method of calling the fork() function in Linux. In addition to introducing the method of calling the fork() function in Linux, this article also introduces the implementation of the fork() function in detail. I hope that you have Helped.
We all know that Linux system startup is very fast, so what content does the Linux
Our disk space often takes a long time to become full, or only a small amount affects the speed of t
The installation of Ubuntu software can be easily installed through the software ce
Configuring swap space for computers can make full use of the system. When physical
How Linux prohibits specific ip address access
Linux view method of memory resource usage
Ubuntu install Nox, compile make error, what should I do?
Detailed method of configuring CentOS devices using Quagga software
Linux system how to set the read access of the .net IIS metabase
Knowledge about file I/O operations under Linux
Linux how to use git to check out a separate branch
What should I do if the Shell command cannot be used after the Linux system is installed?
Red Flag Linux System FTP Configuration Tips
Linux System Tmux Installation and Usage Guide
Win8.1 English fonts are not rendering problems
Fine-tuning Win8.1 this computer
Win8.1 Update can't start? Emergency command to help repair
Windows10 system click shutdown to become an automatic restart how to solve
Computer tracks are very annoying. Several ways to help solve
Win10 Preview 10120: Optimize Tablet Mode Start Screen Display
Move the taskbar to display more date information.
Thunder account forgot how to do Thunder account forgot to solve the solution