Python's OS module has a fork () function for generating child processes, the resulting child processes are mirrors of the parent process, but they have their own address space, and the child process copies a copy of the parent process memory to
Author: huaqing visionary lecturer
For fork This function believes that everyone is not unfamiliar. We knew he would create a subprocess, return two values, and fork would return two times. So here we are today to sort out our understanding of this
Fork () function1. Required header file:#include #include2. Function definitionpid_t fork (void);pid_t is a macro definition whose essence is that int is defined in #includesys/types.h>Return value: If successful call returns two values, the child
This article mainly introduces the fork () function generated in Python, analyze the order of execution of the subprocess and the parent process, and the friends who need it can refer to the following
Python's OS module has the fork () function for
This article mainly introduces the subprocesses generated using the fork () function in Python, and analyzes the execution sequence between the subprocesses and the parent process. For more information, see fork () in the python OS module () the
In the python OS module, the fork () function is used to generate sub-processes. The generated sub-processes are the mirror of the parent process, but they have their own address space, the sub-process replicates the parent process memory to itself.
Fork is one of the most difficult concepts to understand: it executes once but returns two values.First, let's look at the prototype of the fork function:# include # include pid_t fork (void);return value:Negative number: If there is an error, fork (
Fork () function in linux 1. the traditional fork () system calls directly copy all resources to the newly created process. in Linux, fork () uses the copy-on-write page. copy at write time is a technology that can delay or even avoid data copying.
1. Fork Function
# Inlcude
Pid_t fork (void );
A new process created by fork is called a child process ). The fork function is called once and returns two times. The only difference between the two responses is that the return value of the child
The meaning of fork
The following figure is the storage space layout of the C program (typical)
1. An existing process can invoke the fork function to create a new process.The 2.fork function is called once, but returns two times, and the only
Fock means to copy a process, that is, to load the current program again. The difference is that after loading, all States are the same as the current process (including variables ). Unlike a thread, the Fock does not need to provide a function as
Python's OS module has a fork () function for generating child processes, the resulting child processes are mirrors of the parent process, but they have their own address space, and the child process copies a copy of the parent process memory to
Linux under How many "-" will be printed:
12345678
int main ( void int i; for (i=0;i fork (); printf ( } return 0;
When I=0, the main process and the child processes it creates are printed
Nreturn Value:The fork function is called once, but returns two times: 0 is returned in the child process, the child process ID is returned in the parent process, and the error returns-1. By returning a value, you can determine whether it is in a
Fork () function function--Create a new process1. The parent-child process has separate data segments, heaps, stacks, shared code segmentsEach process in Linux has a 4G virtual address space (separate 3G user space and shared 1G kernel space), and
The following is an instance of the fork function.
[Cpp]# Include # Include # Include Int glob = 6;Char buf [] = "a write to stdout \ n "; Int main (void){Int var;Pid_t pid; Var = 88; If (write (STDOUT_FILENO, buf, sizeof (buf)-1 )! = Sizeof (buf)-1
1. Fork system call
Contains the header file and Function: create a sub-processFunction prototypePid_t fork (void );Parameter: No parameter.Return Value:If a child process is successfully created, the child process ID is returned for the parent
Transferred from UNIX environment advanced programming + personal supplement
Calling the f o r k function by an existing process is the only way for the u n I X Kernel to create a new process (this is not applicable to the preceding SectionAnd the
PS: Every blog but in order to record the process of learning, and reflect on the summary, if there are errors, but also to correct .Function prototype: extern __pid_t fork (void) __thrownl;The function is included in the header file unistd.h.Gaze
1#include 2#include 3#include 4#include 5 intMain ()6 {7 intCount=0;8 pid_t pid;//There is only one process at this time9Pid=fork ();//Create two processes at this timeTen if(pid0) One { Aprintf"Error in fork!"); -Exit1);/*fork Error
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.