The fork () function of Linux specifically explains what the child process replicates the parent process

Source: Internet
Author: User

  1. #include <stdio.h> ??
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h> ??
  5. ??
  6. void ? Main ()??
  7. {??
  8. ???? char "Hello"
  9. ??
  10. ???? Pid_t?pid=fork ();??
  11. ??
  12. ???? if (pid==0)??
  13. ???? {??
  14. ???????? str[0]=' B ';? ?
  15. ???????? printf ( "Sub-process str=%s\n" ,str);??
  16. ???????? printf ( "The first address that Str points to in the child process:%x\n" int
  17. ????}??
  18. ???? Else ??
  19. ???? {??
  20. ???????? Sleep (1);??
  21. ???????? printf ( "Parent Process str=%s\n" ,str);??
  22. ???????? printf ( "The first address that Str points to in the parent process:%x\n" int
  23. ????}??
  24. }??
Str=bello in child process
the first address that Str points to in a child process: BFDBFC06?? the virtual address is taken here.
Str=hello in parent process

the first address that Str points to in the parent process: BFDBFC06? The virtual address is taken here.


Explanation: Using the "Write copy technique", the child process did not change the value of the data segment when it was first fork. The child process and the parent process are all code-shared, with the same physical address. The same virtual address,

When a child process changes a variable, the variable is actually copied to the child process and has its own physical address. But the virtual address is still the same, two of the same virtual address points to a different physical address.

So the above answer appears.


is the virtual address here the same? But the physical address is different.





The fork () function of Linux specifically explains what the child process replicates the parent process

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.