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
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 |
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 |
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 |
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 |
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 |
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 |
write a program in c++ to scramble a bmp image file using a scramble key 0x7c and an XOR logic. print out the original image, the scrambled image and the program. Note: the first 24bytes of a bmp file contain the header information of the file.
Write an algorithm that receives a string and reverses it.
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?
0 Answers ASD Lab, Qatar University, UNV,
Write a program using two-dimensional arrays that determines the highest and lowest of the 12 input values. Example: Enter 12 numbers: 13 15 20 13 35 40 16 18 20 18 20 14 highest: 40 lowest: 13
How do I store linked list datas into an array?
Code for Method of Handling Factorials of Any Size?
Given 1 to n distinct random number of which n+1th element was duplicated. How do find the duplicate element and explain the time complexity of the algorithm.
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.
Seat Reservation prog for the theatre. Write a function for seat allocation for the movie tickets. Total no of seats available are 200. 20 in each row. Each row is referred by the Character, "A" for the first row and 'J' for the last. And each seat in a row is represented by the no. 1-20. So seat in diffrent rows would be represented as A1,A2....;B1,B2.....;........J1,J2... Each cell in the table represent either 0 or 1. 0 rep would seat is available , 1 would represent seat is reserved. Booking should start from the last row (J) to the first row(A). At the max 20 seats can be booked at a time. if seats are available, then print all the seat nos like "B2" i.e (2 row, 3 col) otherwise Print "Seats are not available." and we must book consecutive seats only.
1. Write a program using one dimensional array that calculates the sum and average of the five input values from the keyboard and prints the calculated sum and average.
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.
readers and writers problem