What is the purpose of fork()?

前端 未结 15 1227
悲哀的现实
悲哀的现实 2020-12-12 11:11

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?

15条回答
  •  南方客
    南方客 (楼主)
    2020-12-12 11:48

    fork() is used to spawn a child process. Typically it's used in similar sorts of situations as threading, but there are differences. Unlike threads, fork() creates whole seperate processes, which means that the child and the parent while they are direct copies of each other at the point that fork() is called, they are completely seperate, neither can access the other's memory space (without going to the normal troubles you go to access another program's memory).

    fork() is still used by some server applications, mostly ones that run as root on a *NIX machine that drop permissions before processing user requests. There are some other usecases still, but mostly people have moved to multithreading now.

提交回复
热议问题