Lab - III: Process Synchronisation


Do the following three problems in this lab. You may submit them by emailing your programs as in your earlier labs. Name the files Lab-03-01.c, Lab-03-02.c, etc. Write your explanations in a separate file Lab-3-expl with an appropriate extension depending on the type of software used in creating the file.Zip all of them into a single file <your roll number>.zip and email the zipped file to

chakravarthybhagvati@uohyd.ac.in


Question 1

Do this new version of the reader-writer problem. Take the single-reader, multiple-writers problem but now allow the reader to verify the buffer while the multiple writer threads have not terminated.

At first, write the multi-threaded program without any synchronisation primitives. Run the program a few times and make a note of the types of errors you see. List each type of error and write down your explanation why it occurred.

Then, put in synchronisation primitives and make sure the program works correctly now.

Hint: Combine the synchronisation primitives that we discussed in class. Make sure there are no writer-writer and reader-writer conflicts.

Use the code stub below to write your code. The code for the single-reader single-writer is available for helping you code the solution.
In [ ]:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

#define MAXTHREADS 50
#define MAXITEMS   200000

void *writer(void *);
void *reader(void *);

/** The program should be called with two command-line
    arguments:
    First argument is the number of threads
    Second argument is the number of items to write **/
int main (int argc, char *argv[])
{
    /** Declare needed variables **/
    
    /** Use pthread_create to create writer threads **/
    
    /** Use pthread_create to create a single reader thread **/
    
    /** Use pthread_join to wait for both writer and reader
        threads to complete **/
    
    /** Print the number of items written by each thread
        as well as the total number of items **/
}

void *reader(void *arg)
{
    /** Code for reader here **/
}

void *writer(void *arg)
{
    /** Code for writer here **/
}

Question 2: Multiple Readers Multiple Writers Problem

Modify the program for Question 1 to have multiple readers alongside multiple writers. All the readers and writers run alongside each other. Carefully think of the different conflicts that can occur and resolve them with the correct number and type of synchronisation primitives.


Question 3: Circular Buffer Problem

In this version of the problem, the shared buffer is circular. It means that when the writers or readers reach the end of the buffer, i.e. the nitems limit, they go back to cell 0 and restart the reading and writing processes. Try this problem with multiple writers and a single reader. Remember that in this case, when a writer goes back to the beginning, it cannot write into a cell that has not yet been examined by a reader. Similarly, the reader cannot read and verify a cell that has not yet been written freshly by a writer.

Remember the hints given in our class when we discussed the semaphore code.


T H E    E N D