A simple mastery of the use of fork () functions in Linux systems to create child processes _c language

Source: Internet
Author: User

The fork () function is used to create a new process from an existing process. The new process is called a subprocess, and the garden process is called the parent process. A child process obtained using the fork () function is a replica of the parent process. It inherits the entire process's address space from the parent process, including the context of the process, code snippets, process stacks, memory information, open file descriptors, symbol control settings, process priority, process group number, current working directory, root directory, Resource constraints and control terminals, and the child processes are unique only to its process number, resource usage, and timers.

Because the child process is almost completely replicated by the parent process, the parent-child process runs the same program. This requires a way to differentiate them and make them run like this, otherwise the two processes cannot do different things. In effect, when the fork () function is executed in the parent process, the parent process replicates a child process, and the code of the parent-child process runs concurrently in the two address spaces, starting with the return of the fork () function, so that two processes get the return value of the fork () function of the owning The return value in the parent process is the process number of the child process, and returns 0 in the child process. Therefore, you can determine whether the process is a parent or a child by returning a value.

At the same time, it can be seen that the cost of using the fork () function is great, it replicates most of the code snippets, data segments, and stack segments in the parent process, making the fork () function more expensive and not fast.

code example:

#include <stdio.h>
#include <unistd.h>

int main (int argc, const char * argv[]) {
  //Insert Code He Re ...
  pid_t pid;
  if ((PID = fork ()) = = 0) {
    //return 0 is subprocess
    printf ("Child pid:%d\n", Getpid ());
  } else {
    printf ("pid:%d\n", pi d)//PID printf in the parent process to return the subprocess
    ("Father PID:%d\n", Getpid ());
  }


The results of the printing are as follows:

pid:552
Father pid:549 Child
pid:552

Here are some points of attention and summary:
1 before using on VS, it turns out that there is no header file at all, because <unistd.h> is a UNIX-like system; The above code tests OK on Mac OS.

2 fork () is used to create a subprocess, after which the child process is a copy of the parent process, which obtains a copy of the data space, heap, and stack of the parent process, noting that the two are not shared. Parent-child sharing only code snippets. This is the previous implementation, now usually not directly to copy, but write-time replication (copy-on-write).

3 The order of execution of the parent-child process is indeterminate after fork ().

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.