Question

In: Computer Science

Write a C-program to implement the Reader/Writer model for the attached pseudocode modified to produce only...

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);

}

Solutions

Expert Solution

#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


Related Solutions

write pseudocode for the following problems not c code Pseudocode only Write a C program to...
write pseudocode for the following problems not c code Pseudocode only Write a C program to print all natural numbers from 1 to n. - using while loop Write a C program to print all natural numbers in reverse (from n to 1). - using while loop Write a C program to print all alphabets from a to z. - using while loop Write a C program to print all even numbers between 1 to 100. - using while loop...
C Program and pseudocode for this problem. Write a C program that plays the game of...
C Program and pseudocode for this problem. Write a C program that plays the game of "Guess the number" as the following: Your program choose the number to be guessed by selecting an integer at random in the rang of 1 to 1000. The program then asks the use to guess the number. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program keeps telling the player "Too High" or...
PYTHON ONLY NO JAVA! PLEASE INCLUDE PSEUDOCODE AS WELL! Program 4: Design (pseudocode) and implement (source...
PYTHON ONLY NO JAVA! PLEASE INCLUDE PSEUDOCODE AS WELL! Program 4: Design (pseudocode) and implement (source code) a program (name it LargestOccurenceCount) that read from the user positive non-zero integer values, finds the largest value, and counts it occurrences. Assume that the input ends with number 0 (as sentinel value to stop the loop). The program should ignore any negative input and should continue to read user inputs until 0 is entered. The program should display the largest value and...
Using the pseudocode found below, write only the actual (C++) code for this program. Include all...
Using the pseudocode found below, write only the actual (C++) code for this program. Include all libraries. Specification: Write a program that will repeatedly ask the user for quiz grades in the range from 0 to 20. Any negative value will act as a sentinel. When the sentinel is entered compute the average of the grades entered. Design: Constants None Variables float grade float total = 0 int count = 0 float average ------- Inital Algorithm Repeatedly ask user for...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
Design (pseudocode) and implement (source code) a program (name it DistinctValues) to display only district values...
Design (pseudocode) and implement (source code) a program (name it DistinctValues) to display only district values in an array. The program main method defines a single-dimensional array of size 10 elements and prompts the user to enter 10 integers to initialize the array. The main method then calls method getValues() that takes an integer array and returns another single-dimensional array containing only distinct values in the original (passed) array. Document your code and properly label the input prompts and the...
JAVA ONLY. Program 3: Distance calc. This question is fairly straightforward. Design (pseudocode) and implement (source...
JAVA ONLY. Program 3: Distance calc. This question is fairly straightforward. Design (pseudocode) and implement (source code) a program to compute the distance between 2 points. The program prompts the user to enter 2 points (X1, Y1) and (X2, Y2). The distance between 2 points formula is: Square_Root [(X2 – X1)^2 + (Y2 – Y1)^2] Document your code, properly label the input prompts, and organize the outputs as shown in the following sample runs. Note: for C++, #include and then...
only using C (not C++) Implement a program that counts the words and prints their frequencies....
only using C (not C++) Implement a program that counts the words and prints their frequencies. In particular, the program extracts “words” from one or more text files, from a pipe, or from the console, and prints to the console the list of words found and their associated frequencies. Note that your project submission is restricted to only using the following system calls: open(), close(), read(), write(), and lseek() for performing I/O. You are allowed to use other C library...
Write and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
C++ PROGRAM Using the attached C++ code (Visual Studio project), 1) implement a CoffeeMakerFactory class that...
C++ PROGRAM Using the attached C++ code (Visual Studio project), 1) implement a CoffeeMakerFactory class that prompts the user to select a type of coffee she likes and 2) returns the object of what she selected to the console. #include "stdafx.h" #include <iostream> using namespace std; // Product from which the concrete products will inherit from class Coffee { protected:    char _type[15]; public:    Coffee()    {    }    char *getType()    {        return _type;   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT