Besides its usage for CS problem, semaphores can also be used for synchronization of the processes. For example consider two concurrent processes: p1, p2. We require that statement s2 of p2 will be executed after statement s1 of p1. Let p1 and p2 share a common semaphore ‘synch’, initialized to 0. Semaphores can be used for this synchronization
problem as follows :

semaphore synch=0;
void P1(void)
{ ...
 down(synch)
 S1
}
 semaphore synch=0;
void P1(void)
{ ...

 S1
 up(synch)
}
Share with : Share on Linkedin Share on Twitter Share on WhatsApp Share on Facebook