What is the purpose of fork()?

2019-01-29 20:19发布

In many programs and man pages of Linux, I have seen code using fork(). Why do we need to use fork() and what is its purpose?

标签: c unix posix fork
15条回答
虎瘦雄心在
2楼-- · 2019-01-29 20:36

The fork() function is used to create a new process by duplicating the existing process from which it is called. The existing process from which this function is called becomes the parent process and the newly created process becomes the child process. As already stated that child is a duplicate copy of the parent but there are some exceptions to it.

  • The child has a unique PID like any other process running in the operating system.

  • The child has a parent process ID which is same as the PID of the
    process that created it.

  • Resource utilization and CPU time counters are reset to zero in child process.

  • Set of pending signals in child is empty.

  • Child does not inherit any timers from its parent

Example :

    #include <unistd.h>
    #include <sys/types.h>
    #include <errno.h>
    #include <stdio.h>
    #include <sys/wait.h>
    #include <stdlib.h>

    int var_glb; /* A global variable*/

int main(void)
{
    pid_t childPID;
    int var_lcl = 0;

    childPID = fork();

    if(childPID >= 0) // fork was successful
    {
        if(childPID == 0) // child process
        {
            var_lcl++;
            var_glb++;
            printf("\n Child Process :: var_lcl = [%d], var_glb[%d]\n", var_lcl, var_glb);
        }
        else //Parent process
        {
            var_lcl = 10;
            var_glb = 20;
            printf("\n Parent process :: var_lcl = [%d], var_glb[%d]\n", var_lcl, var_glb);
        }
    }
    else // fork failed
    {
        printf("\n Fork failed, quitting!!!!!!\n");
        return 1;
    }

    return 0;
}

Now, when the above code is compiled and run :

$ ./fork

Parent process :: var_lcl = [10], var_glb[20]

Child Process :: var_lcl = [1], var_glb[1]
查看更多
你好瞎i
3楼-- · 2019-01-29 20:37

First one needs to understand what is fork () system call. Let me explain

  1. fork() system call creates the exact duplicate of parent process, It makes the duplicate of parent stack, heap, initialized data, uninitialized data and share the code in read-only mode with parent process.

  2. Fork system call copies the memory on the copy-on-write basis, means child makes in virtual memory page when there is requirement of copying.

Now Purpose of fork():

  1. Fork() can be used at the place where there is division of work like a server has to handle multiple clients, So parent has to accept the connection on regular basis, So server does fork for each client to perform read-write.
查看更多
爷的心禁止访问
4楼-- · 2019-01-29 20:38

fork() will create a new child process identical to the parent. So everything you run in the code after that will be run by both processes — very useful if you have for instance a server, and you want to handle multiple requests.

查看更多
贼婆χ
5楼-- · 2019-01-29 20:39

Fork() system call use to create a child process. It is exact duplicate of parent process. Fork copies stack section, heap section, data section, environment variable, command line arguments from parent.

refer: http://man7.org/linux/man-pages/man2/fork.2.html

查看更多
霸刀☆藐视天下
6楼-- · 2019-01-29 20:40

Fork creates new processes. Without fork you would have a unix system that could only run init.

查看更多
老娘就宠你
7楼-- · 2019-01-29 20:42

fork() is how Unix create new processes. At the point you called fork(), your process is cloned, and two different processes continue the execution from there. One of them, the child, will have fork() return 0. The other, the parent, will have fork() return the PID (process ID) of the child.

For example, if you type the following in a shell, the shell program will call fork(), and then execute the command you passed (telnetd, in this case) in the child, while the parent will display the prompt again, as well as a message indicating the PID of the background process.

$ telnetd &

As for the reason you create new processes, that's how your operating system can do many things at the same time. It's why you can run a program and, while it is running, switch to another window and do something else.

查看更多
登录 后发表回答