Tuesday, April 29, 2014

C/C++ CODE FOR READER WRITER PROBLEM: WRITER's PRIORITY

READER WRITER PROBLEM USING SEMAPHORES : WRITER's PRIORITY

Last Updated On: 7th Dec 2015

Recommended Reading : Data Structures using C/C++

Another famous problem in system programming in the context of concurrency is Reader-Writer problem.Here we will be considering that we have a single memory location and various readers and writers want to access it.Here we will be implementing Writer's priority over reader.Just keep in mind the following points:

  • Multiple readers can enter the critical section or read at the same time.
  • Only one writer can enter the critical section or write at a particular time.
  • Writer and reader can't be simultaneously present in the critical section.

Firstly, let's get some basic understanding of the components we will be using in the following program:

What is pthread anyway ?  


According to Wikipedia, POSIX Threads, usually referred to as Pthreads, is an example of an execution model that exists independently from a language, as well as an example of a parallel execution model. It allows a program to control multiple different flows of work that overlap in time. Each flow of work is referred to as a Thread, and creation and control over these flows is achieved by making calls to the POSIX Threads API.

Explanation : Though the definition is self explanatory, I would like to stress upon the word - execution model. In short, the focus is on the model of how the threads are created and manipulated.We use the various POSIX APIs to do so. Hence POSIX Threads is actually a POSIX Standard for threads.





What is a mutex? Why should I use it? 


Well! Again according to the brainy Wikipedia, In computer science, mutual exclusion refers to the requirement of ensuring that no two concurrent processes are in their critical section at the same time. It is a basic requirement in concurrency control, to prevent race conditions. Here, a critical section refers to a period when the process accesses a shared resource, such as shared memory.

 Explanation : The following points should make the concept deeper and cleaner :-

1) Mutexes have two basic operations, they are lock and unlock.

2) If a mutex is unlocked and a thread calls lock on that mutex, the mutex locks and the thread continues its operation (i.e opeartions in the crtical section).

3) However, If the mutex is locked by some thread, then the other thread blocks until the thread 'holding' the lock calls unlock.

The following code illustrates the concept:



#include<semaphore.h>

#include<pthread.h>

#include<stdio.h>


int rc=0,wc=0,val;

pthread_mutex_t mutex1,mwrite,mread,rallow;

pthread_t tr1,tr2,tw1,tw2;

pthread_attr_t tr1attr,tr2attr,tw1attr,tw2attr;


void *writer();

void *reader();


int main()

{

 pthread_mutex_init(&mwrite,NULL);

 pthread_mutex_init(&mread,NULL);

 pthread_mutex_init(&rallow,NULL);

 pthread_mutex_init(&mutex1,NULL);


 pthread_attr_init(&tw1attr);

 pthread_attr_init(&tr1attr);

 pthread_attr_init(&tr2attr);

 pthread_attr_init(&tw2attr);


 printf("\n Writer 1 created");

 pthread_create(&tw1,&tw1attr,writer,NULL);

 printf("\n Reader 1 created");


 pthread_create(&tr1,&tr1attr,reader,NULL);

 printf("\n Reader 2 created");

 pthread_create(&tr2,&tr2attr,reader,NULL);

 printf("\n WRITER 2 created");

 pthread_create(&tw2,&tw2attr,writer,NULL);


 pthread_join(tw1,NULL);

 pthread_join(tr1,NULL);

 pthread_join(tr2,NULL);

 pthread_join(tw2,NULL);


 return 0;

}


void *writer()

{

 pthread_mutex_lock(&mwrite);

 wc++;

 if(wc==1)

  pthread_mutex_lock(&rallow);

 pthread_mutex_unlock(&mwrite);


 pthread_mutex_lock(&mutex1);

 printf("\n Enter data in writer %d",wc);

 scanf("%d",&val);

 pthread_mutex_unlock(&mutex1);


 pthread_mutex_lock(&mwrite);

 wc--;

 if(wc==0)

  pthread_mutex_unlock(&rallow);

 pthread_mutex_unlock(&mwrite);

 pthread_exit(0);

}


void *reader()

{

 pthread_mutex_lock(&rallow);

 pthread_mutex_lock(&mread);

 rc++;

 if(rc==1)

  pthread_mutex_lock(&mutex1);

 pthread_mutex_unlock(&mread);

 pthread_mutex_unlock(&rallow);


 printf("\n reader %d read data: %d",rc,val);


 pthread_mutex_lock(&mread);

 rc--;

 if(rc==0)

  pthread_mutex_unlock(&mutex1);

 pthread_mutex_unlock(&mread);

 pthread_exit(0);

}
I have used Linux's text editor to write the code.To run this code you have to type this in the terminal:

1) gcc -pthread yourfilename.c
2) ./a.out

You will be able to see the output on terminal screen.

Please let us know your feedback and questions, if any.

1 comment:

  1. Very efficiently written information.Keep up the good work. For sure i will check out more posts. This site seems to get a good amount of visitors.

    canon.com/ijsetup

    ReplyDelete

Thanks for your valuable comment