Site icon T4Tutorials.com

Semaphores, counting semaphores, binary semaphores in Operating systems OS

What are semaphores?

Semaphores are the variables.
Semaphores are used for signaling among processes.
Three kinds of operations are performed on semaphores;

  1. To initialize the semaphore
  2. To increment the semaphore value
  3. To decrement the semaphore value

What are binary semaphores?

Binary semaphores take only the values between 0 to 1.

What are counting semaphores?

Counting semaphores have the non-negative integer value.

How can processes get the critical section?

Wait and Signal Operations in Semaphores

We can implement Wait and Signal Operations in process synchronization. The actual purpose of Wait and Signal operation is to get mutual exclusion.

The Wait Operation in Semaphores

The wait operation decrements the value of Semaphore if it is positive. If Semaphore is negative or zero, then no operation is performed.

wait(Semaphore)
{
while (Semaphore<=0);

Semaphore–;
}

Signal Operation in Semaphores

The signal operation increments the value of Semaphore.

signal(Semaphore)
{
Semaphore++;
}

Semaphores implementation in C++

Now, let’s see theC++ Program for Semaphores.

#include<iostream>
#include<mutex>
using namespace std;
struct semaphore
{
    int mutex;
    int rcount;
    int rwait;
    bool wrt;
};
void addReader(struct semaphore *s)
{
    if(s->mutex == 0 && s->rcount == 0)
    {
        cout<<"Sorry, File isopen in Write mode.\nNew Reader added to queue."<<endl;
        s->rwait++;
    }
    else
    {
       cout<<"Reader Process added."<<endl;
        s->rcount++;
        s->mutex--;
    }
return ;
}
void addWriter(struct semaphore *s)
{
    if(s->mutex==1)
    {
        s->mutex--;
        s->wrt=1;
        cout<<"\nWriter Process added."<<endl;
    }
    else if(s->wrt) 
        cout<<"Sorry, Writer already operational."<<endl;
    else
        cout<<"Sorry, File open in Read mode."<<endl;
return ;
}

void removeReader(struct semaphore *s)
{
    if(s->rcount == 0) cout<<"No readers to remove."<<endl;
    else
    {
       cout<<"Reader Removed."<<endl;
        s->rcount--;
        s->mutex++;
    }
return ;
}
void removeWriter(struct semaphore *s)
{
    if(s->wrt==0) cout<<"No Writer to Remove"<<endl;
   else
   {
        cout<<"Writer Removed"<<endl;
        s->mutex++;
        s->wrt=0;
  if(s->rwait!=0)
{
    s->mutex-=s->rwait;
    s->rcount=s->rwait;
    s->rwait=0;
    cout<<"waiting Readers Added:"<<s->rcount<<endl;
}
}
}
int main()
{
struct semaphore S1={1,0,0};
while(1)
{
cout<<"Options"<<endl<<"1. Add Reader."<<endl<<"2. Add Writer."<<endl<<"3. Remove Reader."<<endl<<"4. Remove Writer."<<endl<<"5. Exit.<<Choice : "<<endl;
int choice;
cin>>choice;
switch(choice)
{
case 1: addReader(&S1); break;
case 2: addWriter(&S1); break;
case 3: removeReader(&S1); break;
case 4: removeWriter(&S1); break;
case 5: cout<<"\n\tGoodBye!";break;
default: cout<<"\nInvalid Entry!";
}
}
return 0;
}

Output

Figure: Semaphores implementation in OS

For example, in this example, we can see in our output screenshot that when we select choice 1, then the program gives a chance to the Reader process to enter into the critical section. Now, if the writer process wants to enter into the critical section, then semaphore stops it. When the reader process is removed, then semaphore can allow the writer process to enter into the critical section.

Difference between Binary Semaphore  VS Counting Semaphore

Let’s see some common difference between binary  and counting semaphore:

Binary Semaphore Counting Semaphore
Mutual exclusion No mutual exclusion
Semaphore Value is only 0 and 1 Semaphore Value is any integer value
The only process at the same time. Can provide a set of Processes
Only one slot More than one slot

Advantages of Semaphores

Some of the advantages of semaphores are mentioned below:

Disadvantages of Semaphores

Some of the disadvantages of semaphores are mentioned below;

Video Lecture

Exit mobile version