什么是叉()的目的是什么? [英] What is the purpose of fork()?

查看:124
本文介绍了什么是叉()的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在很多程序和Linux的手册页,我已经看到了使用叉code()。为什么我们需要使用叉()键,其目的是什么?

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?

推荐答案

叉()是如何创建在Unix中新进程。当你调用,你创建自己的过程的副本都有自己的地址空间。这允许多个任务到彼此的独立运行,就像它们各自具有的计算机的完整存储器自己。

fork() is how you create new processes in Unix. When you call fork, you're creating a copy of your own process that has its own address space. This allows multiple tasks to run independently of one another as though they each had the full memory of the machine to themselves.

下面是的一些示例用法:

Here are some example usages of fork:


  1. 外壳使用运行您从命令行调用的程序。

  2. Web服务器如阿帕奇使用以创建多个服务器进程,每个进程其中处理在自己的地址空间的请求。如果一个人死亡或存在内存泄漏问题,别人都不会受到影响,所以它的功能为容错机制。

  3. 谷歌Chrome浏览器使用来一个单独的进程中处理每个页面。这将prevent客户端code在一个页面上把你的整个浏览器了。

  4. 用来产卵在某些并行程序(进程像使用的 MPI )。注意,这是使用线程不同,该不具有其自己的地址空间和存在的 的内部流程。

  5. 脚本语言使用间接启动子进程。例如,每次你使用像<一个命令href=\"http://docs.python.org/library/subprocess.html#module-subprocess\"><$c$c>subprocess.Popen在Python,你一个子进程并读取其输出。这使程序能够协同工作。

  1. Your shell uses fork to run the programs you invoke from the command line.
  2. Web servers like apache use fork to create multiple server processes, each of which handles requests in its own address space. If one dies or leaks memory, others are unaffected, so it functions as a mechanism for fault tolerance.
  3. Google Chrome uses fork to handle each page within a separate process. This will prevent client-side code on one page from bringing your whole browser down.
  4. fork is used to spawn processes in some parallel programs (like those written using MPI). Note this is different from using threads, which don't have their own address space and exist within a process.
  5. Scripting languages use fork indirectly to start child processes. For example, every time you use a command like subprocess.Popen in Python, you fork a child process and read its output. This enables programs to work together.

在shell 的典型用法可能是这个样子:

Typical usage of fork in a shell might look something like this:

int child_process_id = fork();
if (child_process_id) {
    // Fork returns a valid pid in the parent process.  Parent executes this.

    // wait for the child process to complete
    waitpid(child_process_id, ...);  // omitted extra args for brevity

    // child process finished!
} else {
    // Fork returns 0 in the child process.  Child executes this.

    // new argv array for the child process
    const char *argv[] = {"arg1", "arg2", "arg3", NULL};

    // now start executing some other program
    exec("/path/to/a/program", argv);
}

外壳采用产卵 EXEC 等待一个子进程,并为它完成,然后用自己的继续执行。请注意,您不必使用fork这种方式。您可以随时关闭产卵很多子进程,作为并行程序可能会做,而且每个可能同时运行的程序。基本上,任何时候你在Unix系统中创建新的流程,你使用叉()。对于Windows相当于看看 的CreateProcess

The shell spawns a child process using exec and waits for it to complete, then continues with its own execution. Note that you don't have to use fork this way. You can always spawn off lots of child processes, as a parallel program might do, and each might run a program concurrently. Basically, any time you're creating new processes in a Unix system, you're using fork(). For the Windows equivalent, take a look at CreateProcess.

如果您想了解更多的例子和一个较长的解释,维基百科有一个体面的总结。和这里有一些幻灯片这里如何进程,线程和并发工作在现代的操作系统。

If you want more examples and a longer explanation, Wikipedia has a decent summary. And here are some slides here on how processes, threads, and concurrency work in modern operating systems.

这篇关于什么是叉()的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆