并发编程基础 Lecture Notes(二)

读写内存,硬盘的速度始终不及CPU的处理速度。除了CPU以外构成了这个程序的执行环境,也就是我们所定义的程序上下文。当一个程序执行完了,或者分配给他的CPU执行时间用完了,那它就要被切换出去,等待下一次CPU的“临幸”,在被切换出去的最后一步工作就是保存程序上下文,因为这个是下次他被CPU“临幸”的运行环境,必须保存。

进程切换:

加载程序的上下文(数据,程序,资源等等),然后开始执行A,保存程序A的上下文;调入下一个要执行的程序B的程序上下文,然后开始执行B,保存程序B的上下文。

线程切换:

进程的粒度太大,每次都要有上下文的调入,保存和调出,开销太大。此时可以将进程切割成多个粒度小的线程共享了进程的上下文环境,切换线程的时候,只需要切换CPU的控制权。

The advantages of thread:

- switch的时候开销小

- 保证性能,宏观上并行,微观上串行

从CPU角度上来说线程并无法提升性能,但是如果某些线程涉及到等待资源比如I/O, 等待输入时,多线程允许进程中的其他线程继续执行而不是整个进程被阻塞,从此提高了CPU的利用率

- 多个线程共享一个进程内存空间

Process && Thread && Program

1. 进程,是一个程序的顺序执行。与进程相关的有进程的地址空间address space。在地址空间中,存放可执行的程序,程序的数据和程序的堆栈。除此之外与进程相关的还有其他资源集,比如寄存器,打开文件的清单等,这些都存放在进程表中。在分时系统中,OS周期性的挂起一个进程(挂起时,之前的所有信息都必须保存下来),然后启动运行另一个进程。系统管理器授权每一个进程使用一个给定的UID标识,子进程与父进程一样的UID。用户可以是某个组的成员,每个组也有一个GID标识。为了完成某些任务,相关的进程需要通信。单个处理器可以被多个进程共享,CPU使用某种调度算法决定何时停止一个进程的工作,并转而为另一个进程提供服务。

2. 进程独立享有内存地址空间,是资源分配的基础单位,而线程是CPU分配的基本单位。多个线程可以共享一个进程的资源,线程又有自己的独立的栈来保存本地变量(寄存器,栈和程序计数器)

3. http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html

Confinement:

- don't give any other threads to read or write the data directly

JAVA Iterator: 我们不可以在iterate一个collection的时候,修改集合里面的元素

- local variable could always be thread confined cuz it is just stored in the stack, and each thread has its own stack

Atomicity: an atomic action makes an indivisible state transformation(any intermediate state that may exists on the implementation must not be visible to another process)

- Fine grain,the action is implemented directly by the hardware on which a concurrent program executes

int y = 2, z = 0 开辟一块内存,栈中一个引用该块内存,将数据放入内存,中间操作对其他程序都不可见

int y = 0, z = 0; co x = y + z; // y = 1; z = 2; oc x的值可能是0,1,2,3;因为并行程序顺序的不确定性,y和z的值都对其他程序可见

》At most once property(Amo),at most one simple variable could be changed by another process and if this variable is only reference at most once in the expression. 

int x = 0, y = 0;

co x = y + 1; // y = x + 1; oc

不满足AMO,因为S1和S2互相引用对方的variable

》假如一个expression不满足amo,我们必须通过同步机制来构建所谓的原子性——coarse-grained atomic action

A coarse-grained atomic action is a sequence of fine-grained atomic actions which appears to be invisible.

Await:

await B S;

S - it's atomic, it contains sequential statements

B - delay condition

<await (s>0) x = x - 1;> //the atomic action delays until s is positive then decrements s. No other process can sneak in and chance in the meantime. 

- safety, an assertion that something bad never happens

- liveness, an assertion that something good eventually happens. It relies on the fairness which is concerned with guaranteeing that processes get the chance to proceed regardless of what other processes do.

- triple, {P}S{Q}

P: pre-condition

Q: post-condition

Both of P and Q are assertions

- fairness

》unconditional fairness, every unconditional atomic action that is eligible is eventually executed

》weak fairness

》strong fairness

weak fairness VS strong fairness

Weak fairness:you need to raise a flag saying that you wanna access, and you have to keep this flag up all the time until you get access. The condition should be true from some point and remains true until the action is taken.

Strong Fairness:you need to raise the flag every now and then, and you know that eventually you'll get access

http://cs.stackexchange.com/questions/52824/example-for-weakly-fair-v-s-strongly-fair-scheduling-in-concurrency


Strategy for solving concurrent problems:

critical section -> mutual exclusion, absence of deadlock, eventual entry, no unnecessary delay, global invariant

busy waiting, a process would repeatedly check a condition until it becomes true, but it always takes time to carry out checking

Test and set:

TS(lock, cc) <cc = lock, lock = true, return cc>

R(lock) <lock = false>

boolean cc;

do{

while(TS(lock, cc));

enter critical section;

R(lock);

}while(1);


1. https://www.zhihu.com/question/25532384

2. the materials of Concurrent && Distributed Systems course


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值