操作系统之进程同步

critical section
mutex:互斥
progress:前进,没有死锁,如果没有进程在临界区,选择哪个进入临界区的次数是有限的
bounded waiting:有限等待,没有饿死,

等待:
 busy waiting
      锁变量
      严格轮转法
      peterson's solution
hardware solution
       关中断(多处理器,非抢占无效)
       test ans lock(TSL)
 睡眠与等待
       semaphore(同步,初值为0,两个程序,一个释放,另一个等待释放,P ,V ,重点:生产者消费者几种代码)
       blocking queue
       monitor
       message passing
     
    





1.不行

if (!lock) {

                                        if (!lock) {                

                                            
lock = true;

// critical section                           
lock = false;
}

Context switch after the
testing and before the
locking


2.不行,直接跳过了了某一个的临界区

Lock before testing? How?
lockA = true;                     lockB = true;

if (!lockB) {                         if (!lockA) {
// critical section                 // critical section
}                                                }
                                 
lockA = false;                     lockB = false;


Context switch after the locking and before the testing


3 不行

Lock before testing? How?
lockA = true;                    lockB = true;
while (lockB); //X             if (!lockA) { //Y
// critical section               /* critical section */}
lockA = false;                   lockB = false;

It works!
at X: if B is not locked, enter CS; otherwise wait;
at Y: if A is not locked, enter CS; otherwise leave
Not symmetric!

 



4 可以

Peterson’s algorithm
while (true) {
lock[i] = true;
turn = j;
while (lock[j] && turn == j);
// critical section
lock[i] = false;
// remain section
}
It works, but rathercomplicated!










你可能感兴趣的:(操作系统之进程同步)