操作系统拾遗之进程和线程
2009-11-04 00:00:00 来源:WEB开发网在创建进程时,会有相当的系统调用
究竟有哪些额外的系统调用,请参考上面帖子.
Linux
让我们回到本文的摘要部分的引入, 我的室友提出的对于我而言 闻所未闻 的新观点.
那么, 在Linux下 进程和线程真的没有本质区别吗?
首先大家可以参考这个帖子, Threads vs Processes in Linux.
下面内容摘自 Threads vs Processes in Linux.
Linux uses a 1-1 threading model, with (to the kernel) no distinction between processes and threads
-- everything is simply a runnable task. *
On Linux, the system call clone clones a task, with a configurable level of sharing, among which are:
CLONE_FILES: share the same file descriptor table (instead of creating a copy)
CLONE_PARENT: don't set up a parent-child relationship between the new task and the old
(otherwise, child's getppid() = parent's getpid())
CLONE_VM: share the same memory space (instead of creating a COW copy)
fork() calls clone(least sharing) and pthread_create() calls clone(most sharing). **
forking costs a tiny bit more than pthread_createing because of copying tables and creating COW mappings for memory,
but the Linux kernel developers have tried (and succeeded) at minimizing those costs.
Switching between tasks, if they share the same memory space and various tables, will be a tiny bit cheaper
than if they aren't shared, because the data may already be loaded in cache. However,
switching tasks is still very fast even if nothing is shared -- this is something else that Linux kernel developers
try to ensure (and succeed at ensuring).
In fact, if you are on a multi-processor system, not sharing may actually be a performance boon:
if each task is running on a different processor, synchronizing shared memory is expensive.
更多精彩
赞助商链接