并发编程基础 Lecture Notes(二)

本文深入探讨了进程与线程的概念及其区别,包括它们在内存管理、资源共享和调度方面的特性。此外,还讨论了线程的优势,如提高CPU利用率、减少上下文切换开销以及实现并行与串行之间的平衡。

读写内存,硬盘的速度始终不及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


源码地址: https://pan.quark.cn/s/3916362e5d0a 在C#编程平台下,构建一个曲线编辑器是一项融合了图形用户界面(GUI)构建、数据管理及数学运算的应用开发任务。 接下来将系统性地介绍这个曲线编辑器开发过程中的核心知识点:1. **定制曲线面板展示数据曲线**: - 控件选用:在C#的Windows Forms或WPF框架中,有多种控件可用于曲线呈现,例如PictureBox或用户自定义的UserControl。 通过处理重绘事件,借助Graphics对象执行绘图动作,如运用DrawCurve方法。 - 数据图形化:通过线性或贝塞尔曲线连接数据点,以呈现数据演变态势。 这要求掌握直线与曲线的数学描述,例如两点间的直线公式、三次贝塞尔曲线等。 - 坐标系统与缩放比例:构建X轴和Y轴,设定坐标标记,并开发缩放功能,使用户可察看不同区间内的数据。 2. **在时间轴上配置多个关键帧数据**: - 时间轴构建:开发一个时间轴组件,显示时间单位刻度,并允许用户在特定时间点设置关键帧。 时间可表现为连续形式或离散形式,关键帧对应于时间轴上的标识。 - 关键帧维护:利用数据结构(例如List或Dictionary)保存关键帧,涵盖时间戳和关联值。 需考虑关键帧的添加、移除及调整位置功能。 3. **调整关键帧数据,通过插值方法获得曲线**: - 插值方法:依据关键帧信息,选用插值方法(如线性插值、样条插值,特别是Catmull-Rom样条)生成平滑曲线。 这涉及数学运算,确保曲线在关键帧之间无缝衔接。 - 即时反馈:在编辑关键帧时,即时刷新曲线显示,优化用户体验。 4. **曲线数据的输出**: - 文件类型:挑选适宜的文件格式存储数据,例如XML、JSON或...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值