# Fot T1
while(1):
flag[0] = T
turn = 1
while(turn == 1 && fla[1] = T):
# CS
flag[0] = F
# Fot T2
while(1):
flag[1] = T
turn = 0
while(turn == 0 && fla[0] = T):
# CS
flag[1] = F
do {
wait(empty); // Wait until empty > 0, then empty -> value
wait(mutex);
// CS, Add data to buffer
signal(mutex);
signal(full); // Increment full -> value
} while (1);
do {
wait(full); // Wait until full > 0, then full--
wait(mutex);
// CS, Remove data from buffer
signal(mutex);
signal(empty); // Increment empty
} while (1);
do {
wait(wrt);
// Do Write Operation
signal(wrt);
} while (1);
do {
wait(mutex); // To mutex readcount variable
rc++;
if (rc == 1) {
wait(wrt); // Ensures no writer can enter if there is even 1 writer
}
signal(mutex);
// CS, Reader is reading
wait(mutex);
rc--; // A Reader leaver
if (rc == 0) {
// No reader is left in CS
signal(wrt); // Writer can enter
}
signal(mutex); // Reader leaves
} while (1);
// Each fork is a binary semaphore
do {
wait(fork[i]);
wait(fork[(i+1) % 5]);
// Eat
signal(fork[i]);
signal(fork[(i+1) % 5]);
// Think
} while(1);
// Can lead to Deadlock