Critical Section: Attempted Solution
Shared Variable:
int v=0; /* v==0 => critical section OPEN; v==1 => critical section CLOSED */
Code: At p_i in {1,...,n}:
while (1) {
/* trying region */
while (v == 1) /* do nothing */ ;
v = 1;
/* critical section (region) */
x = 0;
x++;
printf x;
v = 0;
/* remainder region */
}
- what is wrong? what if critical section is: dial_phone("555-1212")?
- how can it be fixed?
- is this spinlock good or bad?
Critical Section: General Problem
- n processes each with segment of code called critical section
- one process changes common (shared) variables, writes to a file.
- no other process is allowed to execute in its critical section
- execution of critical sections is mutually exclusive in time
- one solution: semaphore
- lets one process into the critical section
- puts other processes in a semphore queue (no busy waiting)
- process is finished: head of FIFO queue enters section