Fork and Vfork issues

Source: Internet
Author: User

Vfork and Fork
Two functions are used to create a child process.
Difference: 1. Vfork before the child process calls exec, it is in the space of the parent process.

2. Vfork guarantees that the child process runs first and the parent process starts running after calling exec.



#include <stdio.h> #include <stdlib.h>int main (int argc, char *argv[]) {    int pid,varble = 0;    if (PID = Vfork ()) < 0) {        printf ("Vfork error!\n");    } else if (PID = = 0) {
<span style= "white-space:pre" ></span>varble =;        Exit (0);    }    printf ("pid =%d,var =%d\n", Getpid (), varble);    return 0;}

Code Problem Resolution:

1. exit (); correct execution

2. return 0; The return stack is faulted because the process stack is shared. The program is out of chaos.


Fork and Vfork issues

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.