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 / masud

#include <pthread.h>
#include <sched.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>

#define MAXTHREAD 10 /* define # readers */


void access_database(); /* prototypes */
void non_access_database();

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

sem_t q; /* establish que */
int rc = 0; /* number of processes reading or
wanting to */
int wc = 0;
int write_request = 0;

int main()
{
pthread_t readers[MAXTHREAD],writerTh;
int index;
int ids[MAXTHREAD]; /* readers and initialize mutex, q
and db-set them to 1 */
sem_init (&q,0,1);
for(index = 0; index < MAXTHREAD; index ++)
{
ids[index]=index+1;
/* create readers and error
check */
if(pthread_create(&readers[index],0,reader,&ids
[index])!=0){
perror("Cannot create reader!");
exit(1);

}
}
if(pthread_create(&writerTh,0,writer,0)!=0){
perror("Cannot create writer"); /* create
writers and error check */
exit(1);
}

pthread_join(writerTh,0);
sem_destroy (&q);
return 0;
}

void* reader(void*arg) /* readers function
to read */
{
int index = *(int*)arg;
int can_read;
while(1){
can_read = 1;

sem_wait(&q);
if(wc == 0 && write_request == 0) rc++;
else can_read = 0;
sem_post(&q);

if(can_read) {
access_database();
printf("Thread %d reading\n", index);
sleep(index);

sem_wait(&q);
rc--;
sem_post(&q);
}

sched_yield();
}
return 0;
}

void* writer(void*arg) /* writer's function to
write */
{
int can_write;
while(1){
can_write = 1;
non_access_database();

sem_wait (&q);
if(rc == 0) wc++;
else { can_write = 0; write_request = 1; }
sem_post(&q);

if(can_write) {
access_database();
printf("Writer is now writing...Number of
readers: %d\n",rc);

sleep(3);

sem_wait(&q);
wc--;
write_request = 0;
sem_post(&q);
}

sched_yield();
}
return 0;
}

void access_database()
{

}


void non_access_database()
{

}

Is This Answer Correct ?    7 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

what mean void creat_object?in public class in this code class A{ public: int x; A(){ cout << endl<< "Constructor A";} ~A(){ cout << endl<< "Destructor A, x is\t"<< x;} }; void create_object(); void main() { A a; a.x=10; { A c; c.x=20; } create_object(); } void create_object() { A b; b.x=30; }

2270


find level of following tree (state, parent) " J,D I,D H,C E,B F,B G,C B,A D,A C,A A,& K,E L,E L,F M,F N,G O,H P,I P,H Q,I R,J S,K U,P T,L

2212


how to diplay a external image of output on winxp by using c & c++,

3154


Code for Two Classes for Doing Gzip in Memory?

2988


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.

3615






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

4201


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

4694


A suduco given & u hv 2 check if it is incomplete(blanks left),or correct or incorrect

2609


Given a table of the form: Product Sold on A 1/1/1980 B 1/1/1980 C 1/1/1980 A 1/1/1980 B 1/1/1980 C 2/1/1980 A 2/1/1980 There are 30 products and 10,000 records of such type. Also the month period during which sales happened is given to u. Write the program to display the result as: Product Month No. of copies A January 12 A February 15 A March 27 B January 54 B February 15 B March 10 C January 37

2411


Write a C++ program without using any loop (if, for, while etc) to print prime numbers from 1 to 100 and 100 to 1 (Do not use 200 print statements!!!)

3494


Code for Method of Handling Factorials of Any Size?

2181


write a function that allocates memory for a single data type passed as a parameter.the function uses the new operator and return a pointer to the allocated memory.the function must catch and handle any exception during allocation

2619


Write a C/C++ program that connects to a MySQL server and displays the global TIMEZONE.

4762


write a program to convert temperature from fa height into celcius and vise versa,use modular programming

2675


write a program that prompt the user to enter his height and weight,then calculate the body mass index and show the algorithm used

4583