Talk C together (79th back: C language instance-parent process and child process)

Source: Internet
Author: User

Talk C together (79th back: C language instance-parent process and child process)

Hello, everyone. In the previous session, we talked about the process creation example. The example here is:Parent process and child process. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

In the previous article, we introduced how to use the fork function to create a new process. In this example, we will introduce the parent process and child process. It can also be seen as a further introduction to the fork function.

What are Parent and Child processes? For example, if process A creates process B during running, process A is the parent process and process B is the child process of process. At this time, a reader asked: "Is there any parent process since there is a parent process? "My answer is no. The world of programs belongs to the parent society. You may have heard of the father of C and the father of C ++. Have you ever heard of the mother of C? Or the mother of C ++? Haha. A burst of laughter.

Next, we will explain the parent process and child process through specific code. The following is the specific code for your reference:

#include
  
   #include
   
    int main(){    pid_t pid;    pid = fork();    sleep(5);    if(pid > 0)    {        printf("PID: %d -> Father Process is running \n",getpid());    }    else if(pid == 0)    {        printf("PID: %d -> Son Process is running \n",getpid());    }    else    {        printf("Create process failed \n");        return 1;    }    return 0;}
   
  

In the above Code, we used the fork function to create a new process, which is a sub-process. As we all know, sub-processes and parent processes share the same resources, such as code and data. Therefore, in the above code, the code in the main function can be run by both the parent process and the quilt process, that is, the parent process and the child process execute the same code. If this is the case, the parent process and the child process will output the same result during running.
We compile and run the above program and get the following results:

$. /S // run the compiled program PID: 3212-> Father Process is running // The result PID output by the parent Process during running: 3213-> Son Process is running // result output when the sub-Process is running

From the preceding program running results, we can see that the running results of the Parent and Child processes are completely different, which is inconsistent with the analysis results above. Is the program running wrong, or is the analysis just now incorrect?

Check the processes in the system. First, open a terminal and run the ps command to view the processes in the current system. The running result is as follows:

$ Ps x // enter the ps x COMMAND and execute the pid tty stat time command 1504? Ssl 0: 00 cinnamon-session -- session cinnamon... // system-related processes temporarily ignore 3134 pts/2 S + 0: 01 vim // The vim process we are using 3147 pts/3 Ss 0: 00 bash 3190 pts/4 Ss 0: 00 bash 3212 pts/3 S + 0: 00. /s // from the PID, This is the parent process 3213 pts/3 S + 0: 00. /s // from the PID, This is the sub-process 3215 pts/4 R + 0: 00 ps x // use the ps command to view the process

Through the above results, we can see that the system does run two processes, indicating that the program runs correctly. Is the analysis we just analyzed incorrect? In fact, our analysis and program running results are all correct. The reason for this is that the fork function produces different results in different processes.

In our analysis just now, the parent process and child process execute the same code. More specifically, both the parent process and child process execute the code in the main function. However, when the parent process runs to the fork function, it creates a new process and returns the PID of the new process. The PID is a positive integer greater than 0. Therefore, it only runs the following code:

    if(pid > 0)    {        printf("PID: %d -> Father Process is running \n",getpid());    }

When a child process runs to the fork function, it does not create a new process. The fork function returns 0, and the PID is equal to 0. Therefore, it runs the following code:

    else if(pid == 0)    {        printf("PID: %d -> Son Process is running \n",getpid());    }

Look at the officers, now let's look back at the running results of the program. Is there a sense of openness?

The readers will not write the code in the body, and the detailed code will be put into my resources. You can click here to download and use it.

Let's talk about the examples of parent and child processes. I want to know what examples will be provided later, and I will try again.

Related Article

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.