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
Question 1
Do this new version of theAt first, write the multi-threaded program
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
#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
Remember the hints given in our class when we discussed the semaphore code.