The usage and difference of fork and vfork

Source: Internet
Author: User

Fork and vfork are used to create processes

    1. The fork function is to replicate a process, and when a process invokes it, two almost identical processes appear, and a new process, called a subprocess, is obtained. The original process is called the parent process. A child process is a copy of the parent process where the child process obtains a copy of the data segment and the stack segment from the parent process, which needs to be allocated new memory, and for read-only snippets, typically accessed using shared memory.

#include <stdio.h> #include <sys/types.h> #include <stdio.h>int main () {         int count=0;        pid_t pid;         pid=fork ();     //  Create a sub-process          if (pid<0)         {                 printf ("error  In fork! ");                 exit (1);         }        else if (pid==0 )         {                 printf ("I am the child process,the count is %d,my process id %d,pid=%d\n ", ++count,getpid (), PID);                 exit (0);         }        else                 printf ("i am the parent process,the  Count is %d,my process id %d,pid=%d\n ", Count,getpid (), PID);         return 0;}

Operation Result:

I am The parent process,the count is 0,my process ID 2765,pid=2766

I am the child process,the count is 1,my process ID 2766,pid=0

The difference between the two processes is that their PID values are different. One of the wonders of the fork call is that he is only called once, but can return two times, and he may have a different return value in 3

In the parent process, fork returns the ID of the newly created child process

In a child process, fork returns 0

If an error occurs, fork returns a negative value

There are two reasons why fork can go wrong: one is that the current number of processes has reached the upper limit of the system, when the value of errno is set to Eagain, and the system has insufficient memory, then the value of errno is set to Enomem

2.vfork

As with fork, it is also used to create a new process. But there is a difference between vfork and fork. Fork to copy the data segment of the parent process, while Vfork does not need to completely copy the data segment of the parent process, and the child process shares the data segment with the parent process before the child process calls exec or exit. Fork does not restrict the execution order of the parent process, and vfork in the call, the child process runs first, the parent process hangs, and the parent process's order of execution is no longer restricted until the child process calls exec or exit.

Vfork to create a shared data segment between a child process and a parent process

#include <stdio.h> #include <sys/types.h> #include <unistd.h>int main (void) {         int count=1;        int  child;        printf ("before create son, the  Father ' s count is %d\n ", count);         child=vfork ();   //created a new process, at which time two processes are running         if (child < 0)         {                 printf ("error in vfork!\n");                 exit (1);         }        if (child==0)          {                printf ("This is son, His pid id %d and the count is %d\n ", Getpid (), ++count);                 exit (1);         }        else         {                 printf ("after son, this is father,his pid is %d and  The count is %d, and the child is %d\n ", Getpid (), Count,child);         }        return 0;}

Before create son, the Father ' s count is 1

This is son,his PID ID 2809 and the count is 2

After son, this is father,his pid are 2808 and the count is 2, and the-the-is 2809

The count value is modified in the child process, but the value for output count in the parent process is also 2, which indicates that the child process and the parent process are shared count, that is, the child process created by Vfork and the parent process are shared memory areas.

Also, a child process created by vfork causes the parent process to hang, unless the child process executes exit or Exec to invoke the parent process, for example:

#include <stdio.h> #include <sys/types.h> #include <unistd.h>int main () {         int count = 1;        int  child;        printf ("before create son,the  Father ' s connt is %d\n ", count);         if (! ( Child=vfork ()))         {                 int i;                 for (i=0;i<100;i++)                  {                         printf (" this is son,the i is&Nbsp;%d\n ", i);                         if (i==10) exit (0);                 }                 printf ("this is son, his pid is %d  and the count is %d\n ", Getpid (), ++count);                 exit (0);         }        else        {                 printf ("After  son,this is father,his pid id %d and the count is %d  and the child&Nbsp;is %d\n ", Getpid (), Count,child);        }         return 0;}

The results of the implementation are as follows:

Before create son,the father ' s connt is 1

This is son,the I am 0

This is son,the I am 1

This is son,the I am 2

This is son,the I am 3

This is son,the I am 4

This is son,the I am 5

This is son,the I am 6

This is son,the I am 7

This is son,the I am 8

This is son,the I am 9

This is son,the I am 10

After son,this are father,his PID ID 2720 and the count is 1 and the-the-is 2721

As you can see, the parent process always waits for the child process to finish before it starts executing.

This article is from the "canvas shoes can walk the cat step" blog, please be sure to keep this source http://9409270.blog.51cto.com/9399270/1869621

The usage and difference of fork and vfork

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.