且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

分叉与线程

更新时间:2022-05-02 16:07:03

分叉和线程化方法之间的主要区别是操作系统体系结构之一.在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.