OS Process IPC Semaphore
OS Process IPC Semaphore
Dr. N.Nalini
SCOPE
VIT
Semaphores
• Previous solutions are complicated and generally inaccessible to
application programmers.
• OS designers build software tools to solve critical section problem.
• Synchronization tool provided by the OS that does not require busy
waiting.
• Semaphores -- Synchronization tool that provides more sophisticated
ways (than Mutex locks) for processes to synchronize their activities.
• Semaphore S – integer variable can only be accessed via two
indivisible (atomic) operations
wait ( ) and signal ( ) - Originally called P() and V() - dutch word
P()- proberen (to test) and
V()- verhogen (increment)
Semaphores
• Shared variable : S
• Definition of the wait ( ) operation – sleep()/down()
wait (Semaphore S){ //to test
while (S <= 0) ; // busy wait
S = S – 1;
}
• Definition of the signal ( ) operation – wake-up()/up()
signal (Semaphore S){ //to increment
S = S + 1;
}
When one process modifies the semaphore value, no other process can simultaneously modify the
semaphore value. Also in wait(S), the testing of the integer value of S (S <= 0), as well as possible
modifications (S=S-1), must be executed without interrruption.
Critical Section of n Processes
• Shared data: semaphore mutex = 1;
• Process Pi
do {
wait(mutex);
critical section
signal(mutex);
remainder section
} while (true);
Types of semaphores
struct Semaphore
{
int value;
int List[n];
};
Semaphore S;
S.value--;
if (S.value < 0) {
add this process to S.List;
block(); //suspends, waiting state
}
}
signal(S) {
S.value++;
if (S.value <= 0) {
remove a process P from S.List;
wakeup(P); //resumes, ready state
}
}
• Here, semaphore value may be negative. If it is, its magnitude is the number processes waiting on
that semphore.
• abs(value) shows the number of blocked processes. value < = 0 means that there is a process to
wakeup.
Semaphore as a General Synchronization Tool
process Pi process Pj
instruction A wait(flag);
signal(flag); instruction B
Note:
Semaphores can solve various synchronization problems. System must quarantee that no
two processes can execute wait() and signal() operations on the same semaphore at the
same time. Two operations are executed atomically.
Classical Problems of Synchronization
• Dining-Philosophers Problem
Bounded-Buffer Producer-Consumer Problem
• n buffers, each can hold one item
• Shared data
int n; /*# of slots in the buffer*/
Three semaphores full = 0, /*# of full buffers*/
empty = n, /*# of empty buffers*/
mutex = 1; /*for access to the buffer pool */
Producer process
do { Consumer process
… do {
produce an item in nextp wait(full);
… wait(mutex);
wait(empty); //decrement empty …
wait(mutex); //lock remove an item from buffer to nextc
… …
add nextp to buffer signal(mutex);
… signal(empty);
signal(mutex); //release …
signal(full); //increment full consume the item in nextc
} while (true); …
} while (true);
Readers-Writers Problem
• A data set is shared among a number of concurrent processes
• Readers – only read the data set; they do not perform any updates
• Writers – can both read and write
• Problem – allow multiple readers to read at the same time
• Only one single writer can access the shared data at the same time
• Several variations of how readers and writers are considered – all
involve some form of priorities
• Shared Data
• Data set
• Semaphore rw_mutex initialized to 1
• Semaphore mutex initialized to 1
• Integer read_count initialized to 0
Readers-Writers Problem
Shared data
• int read_count = 0; /* number of readers currently reading the object */
• Two semaphores mutex = 1, /* controls access to read_count*/
rw_mutex = 1; /* controls entry to a writer or first or last reader */
Writer process Reader process
do{
wait(mutex); //reader wants enter to CS
do{ read_count++; //no.of readers incr by 1
wait(rw_mutex); if (read_count == 1) //first reader
wait(rw_mutex); //writer must have to wait
… signal(mutex);
writing is performed …
reading is performed
… …
signal(rw_mutex); wait(mutex); //reader wants to leave after reading
} while (true); read_count--;
if (read_count == 0) //last reader
signal(rw_mutex);
signal(mutex);
} while(true);
Dining-Philosophers Problem
• Five philosophers are sitting around a
table. Each philosopher has a plate in
front him/her. Between each plate
there is a chopstick.
• A philosopher is either “thinking” or
“eating”. When a philosopher gets
hungry he/she tries to get the 2
chopsticks on his/her left and right.
Having obtained both chopsticks the
philosopher can eat.They cannot have
both chopstick at the same time.
• When finished eating, the philosopher
puts both chopsticks down, and
resumes thinking.
Dining-Philosophers Problem
Shared data
semaphore chopstick[5]={1,1,1,1,1};
Philosopher i:
do {
wait(chopstick[i]);
wait(chopstick[(i+1) % 5]);
…
//eat
…
signal(chopstick[i]);
signal(chopstick[(i+1) % 5]);
…
//think
…
} while (true);
Dining-Philosophers Problem
procedure P2 (…) { …. }
• Variables
semaphore mutex
mutex = 1
wait(mutex);
…
body of P;
…
signal(mutex);
• Variables
wait(mutex);
…
body of P;
…
if (next_count > 0)
signal(next)
else
signal(mutex);
x_count++;
if (next_count > 0)
signal(next);
else
signal(mutex);
wait(x_sem);
x_count--;
Implementation (Cont.)
• The operation x.signal() can be implemented as:
if (x_count > 0) {
next_count++;
signal(x_sem);
wait(next);
next_count--;
}
Resuming Processes within a Monitor
• If several processes queued on condition variable x, and
x.signal() is executed, which process should be resumed?
• FCFS frequently not adequate
• Use the conditional-wait construct of the form
x.wait(c)
where:
– c is an integer (called the priority number)
– The process with lowest number (highest priority) is scheduled
next
Single Resource allocation
• Allocate a single resource among competing processes using priority numbers that specifies the
maximum time a process plans to use the resource
R.acquire(t);
...
access the resurce;
...
R.release;
R.acquire(t);
...
access the resurce;
...
R.release;
initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
Solution to Dining Philosophers (Cont.)
Each philosopher “i” invokes the operations pickup() and
putdown() in the following sequence:
DiningPhilosophers.pickup(i);
DiningPhilosophers.putdown(i);