In: Computer Science
Write a C-program to implement the Reader/Writer model for the attached pseudocode modified to produce only one million output instead of an infinite amount. Use pthreads and Linux semaphores. Include ten readers with one writer. The writer should produce one million outputs. Verify that the readers receive an equal share of the information produced. Meaning each reader receives about 100,000 of information. Explain your results regardless of the balance. No use mutex or monitor functions.
****PSEUDOCODE****
/* program readersandwriters */
int readcount, writecount; semaphore x = 1, y = 1, z = 1, wsem = 1, rsem = 1;
void reader()
{
while (true){
semWait (z);
semWait (rsem);
semWait (x);
readcount++;
if (readcount == 1)
semWait (wsem);
semSignal (x);
semSignal (rsem);
semSignal (z);
READUNIT();
semWait (x);
readcount--;
if (readcount == 0) semSignal (wsem);
semSignal (x);
}
}
Void writer ()
{
while (true) {
semWait (y);
writecount++;
if (writecount == 1)
semWait (rsem);
semSignal (y);
semWait (wsem);
WRITEUNIT();
semSignal (wsem);
semWait (y);
writecount--;
if (writecount == 0) semSignal (rsem);
semSignal (y);
}
}
void main()
{
readcount = writecount = 0;
parbegin (reader, writer);
}
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#define MAX 20
sem_t mutexValue, writeBlock;
int sharedData = 0, binCounter = 0;
// Function to perform reader operation
void *readerFunction(void *id)
{
// Wait operation
sem_wait(&mutexValue);
// Increase the binary counter by one
binCounter++;
// Checks if binary counter value is 1
if(binCounter == 1)
// Wait operation
sem_wait(&writeBlock);
// Signal operation
sem_post(&mutexValue);
// Displays reader number with data
printf("Reader - %d reads: %d\n", (int)id, sharedData);
// Wait operation
sem_wait(&mutexValue);
// Decrease the binary counter by one
binCounter--;
// Checks if binary counter value is 0
if(binCounter == 0)
// Signal operation
sem_post(&writeBlock);
sem_post(&mutexValue);
}// End of function
// Function to perform writer operation
void *writerFunction(void *id)
{
// Wait operation
sem_wait(&writeBlock);
// Increase the shared counter by one
sharedData++;
// Displays writer number and data
printf("Writer - %d writers: %d\n", (int)id, sharedData);
// Signal operation
sem_post(&writeBlock);
}// End of function
// main function definition
int main()
{
int c;
// Creates an array of thread for reader and writer
pthread_t readerID[MAX], writerID[MAX];
// Initializes
sem_init(&mutexValue, 0, 1);
sem_init(&writeBlock, 0, 1);
// Loops till MAX
for(c = 0; c < MAX; c++)
{
// Creates thread for writer
pthread_create(&writerID[c], NULL, writerFunction,(void *)c +
1);
pthread_join(writerID[c], NULL);
// Creates thread for reader
pthread_create(&readerID[c], NULL, readerFunction, (void *)c +
1);
pthread_join(readerID[c],NULL);
}// End for loop
return 0;
}// End of main function
Sample Output:
Writer - 1 writers: 1
Reader - 1 reads: 1
Writer - 2 writers: 2
Reader - 2 reads: 2
Writer - 3 writers: 3
Reader - 3 reads: 3
Writer - 4 writers: 4
Reader - 4 reads: 4
Writer - 5 writers: 5
Reader - 5 reads: 5
Writer - 6 writers: 6
Reader - 6 reads: 6
Writer - 7 writers: 7
Reader - 7 reads: 7
Writer - 8 writers: 8
Reader - 8 reads: 8
Writer - 9 writers: 9
Reader - 9 reads: 9
Writer - 10 writers: 10
Reader - 10 reads: 10
Writer - 11 writers: 11
Reader - 11 reads: 11
Writer - 12 writers: 12
Reader - 12 reads: 12
Writer - 13 writers: 13
Reader - 13 reads: 13
Writer - 14 writers: 14
Reader - 14 reads: 14
Writer - 15 writers: 15
Reader - 15 reads: 15
Writer - 16 writers: 16
Reader - 16 reads: 16
Writer - 17 writers: 17
Reader - 17 reads: 17
Writer - 18 writers: 18
Reader - 18 reads: 18
Writer - 19 writers: 19
Reader - 19 reads: 19
Writer - 20 writers: 20
Reader - 20 reads: 20