分叉与线程 [英] Forking vs Threading

查看:88
本文介绍了分叉与线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前在我的应用程序中使用过线程,并且非常了解它的概念,但是最近在我的操作系统讲座中,我遇到了fork().这类似于线程.

I have used threading before in my applications and know its concepts well, but recently in my operating system lecture I came across fork(). Which is something similar to threading.

我在Google上搜索了它们之间的区别,我知道:

I google searched difference between them and I came to know that:

  1. Fork不过是一个看起来与旧流程或父流程完全相似的新流程,但它仍然是具有不同流程ID并拥有自己内存的不同流程.
  2. 线程是轻量级进程,具有较少的开销

但是,我仍然有一些疑问.

But, there are still some questions in my mind.

  1. 什么时候您应该更喜欢fork()而不是线程?
  2. 如果我想以子代身份调用外部应用程序,那么我应该使用fork()还是线程来执行此操作?
  3. 在进行Google搜索时,我发现有人说在线程内调用fork()是一件坏事.为什么人们在做类似的事情时想在线程内调用fork()?
  4. 因为父进程和子进程不能同时运行,fork()不能利用多处理器系统是真的吗?

推荐答案

分叉和线程化方法之间的主要区别是操作系统体系结构之一.在Unix设计之初,fork是一个简单,简单的系统,可以最好地满足大型机和服务器类型的要求,因此在Unix系统上得到了广泛的应用.当Microsoft从头开始重新架构NT内核时,它更多地关注线程模型.因此,今天与Unix系统在分叉方面的效率与Windows在线程方面的效率更高之间仍然存在显着差异.您可以在Apache上最明显地看到这一点,Apache在Unix上使用prefork策略,在Windows上使用线程池.

The main difference between forking and threading approaches is one of operating system architecture. Back in the days when Unix was designed, forking was an easy, simple system that answered the mainframe and server type requirements best, as such it was popularized on the Unix systems. When Microsoft re-architected the NT kernel from scratch, it focused more on the threading model. As such there is today still a notable difference with Unix systems being efficient with forking, and Windows more efficient with threads. You can most notably see this in Apache which uses the prefork strategy on Unix, and thread pooling on Windows.

专门针对您的问题:

什么时候您应该更喜欢fork()而不是线程?

When should you prefer fork() over threading and vice-verse?

在Unix系统上,您要做的工作要比实例化工作程序复杂得多,或者您希望对单独的进程进行隐式安全沙箱处理.

On a Unix system where you're doing a far more complex task than just instantiating a worker, or you want the implicit security sandboxing of separate processes.

如果我想以孩子的身份调用外部应用程序,那么我应该使用fork()还是线程来执行此操作?

If I want to call an external application as a child, then should I use fork() or threads to do it?

如果子代将使用相同的代码执行与父代相同的任务,请使用fork.对于较小的子任务,请使用线程.对于单独的外部进程,都不使用它们,只需使用适当的API调用即可调用它们.

If the child will do an identical task to the parent, with identical code, use fork. For smaller subtasks use threads. For separate external processes use neither, just call them with the proper API calls.

在进行Google搜索时,我发现有人说在线程内调用fork()是一件坏事.为什么人们在做类似的事情时想在线程内调用fork()?

While doing google search I found people saying it is bad thing to call a fork() inside a thread. why do people want to call a fork() inside a thread when they do similar things?

不确定,但我认为复制一个进程和许多子线程在计算上相当昂贵.

Not entirely sure but I think it's computationally rather expensive to duplicate a process and a lot of subthreads.

因为父进程和子进程不能同时运行,fork()不能利用多处理器系统是真的吗?

Is it True that fork() cannot take advantage of multiprocessor system because parent and child process don't run simultaneously?

这是错误的,fork创建了一个新进程,然后利用OS任务计划程序中进程的所有可用功能.

This is false, fork creates a new process which then takes advantage of all features available to processes in the OS task scheduler.

这篇关于分叉与线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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