Race Condition: A situation where
1. several processes access and manipulate the same data concurrently
2.the outcome of the execution depends on the particular order in which the access takes place
Critical Section: a segment of code that accesses shared resource(s) – code, not resource(s) itself
If process p1 is executing in its critical section, then no other processes can be executing in their critical sections
Semaphore: a synchronization tool
A semaphore contains an integer variable that is accessed only through two standard operations: acquire() and release()
acquire(){ while(value <= 0) ; //no-op value--; } release(){ value++; } //上面两个操作都必须是原子操作,并且semaphore上的acquire()和release()不能同时执行
如果value值在运行时只能在0和1之间变化,那Semaphore就起到了mutex lock的作用:
(Note1: mutex = lock = binary semaphore)
(Note2: mutex is a contraction of mutually exclusive)
Semaphore sem = new Semaphore(1); sem.acquire(); ... //critical section sem.release(); ... //remainder section
Spinlock (自旋锁): 是一种Semaphore,它能造成一种情形:
一个进程由于等待锁而不停地空转。这个空转的感觉就像在”spin”(自旋)
也就是说:锁本身并不会空转. The process will spin when it waits for the lock.
由于自旋时进程仍处于运行态,所以很浪费CPU.
Deadlock: a situation where two or more processes are waiting indefinitely for an event that can be caused by one of the waiting processes.
Starvation, or Infinite Blocking:
1. 一种状况:进程永远获取不到锁
2. 如果锁的等待队列采取LIFO模式,就会靠成这种状况