Program:
#include<stdlib.h> #include<stdio.h> #include<pthread.h> #include<semaphore.h> #define MAX 5 // Writer: Only one writer can write at a time // Reader: Multiple Reader's can read // Ensure no writer present while reading int rc = 0; // To store number of readers int cnt = 1; // It is our Critical Section pthread_mutex_t mutex; sem_t write; // Writer Function void *writer(int *i) { // Binary Semaphore sem_wait(&write); // write-- cnt = rand() %10; printf("\n\n Writer: %d Item: %d",*(int *)i ,cnt); sem_post(&write); // write++ } void *reader(int *i) { // Readers Count Increment pthread_mutex_lock(&mutex); rc++; if(rc == 1) sem_wait(&write); pthread_mutex_unlock(&mutex); // Accessing Critical Section printf("\n Reader: %d Item: %d",*(int *)i,cnt); // Readers Count Decrement pthread_mutex_lock(&mutex); rc--; if(rc == 0) sem_post(&write); pthread_mutex_unlock(&mutex); } int main() { pthread_t wr[10],read[10]; sem_init(&write,0,1); pthread_mutex_init(&mutex,NULL); int a[] = {1,2,3,4,5,6,7,8,9,10}; for(int i=0;i<10;i++) { pthread_create(&wr[i],NULL,(void *) &writer,(void *) &a[i]); pthread_create(&read[i],NULL,(void *) &reader,(void *) &a[i]); } for(int i=0;i<10;i++) { pthread_join(wr[i],NULL); pthread_join(read[i],NULL); } sem_destroy(&write); pthread_mutex_destroy(&mutex); return 0; }
Output: