Thursday, February 22, 2007

os: threads, fibers and processes



Thread (computer science) - Wikipedia, the free encyclopedia

Threads compared with processes

Threads are distinguished from traditional multi-tasking operating system processes in that processes are typically independent, carry considerable state information, have separate address spaces, and interact only through system-provided inter-process communication mechanisms. Multiple threads, on the other hand, typically share the state information of a single process, and share memory and other resources directly. Context switching between threads in the same process is typically faster than context switching between processes. Systems like Windows NT and OS/2 are said to have "cheap" threads and "expensive" processes; in other operating systems there is not so great a difference.



Multithreading is a popular programming and execution model that allows multiple threads to exist within the context of a single process, sharing the process' resources but able to execute independently. The threaded programming model provides developers with a useful abstraction of concurrent execution. However, perhaps the most interesting application of the technology is when it is applied to a single process to enable parallel execution on a multiprocessor system.

This advantage of a multi-threaded program allows it to operate faster on computer systems that have multiple CPUs, CPUs with multiple cores, or across a cluster of machines. This is because the threads of the program naturally lend themselves to truly concurrent execution.



A process is the "heaviest" unit of kernel scheduling.
Processes own resources allocated by the operating system. Resources
include memory, file handles, sockets, device handles, and windows.
Processes do not share address spaces or file resources except through
explicit methods such as inheriting file handles or shared memory
segments, or mapping the same file in a shared way. Processes are
typically pre-emptively multitasked. However, Windows 3.1 and older
versions of Mac OS used co-operative or non-preemptive multitasking.


A thread is the "lightest" unit of kernel scheduling. At
least one thread exists within each process. If multiple threads can
exist within a process, then they share the same memory and file
resources. Threads are pre-emptively multitasked if the operating
system's process scheduler is pre-emptive. Threads do not own resources
except for a stack and a copy of the registers including the program counter.

A fiber is a "user thread." In some situations, there is a distinction between "kernel threads" and
"user threads" -- the former are managed and scheduled by the kernel,
whereas the latter are managed and scheduled in userspace. In this
article, the term "thread" is used to refer to kernel threads, whereas
"fiber" is used to refer to user threads. Fibers are co-operatively scheduled:
a running fiber must explicitly "yield" to allow another fiber to run.
A fiber can be scheduled to run in any thread in the same process.




powered by performancing firefox

No comments: