C-CS316 - Lect07 - Process Synchronization (Part 2)
C-CS316 - Lect07 - Process Synchronization (Part 2)
Operating Systems
C-CS316 Spring 2024
LECTURE 07:
Process Synchronization (Part 2)
Dr. Basma Hassan Dr. Ahmed Salama
[email protected] [email protected]
• Variables
semaphore mutex
mutex = 1
wait(mutex);
…
body of P;
…
signal(mutex);
• condition x, y;
• Variables
semaphore mutex; // (initially = 1)
semaphore next; // (initially = 0)
int next_count = 0; // number of processes waiting inside the monitor
• The process with the shortest time is allocated the void acquire(int time) {
resource first if (busy)
x.wait(time);
• Let R is an instance of type ResourceAllocator busy = true;
(monitor) }
• Access to ResourceAllocator is done via: void release() {
busy = false;
R.acquire(t); x.signal();
... }
access the resource;
... initialization code() {
R.release; busy = false;
}
• Where t is the maximum time a process plans to use the }
resource
Bounded-Buffer Problem
Dining-Philosophers Problem
• Semaphore Solution
• The structure of Philosopher i :
while (true){
wait (chopstick[i]);
wait (chopstick[(i + 1) % 5]);
signal (chopstick[i]);
signal (chopstick[(i + 1) % 5]);
DiningPhilosophers.pickup(i);
DiningPhilosophers.putdown(i);
P0 P1
wait(S); wait(Q);
wait(Q); wait(S);
... ...
signal(S); signal(Q);
signal(Q); signal(S);