DEPARTMENT OF ECE M.
Tech (VLSI & ES) EMBEDDED SYSTEMS LABORATORY
PROGRAM TO DEMONSTRATE READER’S WRITER’S PROBLEM
AIM: To write a program that demonstrates reader’s and writer’s problem
APPARATUS:
Hardware Required A PC with 1 GB RAM, 150 MB Free Space
Operating System – Linux Ubuntu/Fedora
Software Required Kernel 2.36.x or higher
GNU C Compiler (gcc)
THEORY:
An object is shared among may threads, each belonging to one of two classes: – Readers: read data,
never modify it – Writers: read data and modify it. The readers-writers problems are examples of a common
computing problem in concurrency. There are at least three variations of the problems, which deal with
situations in which many threads try to access the same shared memory at one time. Some threads may read
and some may write, with the constraint that no process may access the share for either reading or writing,
while another process is in the act of writing to it. (In particular, it is allowed for two or more readers to
access the share at the same time.) A readers-writer lock is a data structure that solves one or more of the
readers-writers problems.
PROCEDURE:
1. Open vi editor using command vi <filename.c>
2. Write the program code and save the code using the command “:wq”
3. In the shell, compile the program using the following syntax
gcc <filename.c> -o <exe_name> -lpthread
4. To run the application use the following syntax
./exe_name
5. Check the output and record the same in the observations.
VASIREDDY VENKATADRI INSTITUTE OF TECHNOLOGY Page | 14
DEPARTMENT OF ECE M. Tech (VLSI & ES) EMBEDDED SYSTEMS LABORATORY
PROGRAM:
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
sem_t w; // write access
sem_t m; // mutex
int rc=0; // readers count
int writersCount;
int readersCount;
pthread_t writersThread[10], readersThread[10];
int writeCount[10], readCount[10];
int i;
void *writer(void *i) {
int a = *((int *) i);
sem_wait(&w); // P(w)
printf("Writer %d writes to DB.\n",a+1);
writeCount[a+1]++;
sem_post(&w); // V(w)
free(i);
}
void *reader(void *i) {
int a = *((int *) i);
sem_wait(&m); // P(m)
rc++;
if (rc == 1) {
sem_wait(&w); // P(w)
}
sem_post(&m); // V (m)
printf("Reader %d reads from DB.\n",a+1);
readCount[a+1]++;
sem_wait(&m); // P(m)
rc--;
VASIREDDY VENKATADRI INSTITUTE OF TECHNOLOGY Page | 15
DEPARTMENT OF ECE M. Tech (VLSI & ES) EMBEDDED SYSTEMS LABORATORY
if (rc == 0) {
sem_post(&w); // V(w)
}
sem_post(&m); // V(m)
free(i);
}
int main() {
sem_init(&w,0,1);
sem_init(&m,0,1);
printf("Enter count of writers:");
scanf("%d",&writersCount);
printf("Enter count of readers:");
scanf("%d",&readersCount);
for (i=0; i<readersCount; i++) {
int *arg = malloc(sizeof(*arg));
*arg = i;
pthread_create(&readersThread[i], NULL, reader, arg);
}
for (i=0; i<writersCount; i++) {
int *arg = malloc(sizeof(*arg));
*arg = i;
pthread_create(&writersThread[i], NULL, writer, arg);
}
for (i=0; i<writersCount; i++) {
pthread_join(writersThread[i], NULL);
}
for (i=0; i<readersCount; i++) {
pthread_join(readersThread[i], NULL);
}
printf("--------------\n");
for (i=0; i<readersCount; i++) {
printf("Reader %d read %d times\n",i+1,readCount[i+1]);
}
for (i=0; i<writersCount; i++) {
printf("Writer %d wrote %d times\n",i+1,writeCount[i+1]);
}
VASIREDDY VENKATADRI INSTITUTE OF TECHNOLOGY Page | 16
DEPARTMENT OF ECE M. Tech (VLSI & ES) EMBEDDED SYSTEMS LABORATORY
sem_destroy(&w);
sem_destroy(&m);
return 0;
}
OUTPUT WINDOW
RESULT:
VASIREDDY VENKATADRI INSTITUTE OF TECHNOLOGY Page | 17