Detailed description of Linux fork () function instances

Source: Internet
Author: User

Fork function learning:

# Include <sys/types. h>
# Include <unistd. h>
# Include <stdio. h>
# Include <stdlib. h>

Main ()

{

Pid_t PID; pid = fork ();

If (PID <0)

Printf ("error in fork! ");

Else if (pid = 0)

Printf ("I am the child process, my process ID is % DN", getpid ());

Else

Printf ("I am the parent process, my process ID is % DN", getpid ());

}

This Code describes how to use the fork function to create sub-processes. The Parent and Child processes run simultaneously and generate different running results. The running result is as follows:

#./A. Out

I am the child process, my process ID is 4286

I am the parent process, my process ID is 4285

Fork is the meaning of fork in English. In fork, take the meaning behind it. The fork function creates a sub-process. The sub-process and the parent process start to run the program after the split at the same time (in fact, it is a CPU time-sharing process.

Rewrite the program:

Main ()

{

Pid_t PID;

Printf ("/n [% d] Not fork pid = % d/N", getpid (), pid );

PID = fork ();

Printf ("/n [% d] forked pid = % d/N", getpid (), pid );

If (PID <0)

{

Printf ("error in fork! /N ");

Getchar ();

Exit (1 );

}

Else if (pid = 0)

Printf ("/n [% d] In child process, p_id = % d/N", getpid (), getpid ());

Else

{

Printf ("/n [% d] in parent process, my pid = % d/N", getpid (), pid );

Printf ("/n [% d] in parent process, my getpid = % d/N", getpid (), getpid ());

}

}

The program running result is as follows:

$./Fork

[1, 3819] Not fork

[1, 3820] forked pid = 0

[3820] In child process, p_id = 3820

[1, 3819] forked pid = 3820

[1, 3819] in parent process, my pid = 3820

[3819] in parent process, my getpid = 3819

We can clearly see that not fork is printed only once. [3819] indicates the process Number of the parent process. After fork is created, the value PID returned by the fork function to the parent process is the process number of the child process [3820]. In the child process, the PID value is zero. That is to say, the PID is set to zero in the sub-process. According to a netizen on the Internet, "In fact, it is equivalent to a linked list. The process forms a linked list. The parent process PID (P means point) points to the sub-process ID because the sub-process has no sub-process, so its PID is 0."

An interesting program:

Int main ()

{

Int I;

For (I = 0; I <3; I ++)

{

Int pid = fork ();

If (pid = 0)

{

Printf ("Son/N ");

}

Else

{

Printf ("Father/N ");

}

}

Return 0;

}

Let's think about how many father sons will appear in the end ?.......

Right answer:

$./Fork

Father

Son

Son

Son

Father

Father

Son

Father

Son

Son

Father

Father

Son

Father

A total of 7 son7 father. Are you correct? This question needs to be painted on paper to understand for I = 0 1 2 Father son father son. Each row represents the running result of a process. When a child process is generated, the child process prints son. When the child process calls fork to generate the child process, the child process is promoted to father. To sum up, Father always prints father. Son is son before fork. After fork, It is father and a new son is generated.

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.