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 function- oriented to convert the input dollar(s) into its equivalent peso. Assume that one dollar is equivalent to 51.60
write a program that reads a series of strings and prints only those strings begging with letter "b"
Show by induction that 2n > n2, for all n > 4.
2 Answers Karvy, Qatar University,
write a function that reverse the elements of an array in place.The function must accept only one pointer value and return void.
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
how to write a program that opens a file and display in reverse order?
a program using one dimensional array that searches a number if it is found on the list of given input numbers given by the user and locate its exact location in the list.. ""EXAMPLE"" enter how many numbers to be inputted: 5 12 14 11 09 30 what number to search: 11 11 IS FOUND IN LOCATION 3 PLZZZ.. ELP ME...
how to find out the maximum number out of the three inputs.
6 Answers ABC, Apple, C3I, HP, TCS,
create a stucture student containing field for roll no,class,year and marks.create 10 student annd store them in a file
Teta-Omeg-Big-Oh Show that f(n) = n2 + 3n3 is ;(n3).
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.
0 Answers ABC, Guidance Software,
Deriving time complexity of Binary tree and AVL tree, step by step.