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同步方法剖析)




转载于:https://my.oschina.net/atwood/blog/122488

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值