开发者

What is easier for OS to set up, a new process or a new thread?

Question as stat开发者_高级运维ed above .. from the stand point of Operating System, which one is easier to create, a thread or a process?


A new thread should be faster to create than a new process.

A process is a heavy weight system structure. It has it's own virtual memory space, owns all handles (mutexes, semaphores, open files), and has protection from other processes. Cross-process communication has to go through the OS.

A thread is a "child" to a process. A thread is simply an execution context (registers, stack, and thread-local state) that can run on another hardware core or be co-scheduled on the same core as other threads within a process. Multiple threads share the resources of a single process including the address space and OS handles owned by the process.

There are structures even faster than dynamically creating threads for achieving multitasking during a programs runtime.

Some systems or code libraries support have thread pools (light-weight threads). In this case, you tell the system how many threads you want to run and it creates them up front. Then instead of creating and destroying threads (which is still a relatively slow process), you can allocate and free threads from this pool.

Job Tasking is another similar lighter weight multicore structure where you have several threads with a job queues of tasks to execute. They run the tasks in their job queues and then sleep when the queues are empty.

For both thread pools and job tasking, there is no need for thread startup / shutdown cost aside from upon creation and destruction of the global pools and queues.


Well traditionally threads are called "lightweight processes" so I guess they are easier to set up.

IIRC in Linux both forking and starting a new thread (clone(2)) are implemented deep down with a call to the same function (do_fork) and the set-up times are really comparable for decent numbers. For large numbers of forks / clones (think thousands) they start to add up.

In TLPI there is a nice comparison:

  • Forking 100,000 times: 22.27 seconds
  • Cloning 100,000 times: 2.97 seconds

In particular a really nice feature of clone is that the speed remains constant even if the size of the process cloned grows.

The real advantage of threads lies in that they don't need IPC.


A new thread is easier to create, since when a new process is created, it requires more setup than a thread, e.g. a security context, an inheritable handle, a current directory, etc.


The major difference between threads and processes is 1.Threads share the address space of the process that created it; processes have their own address.

2.Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.

3.Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.

4.Threads have almost no overhead; processes have considerable overhead.

5.New threads are easily created; new processes require duplication of the parent process.

6.Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.

7.Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of the other threads of the process; changes to the parent process does not affect child processes.


A thread just has to be as easy or easier to create than a process since a creating a process implies creating at least one thread to run the process code.

Rgds, Martin

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜