Create process fork

Source: Internet
Author: User
Name: fork
Role: Create a process
File: # include <unistd. h>
Definition: pid_t fork (void );

Note: fork () generates a new sub-process. Its sub-process copies the data and stack space of the parent process and inherits the user of the parent process.Code, Group code, environment variables, opened file code, working directory, and resource restrictions. Linux uses the copy-on-write (COW) technology. Only when one of the processes tries to modify the space to be copied can the real copy action be performed, because the inherited information is copied, it does not mean the same memory space. Therefore, the modifications made by the sub-process to these variables and the parent process are not synchronized. In addition, the child process does not inherit the file lock and unprocessed signals of the parent process. Note: Linux does not guarantee that the child process will be executed earlier or later than the parent process. ThereforeProgramPay attention to the occurrence of deadlocks or competitive conditions.

Return: If fork () is successful, the new Child Process Code (PID) is returned in the parent process, and 0 is returned in the newly created child process. If fork fails,-1 is returned. The cause of failure is stored in errno.

Error: The eagain memory is insufficient. The enomem memory is insufficient and the data structure space required by the core cannot be configured.

Code:
Pid_t PID;
PID = fork ()
If (pid = 0)
{
/* Child process */
}
Else if (pid> 0)
{
/* Parent process */
}
Else
{
/* Fork Error */
}

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.