Search code examples
csemaphorescheduling

Readers-Writers Problem in C using pthreads and semaphores


I'm trying to create the readers-writers scenario through C code but I'm stuck at trying to break off the readers code in the middle so that a reader does not just enter and exit, it stays and more readers can add.

My current program doesn't enter more than one reader, as soon as one enters, it will leave then writer would come.

I've tried using sleep() but including unistd library really destroys half the code with errors

#include<semaphore.h>
#include<stdio.h>
#include<stdlib.h>
sem_t x,y;
pthread_t tid;
pthread_t writerthreads[100],readerthreads[100];
int readercount;

void *reader(void* param)
{
    sem_wait(&x);
    readercount++;
    if(readercount==1)
    sem_wait(&y);
    sem_post(&x);
    printf("\n%d reader is inside",readercount);
    sem_wait(&x);
    readercount--;
    if(readercount==0)
    {
        sem_post(&y);
    }
    sem_post(&x);
    printf("\n%d Reader is leaving",readercount+1);
}

void *writer(void* param)
{
    printf("\nWriter is trying to enter");
    sem_wait(&y);
    printf("\nWriter has entered");
    sem_post(&y);
    printf("\nWriter is leaving");
}

int main()
{
    int n2,i;
    printf("Enter the number of readers:");
    scanf("%d",&n2);
    int n1[n2];
    sem_init(&x,0,1);
    sem_init(&y,0,1);
    for(i=0;i<n2;i++)
    {
        pthread_create(&writerthreads[i],NULL,reader,NULL);
        pthread_create(&readerthreads[i],NULL,writer,NULL);
    }
    for(i=0;i<n2;i++)
    {
        pthread_join(writerthreads[i],NULL);
        pthread_join(readerthreads[i],NULL);
    }

}

The output goes in the form of

reader is inside
reader is leaving
reader is inside 
reader is leaving
writer is trying to enter
writer has entered
writer is leaving

and so on, I want there to be more than one reader at a time in the program.


Solution

  • Your code works fine. Just adding a slight delay to the reader allows other reads time to get in. Otherwise, the reader is done too quickly and seeing overlapping readers would just be rare. You also forgot to initialize readercount to zero.

    #include<semaphore.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include<pthread.h>
    sem_t x,y;
    pthread_t tid;
    pthread_t writerthreads[100],readerthreads[100];
    int readercount = 0;
    
    void *reader(void* param)
    {
        sem_wait(&x);
        readercount++;
        if(readercount==1)
            sem_wait(&y);
        sem_post(&x);
        printf("%d reader is inside\n",readercount);
        usleep(3);
        sem_wait(&x);
        readercount--;
        if(readercount==0)
        {
            sem_post(&y);
        }
        sem_post(&x);
        printf("%d Reader is leaving\n",readercount+1);
        return NULL;
    }
    
    void *writer(void* param)
    {
        printf("Writer is trying to enter\n");
        sem_wait(&y);
        printf("Writer has entered\n");
        sem_post(&y);
        printf("Writer is leaving\n");
        return NULL;
    }
    
    int main()
    {
        int n2,i;
        printf("Enter the number of readers:");
        scanf("%d",&n2);
        printf("\n");
        int n1[n2];
        sem_init(&x,0,1);
        sem_init(&y,0,1);
        for(i=0;i<n2;i++)
        {
            pthread_create(&writerthreads[i],NULL,reader,NULL);
            pthread_create(&readerthreads[i],NULL,writer,NULL);
        }
        for(i=0;i<n2;i++)
        {
            pthread_join(writerthreads[i],NULL);
            pthread_join(readerthreads[i],NULL);
        }
    
    }
    

    1 reader is inside
    Writer is trying to enter
    2 reader is inside
    2 Reader is leaving
    2 reader is inside
    Writer is trying to enter
    2 Reader is leaving
    1 Reader is leaving