LINUX multi-task Programming (II)-Fork () Functions and Basic Experiments

Source: Internet
Author: User

Fork () function

Create a new process in LinuxUniqueYou can use the fork () function. The fork () function is a very important function in Linux. It is different from the functions we have encountered before, because it looks like two values are returned after one execution, what is the explanation? Don't worry, take a look.

Function Description

The fork () function is used to create a new process from an existing process. A new process is called a child process, and the original process is called a parent process. The child process obtained by using the fork () function is a replica of the parent process. It inherits the address space of the entire process from the parent process, including the process context,CodeSegment, process stack, memory information, opened file descriptor, signal control setting, process priority, process group number, current working directory, root directory, resource limit and control terminal, etc, what a sub-process excludes is its process number, resource usage, and timer.

Because the child process is almost completely replicated by the parent process, the Parent and Child processes will run the sameProgram. This requires a way to differentiate them and make them run as needed. Otherwise, the two processes cannot do different things.

In fact, when the fork () function is executed in the parent process, the parent process will copy a child process, and the code of the Parent and Child processes is () the Return Value of the function starts to run in two address spaces at the same time, so that the two processes can obtain the return value of the fork () function, respectively, the return value in the parent process is the process number of the child process, and 0 is returned in the child process. Therefore, the return value can be used to determine whether the process is a parent process or a child process.

At the same time, we can see that the cost of using the fork () function is very high. It copies most of the code segments, data segments, and stack segments in the parent process, making fork () the system overhead of the function is large, and the execution speed page is not very fast.

Function syntax

The following table lists the syntax points of the fork () function.

Basic lab

The following is the basic experimental program of the fork () function.

The program code is uploaded to the resource. You can download it by yourself. Click here to download it.

 

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

Run the command:./fork to view the successful execution result.

Of course, you can also run the command: Arm-Linux-GCC fork. C-o fork, and then download it to the board to run it.

 

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.