Exploration of fork () and vfork () in linux

Source: Internet
Author: User
Both the fork function and vfork () function can be used to create a process fork (). an existing process can call the fork function to create a new process. A new process created by fork is called a child process ). The fork function is called once but returns two results. The only difference between the two responses is that both the subprocess fork function and vfork () can be used to create a process.
Fork () an existing process can call the fork function to create a new process. A new process created by fork is called a child process ). The fork function is called once but returns two results. The only difference between the two responses is that the child process returns 0 and the parent process returns the child process ID. A child process is a copy of the parent process. it obtains a copy of the data space, heap, stack, and other resources of the parent process. Note that sub-processes hold "copies" of the above bucket, which means that the parent and child processes do not share these buckets. Vfork () generates a new sub-process. however, the child process created by vfork shares the data segment with the parent process (refer to Baidu Library, but it also shares the heap, and the running results show that ), in addition, the child process created by vfork runs before the parent process. Example 1:
# Include
  
   
# Include
   
    
# Include
    
     
Int main (void) {pid_t pid; int _ num = 8; if (pid = vfork () <0) {// perror ("vfork is failed"); exit (1);} else if (pid = 0) {// run the sub-process printf ("this is child process \ n"); _ num ++; printf ("the num is: % d \ n ", _ num); exit (0);} else {//> 0 vfork () the returned value is the subprocess pid printf ("this is parent process \ n "); _ num ++; printf ("the num is: % d \ n", _ num);} return 0 ;}
    
   
  

Running result:


Example 2:
#include 
  
   #include 
   
    #include 
    
     int main(void){    pid_t pid;      int  _num=8;     if ((pid = fork()) < 0) {        perror("fork is failed");        exit(1);    } else if (pid == 0) {        printf("this is child process\n");        _num++;        printf("the num is :%d\n",_num);        exit(0);    } else {        printf("this is parent process\n");        _num++;        printf("the num is :%d\n",_num);      }       return 0;}
    
   
  
Running result:



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.