Fork () questions

Source: Internet
Author: User

Recently in the reading of Linux programming, and then also online access to the relevant information found a fork () a few questions, here to record!
#include "Sys/types.h"
#include "Unistd.h"
#include pit_t fork (void);
The fork () function call succeeds and returns two values;
Parent process: Returns the PID of the child process;
Child process: returns 0;

Error: return-1;

A process consists mainly of the following aspects:
(1) A program that can be executed
(2) All data associated with the process (including variables, memory, buffers)
(3) Program context (program counter PC, save program execution location)

Here the main review, fork () implementation and a few points needing attention, in addition to a few questions.

First: COW (copy-on-write) write-time replication technology,Actually in There's also a talk here ., Repeat here!
See procedure 1 below:
#include <stdio.h>  #include <string.h>  #include <stdlib.h>  #include <unistd.h>  int main ()  {      int  num=11;      pid_t pid=fork ();      if (pid==0)      {          num=22 ';          printf ("num=%d\n in sub-process", num);          printf ("The first address of NUM in subprocess:%x\n", &num);      }      else      {          sleep (1);          printf ("num=%d\n in parent process", num);          printf ("The first address of NUM in the parent process:%x\n", &num);      }  

Operation Result:


From the running results, it is found that the fork () function does create two functions, and each function runs independently, the variables are not shared, and the child process does replicate the resources of the parent process, otherwise the value of NUM will be the same.
But why does the parent/child process point to Num's first address, which is 0xbfe88098?
This involves the logical address (virtual address) and the physical address, mapping the logical address to the physical address we call redirection.
Logical Address:CPU generated address (also known as virtual address), divided into page-type virtual memory, segment virtual memory and Segment-page virtual memory, by base + offset address to get
Physical memory address. This relies on the address transformation mechanism, which is managed by a dedicated MMU.
Physical Address:The address that the memory sees, the programmer cannot see the real physical address, can see only the logical address.
Static redirection:When the program loads the memory to complete the transformation of the logical address to the physical address, no change occurs during program execution.
Dynamic redirection:The transformation of the logical address to the physical address is completed during the execution of the program.
Having said this, the program means that the logical address of the variable is the same, but the physical address is different (the last output value is not the same), because Fork does not fully allocate the physical memory that it needs to the child process after it is created, but instead only copies the virtual memory space, in fact the parent/ The child process shares this physical memory, and when there is a process that needs to rewrite the value of the variable, it is time to assign the corresponding physical memory to the changed process, that is, the cow technology.
second, the following program has created several processes in the end? Output several "-"?
#include <stdio.h> #include <sys/types.h> #include <unistd.h>int main (void) {int i;for (i=0; i<3;   i++) {fork ();  printf ("*\n"); }printf ("  +\n");//represents the number of processes created; return 0;}

Operation Result:


Running results at first glance may not quite understand, let me slowly to explain, * number for each process will print out a *,+ number represents the number of processes created. Because the process here does not share a variable, I would like to count the count counter, but I can not do it, because each counter count is starting from 1, and finally add up, looks more chaotic, so I use * and + to express more intuitive. This also proves a problem, that is, the function of the fork is indeed a copy of the resource space rather than the copy of the pointer (vfork)!! Very good!

Because fork is the parent process that creates a child process, why * is more than +, because the process is duplicated, but exits are certain and only exits once, so + just can represent several processes created. Think it over and you'll understand ...

In fact, the process of creating the process is this:

The number represents the process number, which is the specific process.

Below we can look at the created process by looking at its PID:


It can be seen that a total of 8 processes have produced the process 6773,6774,6775,6776,6777,6778,6779,6780.

So print 14 *,8 + number, you should understand it here!

There is also a problem with the replication buffer:

If the above input format is:

         printf ("*\n");   Or:   printf ("*"); Fflush (stdout);

Into:

         printf ("*");

Then the result will be 24 * numbers printed! This is because printf ("*"), the statement has buffer, so, for the above program , printf ("*"), put "*" into the cache, and there is no real output in the fork, a bit of exit () and _exit () meaning! The cache is copied to the child process space, so there are 10 more, 24 instead of 14. This can be done on your own hands to understand!

In addition, we know that the UNIX system has "block device" and "character device" of the points, so-called block device is a piece of data reading device, such as disk, memory; character device is a character of a character read, such as keyboard, serial port and so on. block devices generally have caches, while character devices are generally not cached .

question one: What is the value range of the PID under Linux?
General pid_max=0x8000 (can be changed), so the maximum value of the process number is 0X7FFF, that is, 32767. Process number 0-299 is reserved for the daemon process. Now the kernel does not seem to have this limitation, "Linux kernel design and implementation" said that in order to be compatible with older versions of UNIX and Linux, the PID maximum value by default is 32767 (the maximum value of short int). If you need to, you can also do not consider the compatibility with the old version, modify the/proc/sys/kernel/pid_max to increase the upper limit with echo to write a value to this file.
Since the general machine can not run so many processes + threads at the same time, so 32768 is definitely enough, but the system tends to allocate unused PID to the new process, so you will find that in the running system, there are many low-level PID is not used, it is because the PID was used by other programs when booting, Of course, you really have the ability to use the maximum value of the PID, the system also has a way to solve, that is, from the beginning (low) search is not occupied by the PID assigned to the new process.
question two: what is the meaning of the init 0/1/2/3/4/5/6 in Linux?
0-Stop (never set the Initdefault to 0)I just use this to shut down the machine, the speed is relatively fast!
1-Single user mode
2-multiuser, no NFS
3-Full multi-user mode (standard run level)
4-No Use
5-x11 (Xwindow)
6-Reboot (reboot) (never set Initdefault to 6)
question three: Why do some places say fork () calls when a child process precedes the parent process?
Because the kernel uses-write-time replication mechanism, the parent/child process after fork is a shared page table descriptor, if the parent process first executes, then there is a great chance that the parent process will modify the Shared page table points to the data, the kernel must now assign to the parent process and copy the new page table for the parent process to modify the use,then if the child process is created and then quits without doing anything, then this copy is redundant., if the child process is executed first, if the child process has not done anything to quit, then there is no so-called copy-on-write,avoid unnecessary page duplication;In addition, in case the parent process does not cause write-time replication, the child process executes the exec system call, resulting in a loss of efficiency due to meaningless replication. If the parent/child process has made changes to the data and performed its own operations, then there is no point! So some places also say that the order of execution of the parent/child process is indeterminate! There is a certain reason.
question four: What is the difference between exec and system?
(1) Exec is directly with the new process to replace the original program run, after the completion of the operationnot go back to the original program .。
(2) system is called by the Shell to execute your command,System=fork+exec+waitpid,After the execution, go back to the original program. Continue to perform the following
Part.
In short, if you call with Exec, you should first fork a new process and then exec. The system does not need you to fork the new process, has been encapsulated.
question five: What does exec replace?
The system calls exec to be in the new process(executable binary file or shell script file)To replace the original process, only the PID of the process remains unchanged. Therefore, it can be argued that the exec system call did not create a new
Processbut all the contents of the original process context are replaced (the code snippet of the original process, the data segment, the stack segment), it's really a bit of "Jinchantuoqiao" in "36." It looks like an old shell, but it's already infused with a new soul.Call success functions do not return, only the call fails, they return a-1, from the original program's call point followed by execution.

Question six: After fork () what exactly did the child process replicate the parent process?
About memory replication and sharing in the fork function the child process replicates the parent process's data space (data segment), stack, and heap, and the parent and child processes share the body segment. "That is, for the data in the program, the child process copies one copy, but for the instruction, the child process is not copied but is shared with the parent process, unless the EXEC function is executed. The virtual space of the two is different, but the corresponding physical space is the same.
Copy-on-write technology (important thing to say two times): The kernel creates virtual space structures for newly generated child processes, but does not allocate physical memory for these segments, they share the physical spaces of the parent process, and when there are changes to the corresponding segments in the child process , the corresponding segments of the child process are allocated physical space. However, the thread created by Vfork () is the virtual address space structure of the kernel's connecting child process, which directly shares the virtual space of the parent process.

Data reference:

http://blog.csdn.net/xy010902100449/article/details/44851453

Http://www.cnblogs.com/blankqdb/archive/2012/08/23/2652386.html

Http://blog.chinaunix.net/uid-24774106-id-3361500.html
Http://www.linuxidc.com/Linux/2015-03/114888.htm

http://blog.csdn.net/lollipop_jin/article/details/8774057

Thank you for sharing this blog!

Fork () questions

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.