How to use fork() in unix? Why not something of the form fork(pointerToFunctionToRun)?

前端 未结 8 2223
盖世英雄少女心
盖世英雄少女心 2021-02-06 05:01

I am having some trouble understanding how to use Unix\'s fork(). I am used to, when in need of parallelization, spawining threads in my application. It\'s always s

8条回答
  •  悲哀的现实
    2021-02-06 05:38

    Fork()'s most popular use is as a way to clone a server for each new client that connect()s (because the new process inherits all file descriptors in whatever state they exist). But I've also used it to initiate a new (locally running) service on-demand from a client. That scheme is best done with two calls to fork() - one stays in the parent session until the server is up and running and able to connect, the other (I fork it off from the child) becomes the server and departs the parent's session so it can no longer be reached by (say) SIGQUIT.

提交回复
热议问题