• C

# Semaphore

Hi all,
I have 2 threads : thread1 and thread2. How can I use semaphore to make sure thread1 will always run before thread2? Thanks a lot for details and explainations.
###### Who is Participating?

Commented:
Assuming
P(Semaphore s)
{
await s > 0, then s := s-1; /* must be atomic once s > 0 is detected */
}

V(Semaphore s)
{
s := s+1;   /* must be atomic */
}

sem1 = -1

V(sem1)   sem1=0
operations
V(sem1)  sem1=1

P(sem1) sem1>0 only if thread 1 has finished
operations
V(sem1)
0

Commented:
Here is an example:

The code implements the problem of N Missionaries and N Cannibals that whants to cross the river with a boat. The problem is about synchronize the threads which are the Missionaries and Cannibals. Here we will have 2 condition variables which will represent the missionaries and cannibals that could be loaded on the boat based on the condition: the number of cannibals in a boat cannot be greater than the number of missionaries.

The condition variables work just like semaphores, but the concept of having semaphores is old. If I remember correct, Dijkstra introduced the concept in the '60s and semaphores are a bit rudimentary regarding mutual exclussion of the "paralel" threads. I quoted the paralel word because we all know that if you have a single core CPU then at a certain time only one process is running. The "paralel" view is a virtual mechanism provided by Operating System.

How does condition variable works?

The mechanism is very simple: Lock and Unlock

When you whant to block threads use a condition variable that will solve also the race condition for that shared resource. So, if you whant to block all threads that have to modify a variable then all you have to do is to call:

The code:

#include <stdio.h>
#include <semaphore.h>

int numM = 0, numC = 0;

void RowBoat(const int m, const int c){
printf("Boat with: %d missionaries and %d cannibals\n",m,c);
}

void *MissionaryArrives(void *arg){

LABEL0:      printf("M %d - %d\n",numM,numC);

if(numC<=1){
printf("M\n");
numM++;
}
else{
printf("M - sleep\n");
printf("M - woke up!\n");
goto LABEL0;
}
if((numM==3 && numC==0)||(numM==2 && numC==1)||(numM==0 && numC==3)){
RowBoat(numM,numC);
numM=0;
numC=0;
}

}

void *CannibalArrives(void *arg){

LABEL1:      printf("C %d - %d\n",numM,numC);
if((numM<=2 && numC==0) || (numM==0)){
printf("C\n");
numC++;
}
else{
printf("C - sleep\n");
printf("C - woke up!\n");
goto LABEL1;
}
if((numM==3 && numC==0)||(numM==2 && numC==1)||(numM==0 && numC==3)){
RowBoat(numM,numC);
numM=0;
numC=0;
}

}

int main(int argc, char **argv){
if(argc==2){
int n,i;
n=atoi(argv[1]);
if(n>100){
printf("Number of missionaries/cannibals cannot exceed 100.\n");
exit(1);
}
for(i=0;i<n;i++){

}
/*for(i=0;i<cannibals;i++){
}*/
for(i=0;i<n;i++){
}
/*for(i=0;i<cannibals;i++){
}*/
}
else{
printf("usage: %s <number of missionaries>/<number of cannibals>\n",argv[0]);
}
printf("%d - %d\n",numM,numC);
return 0;
}
0

Author Commented:
I need to read carefully. SO i give each 250 hihi. Thanks a lot.
0

Author Commented:
Hi sunnycoder,
I read your psedocode but i do'nt get it. I am reading the dinasour OS book about semaphore , but i don't get it too.
0

Commented:
In sunnycoder's pseudo code, try to visualize what happens.