The principle and usage of deep analysis fork ()

Source: Internet
Author: User

We all know that through the fork () system call we can create a new process that is as impressive as the current process. We usually refer to the new process as a child process, and the current process is called the parent process. The child process inherits the entire address space of the parent process, including the process context, the stack address, and the memory Information Process Control block ( PCB) and so on.

  1. Parent-Child processes

So let's first talk about the differences between parent and child processes:

    • The parent process has a lock set and the child process does not inherit
    • Process ID is different
    • Pending alarms for child processes are cleared
    • The pending signal set of the child process is set to the empty

  2.fork System Call Description

With the man manual we can easily know that the fork () contains the header files <sys/types.h> and <unistd.h> the function is to create a child process. Functional prototype: pid_t fork (void), pid_ T is a data structure that represents the PID of the regular number. If you create a child process successfully, the ID of the child process is returned for the parent process. And for a subprocess, it returns 0. The return 1 represents a failure to create a child process.

  

  

#include <sys/types.h>#include<unistd.h>#include<stdlib.h>#include<stdio.h>#include<string.h>#include<signal.h>#include<errno.h>#include<signal.h>intMainvoid) {pid_t pid;    Signal (sigchld,sig_ign); printf ("before fork pid:%d\n", Getpid ()); intABC =Ten; PID= Fork ();if(PID = =-1)//Error return {perror ("Tile"); return-1; }    if(PID >0)//parent process space {ABC++; printf ("parent:pid:%d \ n", Getpid ()); printf ("abc:%d \ n", ABC); Sleep ( -); }    Else if(PID = =0) {//child process space ABC++; printf ("child:%d,parent:%d\n", Getpid (), Getppid ()); printf ("abc:%d", ABC); } printf ("Fork after...\n");}Before Fork pid:27319parent:pid:27319

Before Fork pid:27319parent:pid:27319
Abc:11
Before Fork pid:27319child:27320,parent:27319
Abc:11fork after ...

  3.fork () system invoke note point:

Through the above procedure we can know: 1) After the fork system is called, the parent and child processes are executed alternately, and they are in different spaces.

2) A call to the fork () function returns 2 times, this is a bit abstract difficult to understand, at this time two processes in a separate space, their respective executor of their own things, no conflict, so return 2 times PID ==0, a PID greater than 0. And as for the first child process or the parent process to execute first, There is no definite rule, it is random.

3) the fork () return value greater than 0 is set to the parent process because it is relatively easy for the child process to get the PID of the parent process, and the parent process is called hard by the child process PID, so the PID byte of the child process in the fork () system call has its own return to the parent process.

4) The child execution of Forl () does not start from scratch after the fork () because the parent process has already set up the environment for the child process before the fork (). So the byte valid code begins. Understand the above four points, I believe we should have a deeper understanding of this system call.

  4.vfork () system call:

So finish the fork (), we may as well compare with vfork (), and learn to end the vfork (.); Vfork () In some cases, we know that vfork () is the same as the fork () execution, except that the child process executes an exec system call or calls _exit (0) to exit. Function prototype: pid_t vfork vfork (void), The exact return value is similar to where fork (). It is not recommended to use Vfork () when this function is not implemented on write-time assignment.

    • The conclusion is as follows 1) vfork () child processes share data segments with parent processes

2) Vfork () is the child process that executes before the parent process executes.

Usually we are together with the EXEC function to replace the process impression in the main process.

The principle and usage of deep analysis fork ()

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.