Process derivation--fork~~~

Source: Internet
Author: User

Stumbled, Unix also saw the process management, suddenly feel tall on the. However, in the face of the first system call fork, this English pronunciation is really lame, almost enrolled into ~ ~.

In Unix, there is a unique process ID (PID) for any process, but there are many other properties besides the process ID: Parent process ID (ppid), process actual user ID, process valid user ID, process actual user group ID, process valid user group ID. For these properties, you can use the following functions to get

#include <unistd.h>pid_t getpid (void);p id_t getppid (void), uid_t getuid (void), uid_t geteuid (void), gid_t getgid ( void); gid_t getegid (void);
It is important to note that none of these functions returns an error.


function fork

#include <unistd.h>pid_t fork (void);

Very peculiar, its return value will have two, for the child process returns 0, for the parent process, returns the child process ID, if an error returns-1.

Derived from the new child process will inherit a lot of the parent process, what the actual user ID ah, and so much more, if you have a UNIX programming books, the above will certainly have these, here is not wordy, here is the key to explore some of the more difficult to understand in the book.

#include <stdio.h> #include <unistd.h>//test Fork usage int main (int argc,char* argv[]) {    printf ("Before fork ~\n ");     pid_t pid=fork ();    if (PID = = 0)   The//fork function returns 0 instructions to execute the child process    {        printf ("This is child\n");    }    else if (PID > 0) The     //fork function returns the PID number    {        printf ("This parent\n") of the subprocess that returns a greater than 0 description;     }    return 0;                    The order of execution of two processes is not necessarily,                                //depending on the scheduling of the system}
Let's take a look at the procedure above, although the code is small, but it's a big deal. The first is printf, which is a row buffer for IO to the standard output stream. So when the buffer is full, or when the buffer is flushed, the buffer data is flushed. However, a child process that comes out of the fork function inherits the buffer of the parent process. So if the data in the buffer is not refreshed, which means that the child process will also get a copy of the "before fork~" from the parent process, then the program will magically output two before fork~ when it executes, of course, the code above will only output once, because it flushes the buffer with \ n.

Another thing is, if the program is redirected to the file, then the situation is a little bit the same, because this time is full buffer, even if encounter \ n will not flush the buffer, this way can certainly see two before fork~!

Another situation is that, after the parent process opens a file with open, the child process that is forked is the same file table as the parent process, which means they can change the offset of the file, as if it were copied with the DUP function!


Finally, for two processes, their execution order is not the same, this depends on the scheduling of the system, so you execute this program several times, you will find that each execution results are different. If you want to control the order in which the process executes, you have to use the wait function or the back signal. The time to say it again ~


Well, we know that we generally derive a new process to perform different tasks, so the General Fork sub-process will immediately call the EXEC function (later encountered) to execute the new code, so in order to improve efficiency, often with a vfork function, The difference between the Vfork function and the fork function is that the fork function copies a copy of the data space of the parent process. and Vfork won't. (Simply speaking, the sub-process after the fork does not change the value of the parent process's variables, but vfork), the way to understand this is to raise a chestnut ~

#include <stdio.h> #include <unistd.h>int main (int argc,char* argv[]) {    printf ("before fork~\n");    int test=1;    pid_t pid=fork ();    if (0==pid)    {        printf ("This is child\n");        test++;    }    else if (pid>0)    {        printf ("This is parent\n");        printf ("test=%d\n", test);}    }

We can see that in the sub-process of the code slice, we execute the test++ statement, but in the parent process output when the test is still equal to 1, which means that the child process and the parent process has its own data space, the changes will not affect each other, but vfork will not, Vfork's idea is that we trust the program fork to use exec to execute other programs, so there's no need to create a space for the subprocess to hold the parent process's variables. So the vfork and parent processes are shared with the unified block memory space. So this time if you change the value of the parent process's variable in the subprocess, it will affect the value of the parent process.


Finally, the fork, for the fork function, the system will not really open up a complete space to the child process to replicate the contents of the parent process, but is the copy-on-write (Copy-on-write) method, that is, the parent process and child processes still share data segments, stacks, heap space, Only the system will make the permission of this space read-only, if the parent process or child process attempts to modify a space, this time the system will only copy the data of that space. In order to achieve the improvement of efficiency.


Process derivation--fork~~~

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.