I would like to ask for help with this task.
My task is write an easy program in C which simulates readers-writers problem. The program requirements are:
After program start, the user will be asked for enter count of writers and readers.
Program will continously inform the user about status of the threads.
After end of the program, little statistic (how many times had every reader read and every writer write).
I have done some basic structure of the program (dealing with the critical section through semaphores etc.), but I think that the program does not do, what it should do. Program runs without fault or unexpected behavior, but the counts, how many times had every reader read and every writer write are always one for each reader or writer).
Where am i wrong? Maybe I don't understand the task well.
Thank You very much for response.
Program code:
#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--;
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]);
}
sem_destroy(&w);
sem_destroy(&m);
return 0;
}
Output: Enter count of writers:4 Enter count of readers:4
Reader 1 reads from DB.
Reader 3 reads from DB.
Reader 4 reads from DB.
Reader 2 reads from DB.
Writer 1 writes to DB.
Writer 2 writes to DB.
Writer 3 writes to DB.
Writer 4 writes to DB.
--------------
Reader 1 read 1 times
Reader 2 read 1 times
Reader 3 read 1 times
Reader 4 read 1 times
Writer 1 wrote 1 times
Writer 2 wrote 1 times
Writer 3 wrote 1 times
Writer 4 wrote 1 times
pthread_create
function, each thread it's own data. – Iharob Al Asimi