How reader and writer problem was implemented and come up
with effective solution for reader and writer problem in
case we have n readers and 1 writer.

Answer Posted / avinash

#include<pthread.h>
#include<semaphore.h>
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
sem_t mutex, cnt;

int readcount = 0;
int rcnt, wcnt;

void *wfun (void *args)
{
sem_wait (&cnt);
printf("\nIn writer : value of read cnt is %d", readcount );
sem_post(&cnt);
}

void *rfun (void *args)
{
sem_wait(&mutex);
readcount ++;
if(readcount == 1)
sem_wait(&cnt);
sem_post(&mutex);
printf("\nIn reader : reader has incremented read cnt, its value is %d",readcount );
sem_wait(&mutex);
readcount --;
if(readcount == 0)
sem_post(&cnt);
sem_post(&mutex);

}

int main()
{

int i, *t;
t = (int*)malloc(sizeof(int));

printf("\nenter number of readers: ");
scanf("%d",&rcnt);
printf("\nenter number of writers: ");
scanf("%d",&wcnt);

//initialize semaphores
sem_init(&mutex,0,1);
sem_init(&cnt,0,1);
pthread_t rdrs[10];
pthread_t wrs[10];

//create reader threads
for(i=0;i<rcnt;i++)
{
*t = i;
pthread_create(&rdrs[i],NULL,rfun, (void *)t);
}

//create writer threads
for(i=0;i<wcnt;i++)
{
*t=i;
pthread_create(&wrs[i],NULL,wfun,(void *)t);
}

for(i=0;i<rcnt;i++)
pthread_join(rdrs[i],NULL);

for(i=0;i<wcnt;i++)
pthread_join(wrs[i],NULL);
return(0);
}

Is This Answer Correct ?    6 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Create a program to read two random data set in two files named data1.txt and data2.txt manifold contains integer numbers, whereas data2.txt file contains the float type numbers. Simpanlahmasing each into 2 pieces of data that is an array of type integer array and an array of type float, then calculate the average numbers in the second array.

2152


write a program using virtual function to find the transposing of a square matrix?

2843


write a program that reads a series of strings and prints only those strings begging with letter "b"

2674


Performance Algorithm A performs 10n2 basic operations and algorithm B performs 300 lg n basic operations. For what value of n does algorithm B start to show its better performance?

7341


create a stucture student containing field for roll no,class,year and marks.create 10 student annd store them in a file

2220






Code for Easily Using Hash Table?

2391


We need to write the function to check the password entered is correct or not based on the following conditions.. a) It must have atleast one lower case character and one digit. b)It must not have any Upper case characters and any special characters c) length should be b/w 5-12. d) It should not have any same immediate patterns like abcanan1 : not acceptable coz of an an pattern abc11se: not acceptable, coz of pattern 11 123sd123 : acceptable, as not immediate pattern adfasdsdf : not acceptable, as no digits Aasdfasd12: not acceptable, as have uppercase character

3957


Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the total number of disk writes by MySQL.

2065


U hv to enter a range from a and b and search hw many no. of times a pattern n. occurs between the range a and b. Eg :i/p:enter range :0 100 Enter pattern: 13 o/p: the no. times 13 occurred betwwn 0 to 100:1 Eg :i/p:enter range :100 1000 Enter pattern: 13 o/p: the no. times 13 occurred betwwn 100 to 1000: (in this 13,113,131,132,133…139,213,313,…913 all these will be counted)

2125


Write a (n) algorithm that sorts n distinct integers, ranging in size between 1 and kn inclusive, where k is a constant positive integer. (Hint: Use a kn-element array.)

4451


What output does the following code generate? Why? What output does it generate if you make A::Foo() a pure virtual function? class A { A() { this->Foo(); } virtual void Foo() { cout << "A::Foo()" << endl; } }; class B : public A { B() { this->Foo(); } virtual void Foo() { cout << "A::Foo()" << endl; } }; int main(int, char**) { A objectA; B objectB; return 0; }

644


Implement a command console for changing settings on a particular object. The command console should allow you to enter a string and will return the response (very similar to a terminal session). The commands are as follows: SET propertyname=newvalue will change the target object’s member named “propertyname” to have a value equal to “newvalue”. If the input value is incompatible (i.e. an int being set to a string), print out an appropriate error message. GET propertyname will print out the current value of the target object’s member named “propertyname”. GET * will print out a list of all target object members and their current values. The system should be extensible for future commands and should accept an arbitrary object, such that another developer could insert another object into the system and rely on the command console to get and set the properties correctly.

3398


How to swap two ASCII numbers?

2449


How to Split Strings with Regex in Managed C++ Applications?

3125


Write a simple encryption program using string function which apply the substitution method.

5538