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

Source: Internet
Author: User
Tags resource root directory

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 a Linux system, which is different from the one we've encountered before, because it looks like it's executed once but returns two values, what's the explanation? No hurry, 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 subprocess, and the original process is called a parent process. A child process obtained using the fork () function is a replica of the parent process. It inherits the entire process's address space from the parent process, including the process context, code snippet, process stack, memory information, open file descriptor, signal control settings, process priority, process group number, current working directory, root directory, Resource constraints and control terminals, and the child processes are unique only to its process number, resource usage, and timers.

Because the child process is almost completely replicated by 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.

is actually executing the fork () function in the parent process, the parent process replicates a child process, and the code for the parent-child process runs concurrently in two address spaces, starting with the return of the fork () function, so that two processes get the return value of the fork () function that they belong to. The return value in the parent process is the process number of the child process, and returns 0 in the child process. Therefore, a return value can be used to determine whether the process is a parent or a child process.

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

function syntax

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

Basic experiment

The following is the basic experimental program 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 the successful execution of the following diagram

Of course, you can also use the command: ARM-LINUX-GCC fork.c-o Fork can be downloaded to the board to run after the cross compilation.

Related Article

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.