Print Friendly and PDF
Consider the standard producer-consumer problem. Assume, we have a buffer of 4096 byte length. A producer thread will collect the data and writes it to the buffer. A consumer thread will process the collected data from the buffer. Objective is, both the threads should not run at the same time. solve it via Semaphore and mutex via consumer-producer problem.

The producer-consumer problem:

Consider the standard producer-consumer problem. Assume, we have a buffer of 4096 byte length. A
producer thread will collect the data and writes it to the buffer. A consumer thread will process the collected
data from the buffer. Objective is, both the threads should not run at the same time.
solve it via Semaphore and  mutex  via consumer-producer problem.

Some Basic Concepts:

Using Mutex:

A mutex provides mutual exclusion, either producer or consumer can have the key (mutex) and proceed withtheir work. As long as the buffer is filled by producer, the consumer needs to wait, and vice versa.At any point of time, only one thread can work with the entire buffer. The concept can be generalized usingsemaphore.


Using Semaphore:


A semaphore is a generalized mutex. Instead of single buffer, we can split the 4 KB buffer into four 1 KBbuffers (identical resources). A semaphore can be associated with these four buffers. The consumer andproducer can work on different buffers at the same time.Binary semaphore:


Misconception:


There is an ambiguity between binary semaphore and mutex. We might have come across that a mutex is
binary semaphore. But they are not! The purpose of mutex and semaphore are different. May be, due to
similarity in their implementation a mutex would be referred as binary semaphore.
Strictly speaking, a mutex is locking mechanism used to synchronize access to a resource. Only one task (can
be a thread or process based on OS abstraction) can acquire the mutex. It means there will be ownership
associated with mutex, and only the owner can release the lock (mutex).
Semaphore is signaling mechanism (“I am done, you can carry on” kind of signal). For example, if you are
listening songs (assume it as one task) on your mobile and at the same time your friend called you,
an interrupt will be triggered upon which an interrupt service routine (ISR) will signal the call processing task
to wakeup.

Implementation By a Semaphore 

#include <iostream>
#include <unistd.h> 
#include <sys/types.h> 
#include <errno.h>
#include <stdlib.h> 
#include <pthread.h> 
#include <string.h> 
#include <semaphore.h>

using namespace std;  
int b,x;
pthread_t tid1,tid2; 
sem_t mutex;  
void* job1(void *arg)
{    sem_wait(&mutex);   
           
    cout<<"Enter polynomial values 4X^2 + 5X";
cin>>x;

b=x*x*4;
    sem_post(&mutex); 
    return NULL;
 }  
void* job2(void *arg) 
{
sem_wait(&mutex); 
b=b + 5*x;
x=b;

sem_post(&mutex);
}

int main(void) {  
   int i = 0;
 sem_init(&mutex, 0, 1);  
    pthread_create(&tid1, NULL, &job1, NULL);
    pthread_create(&tid2, NULL, &job2, NULL);
    pthread_join(tid1, NULL);   
    pthread_join(tid2, NULL);
    cout<<"Result"<<x<<"\n";    
    sem_destroy(&mutex);   
    return 0; } 

Implementation By a Mutex

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
using namespace std;  
int x,b;  
pthread_t tid1,tid2; 
pthread_mutex_t lock;  
void* job1(void *arg) 
{   
pthread_mutex_lock(&lock);        
    cout<<"Enter polynomial values 4X^2 + 5x";
cin>>x;
b=x*x*4;
     pthread_mutex_unlock(&lock);
    return NULL;
 }  
void* job2(void *arg) 
{
pthread_mutex_lock(&lock); 
b= b+ 5*x;
x=b;
 pthread_mutex_unlock(&lock);
}
 int main(void)
{  
 int i = 0;    
 int err;  
  if (pthread_mutex_init(&lock, NULL) != 0)   
  {        
cout<<"mutex init failed";
 return 1;    
 }  
pthread_create(&tid1, NULL, &job1, NULL);
pthread_create(&tid2, NULL, &job2, NULL);

pthread_join(tid1, NULL);    
pthread_join(tid2, NULL);  

cout<<"Result="<<x<<"\n";    
pthread_mutex_destroy(&lock);  
    return 0; 
} 


zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: