IPC 全称 Interprocess communication,意思是进程间通信。
IPC要解决三个问题:
- The first was alluded to above: how one process can pass information to another.
- The second has to do with making sure two or more processes do not get in each other’s way。 (MUTUAL EXCLUSION)
- The third concerns proper sequencing when dependencies are present。
其中,如果是线程间的通信,只需要解决第二、三个问题,因为同一个进程之间的线程是共享address space的,传递信息对它们来说不成问题。
Basic Concept
什么是Race Conditions呢?简单来说就是两个线程同时想占用一个资源,这种情形就是Race Conditions。
什么是Critical Region呢?That part of the program where the shared memory is accessed is called the critical region or critical section.
如果我们能让两个process不同时进入critical region,我们就能避免race condition,但是这样远远不够,需要达到四个conditions来得到一个好的solution:
- Mutual Exclusion (互斥)
No other process must execute within the critical section while a process is in it. - Progress(空闲让进) No process running outside its critical region may block other processes.
- Bounded Waiting(有限等待) No process should have to wait forever to enter its critical region.
- Speed and Number of CPU No assumption may be made about speeds or number of CPUs.
以下开始介绍各种方法,并且会在最后对各个方法进行总结。
Mutual Exclusion with Busy Waiting
Disabling Interrupts
为了避免race conditions,最简单的做法就是让process 拥有disable interrupts的权限,这样没有clock interrupts发生,也就不会发生竞争了,但是这样做有以下的缺点:
- It’s unwise to give user processes the power to turn off interrupts。简而言之就是用户的权限太大了这样不好。
- 这个方法只在single-CPU的系统中适用。
Lock Variable
每次进入critical region之前加“锁”,以避免其它process进入。
缺点:并不能避免race conditions,在processes同时进入时还是会出现问题。
分析:待施工,为什么还是会出现问题?
Strict Alternation
用以下的方式处理:

但是不满足第三个条件空闲转进(progress)。
分析:为什么不满足?待施工
Peterson’s Solution
这个solution很强大,可以说解决了大部分的问题。

在process进入critical region之前先调用enter_region,出来之前调用leave_region。
分析:为什么它这么有效呢?待施工
The TSL Instruction
这个方法只需要你明白这条语句做了什么事情就可以了:
TSL RX,LOCK
It reads the contents of the memory word lock into register RX and then stores a nonzero value at the memory address lock.
这个solution其实就是保证了while和lock之间不会发生context switch,也就是这个操作是atomic的。
Avoid Busy Waiting
以上介绍的方法都是busy waiting的,所谓busy waiting,以lock举例,当其中某个process被禁止进入critical region的时候,是一直在while循环里执行的,所以,即使此时的while循环不对实际的任务有任何帮助,
但是它还是在占用系统资源,这就是所谓的busy waiting。
而现在要介绍的一些方法,就能避免陷入busy waiting。
Sleep and Wakeup
sleep和wakeup是系统的一种primitive(原语),definition非常简单。
Sleep is a system call that causes the caller to block, that is, be suspended until another process wakes it up. The wakeup call has one parameter, the process to be awakened.
用sleep和wakeup解决producer-consumer问题

但是这个解决方案也存在问题,原因它并未对count进行保护,假设这样一种情况,count=0,此时consumer判断count==0 = true,准备执行sleep()语句的时候突然开始执行producer进程,此时的count还是0,
它会一直执行到count==1,然后执行wakeup,但这时的consumer不处在sleeping状态,于是浪费了一次wakeup,导致的结果就是两者永远blocked。
解决这个问题的方法之一是增加一个wakeup waiting bit,如果一个wakeup信号被传递到某个awake进程的时候,就将wakeup waiting bit设为1,之后在某个进程打算sleep的时候检测wakeup waiting bit,如果为1,则不会睡觉 (起来high),并将bit设为0。
这个解决方案也还是存在缺陷,如果有多个进程(大于2个)同时要进行管理,这种解决方案就会显得低效了。
Semaphores
Semaphores信号量是本章的重中之重,下面将介绍semaphores的基本概念并且将其应用于多种场景。
什么是Semaphores呢?暂且可以先把semaphores当成一个个的值(大于等于0),对semaphores可以进行两种操作,down和up。
对semaphores进行down操作的时候如果semaohore=0,那么进程就会block,up操作不会引起进程的block。
那么如何用semaphores解决producer-consumer问题呢?

上面的程序含金量是十足的,有很多的地方可供解析,下面以问答的形式来解析。
-
producer里的down(&mutex)和down(&empty)能互换吗? 不行!如果互换,可能会导致出现block forever的情况。 互换之后,假设此时的empty=0,full=N,producer先进入critical region,并且down(&empty)不让其它进程进入,然而此时producer试图down(&empty),blocked,consumer也blocked,两者不能互相唤醒,也就永远处于blocked状态了。 -
这个程序用到了三个semaphores:mutex,empty和full,各自的作用是什么? mutex是互斥semaphore,为了不让其它进程进入critical region。empty和full是同步信号量,down(&empty)在producer中出现,也就是说,先要producer生产产品,否则consumer一直blocked,这就是一个同步。 并且empty的初始值是N,还规定了buffer的大小是N。
之后会专门写一章来讲semaphores的应用。
Monitors
既然Semaphores已经能解决问题了,为什么还会有Monitors的出现呢?因为人类实在太懒了,使用semaphores虽然能解决问题,但是你必须对semaphores非常熟悉,比如前面提到,两个down的顺序如果颠倒了, 一样会出现问题,monitors把这些东西都封装起来,封装成一个一个procedures,让你去调用。
A monitor is a collection of procedures,variables,and data structures that are all grouped together in a special kind of module or package.Processes may call the procedures in a monitor whenever they want to, but they cannot directly access the monitor’s internal data structures from procedures declared outside the monitor.