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.

Answers were Sorted based on User's Feedback



How reader and writer problem was implemented and come up with effective solution for reader and wr..

Answer / sriram

The reader and writer problem is IMPLEMENTED THROUGH
SEMAPHORES.

THE EFFECTIVE SOLUTION TO SOLVE THE READER AND WRITER
SOLUTION IS :


A) If a writer is waiting in the ready queue to enter
into the critical section, then the current reading process
should be completed without delay.

B) Only one reader can be put into the ready queue
because the writer is waiting.

c) The semaphores such as ReadCount-to count the number
of reader in ready queue,
Reader and Writer, these are the important semaphores to
solve the reader and writer problem.

Is This Answer Correct ?    23 Yes 4 No

How reader and writer problem was implemented and come up with effective solution for reader and wr..

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

How reader and writer problem was implemented and come up with effective solution for reader and wr..

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

How reader and writer problem was implemented and come up with effective solution for reader and wr..

Answer / ashwini

#include<stdio.h>
#include<pthread.h>

pthread_mutex_t m;

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

FILE *fp;



main()
{
pthread_t tid1,tid2,tid3;
pthread_mutex_init(&m,NULL);
pthread_create(&tid2,NULL,reader,NULL);
pthread_create(&tid1,NULL,writer,NULL);
pthread_create(&tid3,NULL,reader,NULL);

pthread_join(tid2,NULL);
pthread_join(tid1,NULL);
pthread_join(tid3,NULL);
}
void* writer(void *str)
{
pthread_mutex_lock(&m);

fp=fopen("file1.txt","w");
global += 35;
fprintf(fp,"%d",100);
fclose(fp);
pthread_mutex_unlock(&m);
}
void* reader(void *param)
{
int abc;
pthread_mutex_lock(&m);
fp=fopen("file1.txt","r");
fscanf(fp,"%d",&abc);
printf("%d",global);
fclose(fp);
pthread_mutex_unlock(&m);
}

Is This Answer Correct ?    15 Yes 12 No

How reader and writer problem was implemented and come up with effective solution for reader and wr..

Answer / raj kumar

Readers: read data, never modify it
Writers: read data and modify it
criteria:
– Each read or write of the shared data must happen within
a critical section.
– Guarantee mutual exclusion for writers.
– Allow multiple readers to execute in the critical section
at once.
In this example, semaphores are used to prevent any writing
processes from changing information in the database while
other processes are reading from the database.
semaphore mutex = 1; // Controls access to
the reader count
semaphore db = 1; // Controls access to
the database
int reader_count; // The number of
reading processes accessing the data

Reader()
{
while (TRUE) { // loop forever
down(&mutex); // gain access
to reader_count
reader_count = reader_count + 1; // increment the
reader_count
if (reader_count == 1)
down(&db); // if this is
the first process to read the database,
// a down on db
is executed to prevent access to the
// database by a
writing process
up(&mutex); // allow other
processes to access reader_count
read_db(); // read the database
down(&mutex); // gain access
to reader_count
reader_count = reader_count - 1; // decrement
reader_count
if (reader_count == 0)
up(&db); // if there are
no more processes reading from the
// database,
allow writing process to access the data
up(&mutex); // allow other
processes to access reader_countuse_data();
// use the data
read from the database (non-critical)
}

Writer()
{
while (TRUE) { // loop forever
create_data(); // create data
to enter into database (non-critical)
down(&db); // gain access
to the database
write_db(); // write
information to the database
up(&db); // release
exclusive access to the database
}

Is This Answer Correct ?    3 Yes 3 No

How reader and writer problem was implemented and come up with effective solution for reader and wr..

Answer / kiran baviskar

#include<pthread.h>
//#include<semaphore.h>
#include<stdio.h>
#include<stdlib.h>

pthread_mutex_t x,wsem;
pthread_t tid;
int readcount;

void intialize()
{
pthread_mutex_init(&x,NULL);
pthread_mutex_init(&wsem,NULL);
readcount=0;
}

void * reader (void * param)
{
int waittime;
waittime = rand() % 5;
printf("
Reader is trying to enter");
pthread_mutex_lock(&x);
readcount++;
if(readcount==1)
pthread_mutex_lock(&wsem);
printf("
%d Reader is inside ",readcount);
pthread_mutex_unlock(&x);
sleep(waittime);
pthread_mutex_lock(&x);
readcount--;
if(readcount==0)
pthread_mutex_unlock(&wsem);
pthread_mutex_unlock(&x);
printf("
Reader is Leaving");
}

void * writer (void * param)
{
int waittime;
waittime=rand() % 3;
printf("
Writer is trying to enter");
pthread_mutex_lock(&wsem);
printf("
Write has entered");
sleep(waittime);
pthread_mutex_unlock(&wsem);
printf("
Writer is leaving");
sleep(30);
exit(0);
}

int main()
{
int n1,n2,i;
printf("
Enter the no of readers: ");
scanf("%d",&n1);
printf("
Enter the no of writers: ");
scanf("%d",&n2);
for(i=0;i<n1;i++)
pthread_create(&tid,NULL,reader,NULL);
for(i=0;i<n2;i++)
pthread_create(&tid,NULL,writer,NULL);
sleep(30);
exit(0);
}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C++ Code Interview Questions

what is the use of using for loop as "for(;;)"?

5 Answers   Satyam,


Ask the user to input three positive integers M, N and q. Make the 2 dimensional array of integers with size MxN, where all the elements of I (I = 1,…,M) line will be members of geometrical progression with first element equal to the number of line (I) and denominator q.

0 Answers  


1.program to add any two objects using operator overloading 2.program to add any two objects using constructors 3.program to add any two objects using binary operator 4.program to add any two objects using unary operator

2 Answers   IBM,


Here's the programm code: int magic(int a, int b) { return b == 0 ? a : magic(b, a % b); } int main() { int a, b; scanf("%d%d", &a, &b); printf("%d\n", magic(a, b)); return 0; } on input stream we have integers 4, 45 What's the output integer? How many times will be initiated "magic" function?

1 Answers  


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

0 Answers   HCL,






Subsets Write an algorithm that prints out all the subsets of 3 elements of a set of n elements. The elements of the set are stored in a list that is the input to the algorithm. (Since it is a set, you may assume all elements in the list are distinct.)

1 Answers   CSC, Qatar University,


write a program to sort 'n' elemnts using bubble sort

1 Answers   IBM,


Counting in Lojban, an artificial language developed over the last fourty years, is easier than in most languages The numbers from zero to nine are: 0 no 1 pa 2 re 3 ci 4 vo 5 mk 6 xa 7 ze 8 bi 9 so Larger numbers are created by gluing the digit togather. For Examle 123 is pareci Write a program that reads in a lojban string(representing a no less than or equal to 1,000,000) and output it in numbers.

4 Answers  


using friend function find the maximum number from given two numbers from two different classes.write all necessary functions and constructor for the classes.

1 Answers   TCS,


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

0 Answers   iGate,


#include<iostream.h> //main() //{ class A { friend class B; public: void read(); }; class B { public : int a,b; }; void A::read() { cout<<"welcome"; } main() { A x; B y; y.read(); } In the above program......, as B is a friend of A B can have access to all members,i cant access y.read . could you please tell me the reason and what would i code to execute this program?

2 Answers  


using repetition structure. Write a c program that will accept five numbers. The program should count and output the total count of even numbers and total count of add numbers.

2 Answers  


Categories