Linux Multi-task programming: fork () function and its basic experiment

Source: Internet
Author: User

Source: CSDN Wang Wensong transferred from Linux commune

Fork () function

The only way to create a new process in Linux is to use the fork () function. The fork () function is a very important function in the Linux system, and the functions we have encountered before are distinguished by some, because it appears to execute once but returns two values, what is the explanation? Don't worry, look slowly.

Function description

The fork () function is used to create a new process from a process that already exists, a new process called a child process, and the original process is called the parent process. The child process that is obtained by using the fork () function is a replica of the parent process that inherits the address space of the entire process from the parent process, including the process context, code snippet, process stack, memory information, open file descriptor, signal control setting, process priority, process group number, current working directory, root directory, Resource constraints and control terminals, and the child process is unique only to its process number, resource usage and timers.

Because the child process is almost a full copy of the parent process, the parent-child process runs the same program. This requires a way to differentiate them and make them run like this, otherwise the two processes cannot do different things.

In fact, when the fork () function is executed in the parent process, the parent process copies a child process, and the code of the parent-child process runs concurrently in two address spaces, starting with the return of the fork () function, allowing two processes to obtain the return value of their respective fork () function, respectively. Where the return value in the parent process is the process number of the child process, and 0 is returned in the child process. Therefore, you can determine whether the process is a parent or child process by returning a value.

At the same time, it can be seen that the cost of using the fork () function is very large, it replicates most of the code snippets, data segments, and stack segments in the parent process, making the fork () function more expensive, and the execution Speed page is not very fast.

function syntax

The following table lists the syntax points for the fork () function

Basic experiments

The following is a basic experimental procedure for the fork () function

program code I upload to the resources, you can download, click here to download

Write it as a C file, and then enter the command: GCC fork.c-o fork

Then execute the command:./fork, you can see the results of successful execution

Of course, you can also use the command: ARM-LINUX-GCC Fork.c-o Fork Cross-compile and download to the board to run.

Linux Multi-task programming: fork () function and its basic experiment (GO)

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.