Readers-writers problem using Semaphore
Objective
In this post, we will see Readers-writers problem code using C Language and we will try to understand how to solve this problem.
Readers-writers problem in c is the Synchronization problem which can be solved by using this algorithm. This code is written to run on Linux or Unix machines. So for running this code, you must have UNIX system with c compiler installed on it then only you can run these programs. The main reason that we require the Unix system is that we can only test reader-writer problem on Unix system.
Readers-writers problem is Mutual Exclusion and Synchronization problem which can solve by semaphores and monitors. Here is the Source Code for writer priority using C under reader and writer problem. Reader-writer code can be implemented in C on Linux.
We have declared two variables named as reading count and write count which will count the read and write to control the synchronization problem. Only the read or write permission will be given based on these variables. So we can say that it is a way in which we should handle the reader and writer.
Mutual Exclusion and Synchronization are like the traffic rules of computer programs, ensuring that multiple threads or processes play nicely together when using shared resources. Imagine a semaphore as a traffic light: it tells threads when to stop and when to go, preventing them from crashing into each other while accessing shared data. Monitors, on the other hand, are like gated communities for data, where access is carefully controlled through a single entrance. Inside, everyone follows the rules, taking turns and waiting patiently when needed. Both semaphores and monitors offer smart ways to keep our programs running smoothly, preventing chaos and ensuring that everyone gets their fair share of resources.
Reader Writer problem using Reader Priority code can be found here.
Readers writers problem Code
#include
#include
#include
void * reader(void *) ;
void *writer (void *) ;
sem_t x,y,z,wsem,rsem ;
int readcount=0 ;
int writecount=0 ;
main()
{
int a=1,b=1;
system("clear");
sem_init(&wsem,0,1) ;
sem_init(&x,0,1) ;
sem_init(&rsem,0,1) ;
sem_init(&y,0,1) ;
sem_init(&z,0,1) ;
pthread_t r,w,r1,w1,r2,r3,w2,w3 ;
pthread_create(&r,NULL,reader,(void *)a);
a++;
pthread_create(&r1,NULL,reader,(void *)a);
a++;
pthread_create(&w,NULL,writer,(void *)b);
b++;
pthread_create(&w1,NULL,writer,(void *)b);
b++;
pthread_create(&r2,NULL,reader,(void *)a);
a++;
pthread_create(&w2,NULL,writer,(void *)b);
b++;
pthread_create(&r3,NULL,reader,(void *)a);
a++;
pthread_create(&w3,NULL,writer,(void *)b);
pthread_join(r,NULL);
pthread_join(r1,NULL);
pthread_join(w,NULL);
pthread_join(w1,NULL);
pthread_join(r2,NULL);
pthread_join(w2,NULL) ;
pthread_join(r3,NULL);
pthread_join(w3,NULL);
printf("main terminated\n");
}
void * reader(void * arg)
{
int c=(int)arg ;
printf("\nreader %d is created",c);
sleep(1);
sem_wait(&rsem);
sem_wait(&x) ;
readcount++;
if(readcount==1)
sem_wait(&wsem) ;
sem_post(&x) ;
sem_post(&rsem);
sleep(1)
/*Critcal Section */
printf("\n\nreader %d is reading\n ",c);
sleep(1) ;
printf("\nreader%d finished reading\n",c);
/* critical section completd */
sem_wait(&x) ;
readcount-- ;
if(readcount==0)
sem_post(&wsem) ;
sem_post(&x) ;
}
void * writer(void * arg)
{
int c=(int)arg ;
printf("\nwriter %d is created",c);
sleep(1);
sem_wait(&y) ;
writecount++ ;
if(writecount==0)
sem_wait(&rsem) ;
sem_post(&y) ;
sem_wait(&wsem) ;
printf("\nwriter %d is writing\n",c) ;
sleep(1);
printf("\nwriter%d finished writing\n",c);
sem_post(&wsem) ;
sem_wait(&y) ;
writecount-- ;
if(writecount==0)
sem_post(&rsem) ;
sem_post(&y) ;
}
In conclusion, this article provides an in-depth exploration of the Readers-Writers problem in C Language, offering valuable insights into concurrent resource management. Through detailed analysis and practical code examples, readers gain a solid understanding of synchronization techniques and deadlock prevention strategies. Readers-Writers problem in C, concurrent resource access, and synchronization techniques, this content aims to enhance visibility and accessibility for programmers seeking solutions to concurrency challenges. With this comprehensive guide, readers can master the complexities of concurrent programming in C, empowering them to develop robust and efficient systems.
Download Source Code
Please use the below link for downloading source code.
Leave a Reply