Lecture 6: Synchronization Flashcards
define race condition
several process access and manipulate the same data at the same time so that the outcome of execution is depends on the order of process executioln
what is critical section
- process may be changing common variables, updating table, writing file, …
- critical section problem design protocol to solve race condition
- each process need to ask permission to enter critical section in entry section, follow by exit section and remaining section
define mutual exclusive
if a process,P1 is executing in its critical section, no other process can enter its critical section
define bound waiting
bound must exist when process are allowed to enter their critical section
- after request is made
- before request is granted
Peterson’s Solution
- used to restrict two process to alternate execute between critical section and remaining section
- assume LOAD and STORE cannot be interrupt
- sharing 2 variables: int turn and boolean flag[2]
int turn variables
determine whose turn to enter the critical section
boolean flag
determine if a process is ready to enter the critical section
critical section problem’s solution requirement
- bound waiting requirement
- progress requirement
- mutual exclusive
other solution for critical section problem
- lock
- test memory word and set value: test_and_set()
- swap content of two memory word: compare_and_swap()
how Mutex Lock work
- process acquire a lock before entering critical section
- then release the lock after exit the critical section
what is semaphore S
an integer variable that can only accessed through wait() and signal()
counting semaphore
- initialized to number of resources available
- process perform a wait() in order to use the resource
- process perform a signal when release the resource
- all resources are being used when semaphore count = 0
- process will be blocked until semaphore count >0
3 classical problems of synchronization
- bounded-buffer problem
- reader and writer problem
- dining’s philosophers problem
problem with dining philosophers
- possibly create deadlock
- all philosophers hungry at the same time
how to prevent deadlock in dining philosophers problem
- allow at most n-1 philosophers
- allow pick up when both chopstick are available
- asymmetric solution: odd number pick left first and even number pick right first