Linux-fork () function, with code comments

Source: Internet
Author: User

////main.c//Project_c////Created by Lijinxu on 16/8/13.//copyright©2016 year Lijinxu-neu. All rights reserved.//#include<stdio.h>intMainintargcConst Char*argv[])    {pid_t fpid; /*Test Fork Part 1*/        intCount =0; printf ("The main process ' s ID is%d\n", Getpid ()); Fpid=Fork (); //1.fork://Fork creates a process that is almost identical to the original process through a system call.    However, depending on the parameters, two processes can also perform different functions. //after a process calls fork, the system assigns resources, such as data storage and code space, to the new process first. Then copy the original values (not exactly the same)//The fork call returns two times at a time:printf"fpid:%d\n", Fpid); if(Fpid <0)//Fpid < 0: Error occurredprintf"Error in fork\n"); Else if(Fpid = =0){//Fpid = = 0: Child process returned 0printf"Child process ' s ID is%d\n", Getpid ()); printf ("My Parent process ' s ID is%d\n", Getppid ()); Count+=1; }Else{//fpid > 0: Parent process returns the ID of the newly created child processprintf"Parent process ' s ID is%d\n", Getpid ()); printf ("My Parent process ' s ID is%d\n", Getppid ()); Count+= -; }    //2.fpid: In fact, the relationship between processes, like a linked list.    The parent process returns the ID of the child process that was pointed to, and no process after the child process returns 0. //3.getpid: Each process has a unique (non-identical) process identifier, which can be obtained through GETPID (). //3.1.getppid: Gets the PID of the parent process. //4. Execution order: There is no fixed order of execution, sequencing according to the system's process scheduling strategy. //5. Interpretation of the results of implementation: 4868-4867 (PAR) (Main), 4870 (child), 0. //when the printout is printed, it is found that the PID = 1 of the parent process is printed in the child process (as indicated by the previous result: it should be 4867). This is because the parent process (main process) has exited after execution, and the parent process of the child process is set to P1,p1 never die .printf"Count =%d\n", Count); /*Test Fork Part 2*/     for(inti =0; I <3; i + +) {Fpid=Fork (); if(Fpid = =0) printf ("child\n"); Else if(Fpid >0) printf ("father\n"); Else{printf ("error\n"); }    }    /*i = 0, 1, 2 father father father son son                       Father son son father father son Son father son sum up the law, for this n-cycle situation, the number of times to execute the printf function is (1+2+4+......+2n-1), the created Wahabbi          The number of threads is 1+2+4+......+2n-1. */    return 0;}

The following transfers from http://blog.csdn.net/jason314/article/details/5640969

Another code is as follows:

 for (i=0;i<2; i++) {      pid_t fpid=fork ();   after execution, i=0,fpid=3225      if(fpid==0)         printf ("  %d child  %4d%4d%4d/n", I,getppid (), Getpid (), fpid);       Else          printf ("%d parent%4d%4d%4d/n", I,getppid (), Getpid (), fpid) ;  }  

Clearly see the relationship between processes:

Linux-fork () function, with code comments

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.