Reader -Writer Program

What is reader writer problem?
Read this link to know this problem: https://en.wikipedia.org/wiki/Readers%E2%80%93writers_problem
Following algorithm is implement using pthread lib.

Reader()
{

lock(read)
if(reader==0)
   lock(write)
reader++
unlock(read)

reader from shared resource

lock(read)
reader--
if(reader == 0)
   unlock(write)
unlock(read)

}

// Writer thread

writer()
{

produce item
lock(write)
write item to shared resource
unlock(write)

}

[sourcecode language="CPP" highlight=""]

#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>

#define MAX 20

// Shared buffer with default value
char buffer[MAX] = "Sacheen Birhade";

// Variable to count number of readers
volatile int reader = 0;

// read and write mutex
pthread_mutex_t write = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t read = PTHREAD_MUTEX_INITIALIZER;

// Helper function to generate random string
char *rand_string(int len) {

        char set[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,!.?-#'";
        int n, idx;
        char *rand_str = NULL;
 if (len) {
                rand_str = malloc(sizeof(char) * (len + 1));

                if (rand_str) {
                        for (int n = 0; n < len; n++) {
                                idx = rand() % (sizeof(set) - 1);
                                rand_str[n] = set[idx];
                        }

                        rand_str[len] = '\0';
                }
        }

        return rand_str;
}


// Writer thread function
void *writer_thread(void *args)
{

        char *str;

        while (1) {
                // Generate random string
                str = rand_string(MAX - 1);
                // take lock on write mutex
                pthread_mutex_lock(&write);
                memset(buffer, 0, MAX);
                // write into buffer
                sprintf(buffer, "%s", str);
                printf("Writer thread has writen: %s\n", buffer);
                // release write lock
                pthread_mutex_unlock(&write);
        }

}

// Reader thread function
void *reader_thread(void *args)
{
        while (1) {
                // take read lock
                pthread_mutex_lock(&read);
                // if this is first reader thread take a write lock so no writer thread can write
                if (reader == 0) {
                        pthread_mutex_lock(&write);
                }
                // increate reader to count readers
                reader++;
                // unlock read
                pthread_mutex_unlock(&read);
                // Now time to read from buffer
                printf("Reader thread has read: %s\n", buffer);
                pthread_mutex_lock(&read);
                //Reader has done of reading from the buffer. Now reader is exiting. Decrement reader.
                reader--;
                // If only one reader thread is remaining, release write lock. So that writer thread "may" get a chance
                if (reader == 0) {
                        pthread_mutex_unlock(&write);
                }
                // release read
                pthread_mutex_unlock(&read);
        }

}


int main()
{

        pthread_t tid1, tid2;

        // Creating threads
        pthread_create(&tid1, NULL, &reader_thread, NULL);
        pthread_create(&tid2, NULL, &writer_thread, NULL);

        pthread_join(tid1, NULL);
        pthread_join(tid2, NULL);

        return 0;
}


--Sacheen Birhade.

Comments

Popular posts from this blog

STL Questions

Producer Consumer problem using mutex

Interview questions