A small thought of fork function in Linux

Source: Internet
Author: User

1. Fork function

Header file:

#include <unistd.h>

Function Prototypes:

pid_t fork (void); (pid_t is a macro definition whose essence is that int is defined in #include<sys/types.h>)

Return value: Two values are returned if the call is successful, the child process returns 0, the parent process returns the child process ID; otherwise, an error returns-1

Function Description:

An existing process can call the fork function to create a new process. A new process created by Fork is called a subprocess (child process). The fork function is called once but returns two times. The only difference of two returns is that the child process ID is returned in the parent process with a value of 0.

A child process is a copy of the parent process that obtains a copy of the parent process's data space, heap, stack, and so on. Note that the child process holds a "copy" of the above storage space, which means that these storage spaces are not shared between parent and child processes.

Linux copies the address space content of the parent process to the child process, so that the child process has a separate address space. After a successful creation of a new process, there are two fundamentally identical processes in the system, which do not have a fixed sequencing and which process first executes the process scheduling policy to look at the system.

Why fork return value has two:

In fact, the equivalent of a linked list, the process forms a linked list, the parent process's fork function returns a value that points to the process ID of the child process, because the child process has no child process, so its fork function returns a value of 0.


Here's a magical example:

#include <stdio.h> #include <unistd.h> #include <sys/types.h>int main () {int i=0;   for (i;i<2;i++) {pid_t id = fork ();   if (id==0) {printf ("i=%d,child,pid=%d,father,pid=%d,id=%d\n", I,getpid (), Getppid (), id);     } else {printf ("i=%d,father,pid=%d,grandfather,pid=%d,id=%d\n", I,getpid (), Getppid (), id);   Sleep (1); }} return 0;}

Input Result:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/82/FE/wKiom1doCyqxfSPUAABKFrxl3fY905.png "title=" screen Shot 2016-06-20 at 23.25.44.png "alt=" Wkiom1docyqxfspuaabkfrxl3fy905.png "/>

Analysis Process:


650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/82/FE/wKioL1doDtfTbDznAAHSalk_CrQ162.png "title=" screen Shot 2016-06-20 at 23.41.50.png "alt=" Wkiol1dodtftbdznaahsalk_crq162.png "/>

Each process will printf a sentence corresponding to a total of 3 new child processes, the parent process repeated printing 1 times, the child process repeated printing, so there is a total of 6 words printed.




This article from "Momo is spicy moe" blog, please be sure to keep this source http://momo462.blog.51cto.com/10138434/1791206

A small thought of fork function in Linux

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.