Synchronization Problem
Synchronization Problem
Critical Sections
We only want to prevent pi and pj from interfering with one another; this
prevents any other process pk to execute
Mutual exclusion:
Only one process at a time in the Critical Section (CS)
P(semaphore s):
get( ) P( ) wait( )
disable_interrupts();
{ s.value--;
if (s.value < 0) pthread_mutex_lock()
{place this process in s.queue;
block this process
}
enable_interrupts();
}
V(semaphore s):
{disable_interrupts();
s.value++; release( ) V( ) signal( )
if (s.value <= 0)
{ remove a process P from s.queue; pthread_mutex_unlock()
wakeup P;
}
enable_interrupts();
}
Shared Account Problem
Pi() Pj()
{ {
. . . . . .
/* Enter the CS */ /* Enter the CS */
P(mutex); P(mutex);
balance += amount; balance -= amount;
V(mutex); V(mutex);
. . . . . .
} }
semaphore mutex = 1;
pthread_create(P0, 0);
pthread_create(P1, 0);
Processing Two Critical Sections
shared lock1 = FALSE; shared lock1 = FALSE;
shared lock2 = FALSE; shared lock2 = FALSE;
. . . . . .
Deadlock may occur
if locks are not used properly!
shared boolean lock1 = FALSE;
shared boolean lock2 = FALSE;
. . . . . .
P(lock1); P(lock2);
P(lock2); P(lock1);
<update length>; <add element>;
V(lock2); Vlock1);
V(lock1); V(lock2);
. . . . . .
Classical Problems of
Synchronization
Classical Problems of Synchronization
• Bounded-Buffer Producer/Consumer Problem
• Readers and Writers Problem
• Dining Philosophers Problem
Bounded-Buffer Producer/Consumer Problem
• Shared data:
semaphore full, empty, mutex
• Initially:
full = 0, empty = n, mutex = 1
where n is the buffer size
Bounded-Buffer Producer/Consumer Problem
} while (TRUE);
Bounded-Buffer Producer/Consumer Problem
R
R
R
} while (TRUE);
First Readers/Writers Problem
do{ A reader will wait only if a
writer is currently writing.
P(mutex);
Note that if readcount ==
readcount++;
1, no reader is currently
if (readcount == 1)
reading and thus that is
P(wrt);
the only time that a reader
V(mutex); has to make sure that no
We must make sure … writer is currently writing
that readers update reading is performed (i.e., if readcount > 1, there
the shared variable … is at least one reader
readcount in a P(mutex); reading and thus the new
mutually exclusive readcount--; reader does not have to
manner if (readcount == 0) wait)
V(wrt);
V(mutex);
} while(TRUE);
First Reader/Writer Solution
Reader: Writer:
do{
P(mutex);
do {
readcount++;
if (readcount == 1) P(wrt);
P(wrt); …
V(mutex); writing is perform
… …
reading is performed V(wrt);
… } while (TRUE);
P(mutex);
readcount--; • First reader competes with writers
if (readcount == 0)
V(wrt);
• Last reader signals writers
V(mutex);
• Any writer must wait for all readers
} while(TRUE); • Readers can starve writers
• “Updates” can be delayed forever
not desirable!
Favor Writer
reader() { writer() {
while(TRUE) { while(TRUE) {
<other computing>; <other computing>;
P(mutex2);
P(RD); writeCount++;
P(mutex1); if(writeCount == 1)
readCount++; P(RD);
if(readCount == 1) V(mutex2);
P(WRT); P(WRT);
V(mutex1); access(resource);
V(RD); V(WRT);
P(mutex2)
access(resource); writeCount--;
P(mutex1); if(writeCount == 0)
readCount--; V(RD);
if(readCount == 0) V(mutex2);
V(WRT); }
V(mutex1); }
}
}
• Writers can starve readers
int readCount=0, writeCount=0; • “Reads” can be delayed forever
semaphore mutex1=1, mutex2=1;
semaphore RD=1,WRT=1
Not desirable, either !
Dining Philosophers Problem