What is the purpose of fork()?

前端 未结 15 1223
悲哀的现实
悲哀的现实 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:35

    fork() is used to create child process. When a fork() function is called, a new process will be spawned and the fork() function call will return a different value for the child and the parent.

    If the return value is 0, you know you're the child process and if the return value is a number (which happens to be the child process id), you know you're the parent. (and if it's a negative number, the fork was failed and no child process was created)

    http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html

提交回复
热议问题