Process vs Thread

from: http://technet.microsoft.com/en-us/library/cc767883.aspx

(in case of Windows NT )

A process comprises:

  •     A private memory address space in which the process's code and data are stored.
  •     An access token against which Windows NT makes security checks.
  •     System resources such as files and windows (represented as object handles).
  •     At least one thread to execute the code.

A thread comprises:

  •     A processor state including the current instruction pointer.
  •     A stack for use when running in user mode.
  •     A stack for use when running in kernel mode.

Since processes (not threads) own the access token, system resource handles, and address space, threads do NOT have their own address spaces nor do they have their own access token or system resource handles. Therefore, all of the threads in a process SHARE the same memory, access token, and system resources (including quota limits) on a "per-process" rather than a "per-thread" basis. In a multithreaded program, the programmer is responsible for making sure that the different threads don't interfere with each other by using these shared resources in a way that conflicts with another thread's use of the same resource. (As you might suspect, this can get a little tricky.)

...multithreaded programs must be specially programmed to ensure that threads don't step on each other.

Comment: Thus multi-threading always need the consideration of resource conflicts (critical-section problem). As for multi-process, it is true only when two processes share some common data. e.g, in linux when we create a child process we just get a copy of the current one, so we need no considerations about synchronization, while it is because of the nature of multi-threading that we should deal with it.

A section of code that modifies data structures shared by multiple threads is called a critical section . It is important than when a critical section is running in one thread that no other thread be able to access that data structure. Synchronization is necessary to ensure that only one thread can execute in a critical section at a time. This synchronization is accomplished through the use of some type of Windows NT synchronization object. Programs use Windows NT synchronization objects rather than writing their own synchronization both to save coding effort and for efficiency: when you wait on a Windows NT synchronization object, you do NOT use any CPU time testing the object to see when it's ready.

Windows NT provides a variety of different types of synchronization objects that programs can use to coordinate threads' access to shared data structures. Synchronization objects remember their states and can be set and tested in one uninterruptable step. They also cause the thread to be suspended while waiting on an object and to automatically restart when the other thread signals that it's done.

Comment: suspended - solve busy waiting


Read More:

1. 原文 http://technet.microsoft.com/en-us/library/cc767883.aspx

2. http://www.ibm.com/developerworks/cn/linux/l-linux-synchronization.html(中文:Linux同步方法剖析)




你可能感兴趣的:(Process vs Thread)