A simple example of "Linux" about understanding fork () functions __oracle

Source: Internet
Author: User
1.fork () function

The fork () function creates a process that is almost identical to the original process through system calls, and this new process is called a subprocess. Once a process calls the fork () function, the system assigns resources to the new process, such as space for storing data and code. All the values of the original process are then copied to the new process, with only a few values different from the values of the original process. The equivalent of cloning a self. One thing to note: After calling the fork function, it must be that the code snippet that two processes execute at the same time is the code after the fork function, and the previous code and the execution of the parent process are complete. Let's look at a very simple example.
The fork function returns two values to return a value greater than 0 to the parent process return 0 to return other values to the child process fork failed 2. A simple example of fork

#include <stdio.h>
#include <unistd.h>
int main ()
{
    pid_t pid;
    int count = 0;
    PID = fork ();   Fork a Process
    if (PID = = 0)
    {               //pid to 0, printf ("This is the child
        process, PID is%d\n", getpid ());/ Getpid returns the PID count+=2 of the current process
        ;
        printf ("Count =%d\n", count);
    }
    else if (PID > 0)
    {
        printf ("This are Father process, PID is%d\n", getpid ());
        count++;
        printf ("Count =%d\n", count);
    }
    else
    {
        fprintf (stderr, "error:fork () failed!\n");
    }
    return 0;
}

The following are the results of the operation:

Some people will have an illusion of the results of the operation, that is, the two branches of the IF statement in the program if (PID = 0) and else if (PID > 0) have been executed, in fact, is not the case, the reason for this operation is because, in main () The 6th line of the function fork a process called a subprocess of the original process, the original process called the parent process, which executes concurrently with the original process, and who does not have the rule after tax, which is determined by the operating system.
We use the GDB tool to step through the program, and we'll probably get a clearer idea of the process. 3. Debug the parent process with GDB.

1. Enter the GDB a.out and enter start debugging, when the terminal displays the code to be executed

2. Enter N, execute the current line of code, and display the next code that will be executed

A child process is about to be created, and the current process is the parent process.
3. Re-enter n Execute pid=fork () statement

At this time, the terminal displays

Detaching after fork from the child process 2755.
This are child process, PID is 2755
count = 2

This time, the description has created a child process, the subprocess PID is 2755, and since we are now stepping through the parent process, which does not affect the execution of the subprocess, the subprocess has only a few lines of code, which is done at this time and shows the execution result on the terminal, which is the following two lines.

This are child process, PID is 2755
count = 2

4. At this time, we enter P-PID and look at the value of the fork function returned to the parent process is not the PID of the child process

Sure enough is the PID value of the child process.
5. Enter n then execute the judgment statement

Because the PID value is 2755, it skips over the statements within the IF (pid==0) branch to determine if the PID is greater than 0.
6. Input n then judge whether the PID is greater than 0

The PID value is greater than 0, executing the statement within the branch.
7. Then enter n until the program ends normally.

The above is the process of debugging the parent process, in fact, the child process can also use GDB to debug, next time. 4. Use the PS aux command to view the parent process and child processes

The PS aux command can look at all the processes running in the system, but we have very little code, the system can be executed in an instant, and the PS aux command is not captured at all. So we make some changes in the code.
  

#include <stdio.h>
#include <unistd.h>
int main ()
{
    pid_t pid;
    int count = 0;
    PID = fork ();   Fork a Process
    if (PID = = 0)
    {               //pid to 0, printf ("This is the child
        process, PID is%d\n", getpid ());
        count+=2;
        printf ("Count =%d\n", count);
    }
    else if (PID > 0)
    {
        printf ("This are Father process, PID is%d\n", getpid ());
        count++;
        printf ("Count =%d\n", count);
    }
    else
    {
        fprintf (stderr, "error:fork () failed!\n");
    }
    Sleep (10);//new joined row, let the program pause here for 10 seconds, the parent process and subprocess will execute this line of code return
    0;
}

We added a statement on line 30th to suspend the program for 10s
Recompile the program, and then execute the program, enter the./a.out, quickly switch to another terminal (if you have a slow hand, you can pause for a while), enter PS aux view the running process.
Execution procedure:

Switch terminals, input PS aux view process

You can see that the A.out program produces two processes, the parent process PID is 2928, and the subprocess PID is 2929.

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.