0% found this document useful (0 votes)
12 views2 pages

Ex 5 Reader Writer Problem

Uploaded by

ramsrini533
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Ex 5 Reader Writer Problem

Uploaded by

ramsrini533
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

5.

READERS WRITERS PROBLEM


The readers-writers problem is a classical problem of process synchronization, it relates to a data set
such as a file that is shared between more than one process at a time. Among these various processes,
some are Readers - which can only read the data set; they do not perform any updates, some are
Writers - can both read and write in the data sets.
The readers-writers problem is used for managing synchronization among various reader and writer
process so that there are no problems with the data sets, i.e. no inconsistency is generated.
If two or more than two readers want to access the file at the same point in time there will be no
problem. However, in other situations like when two writers or one reader and one writer wants to
access the file at the same point of time, there may occur some problems.
Solution is, if one reader is reading then no writer is allowed to update at the same point of time,
similarly, if one writer is writing no reader is allowed to read the file at that point of time and if one
writer is updating a file other writers should not be allowed to update the file at the same point of
time. Multiple readers can access the object at the same time.

Program
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>

sem_t x,y;
pthread_t tid;
pthread_t writerthreads[100],readerthreads[100];
int readercount = 0;

void *reader(void* param)


{
sem_wait(&x);
readercount++;
if(readercount==1)
sem_wait(&y);
sem_post(&x);
printf("%d reader is inside\n",readercount);
usleep(3);
sem_wait(&x);
readercount--;
if(readercount==0)
{
sem_post(&y);
}
sem_post(&x);
printf("%d Reader is leaving\n",readercount+1);
return NULL;
}

void *writer(void* param)


{
printf("Writer is trying to enter\n");
sem_wait(&y);
printf("Writer has entered\n");
sem_post(&y);
printf("Writer is leaving\n");
return NULL;
}

int main()
{
int n2,i;
printf("Enter the number of readers:");
scanf("%d",&n2);
printf("\n");
int n1[n2];
sem_init(&x,0,1);
sem_init(&y,0,1);
for(i=0;i<n2;i++)
{
pthread_create(&writerthreads[i],NULL,reader,NULL);
pthread_create(&readerthreads[i],NULL,writer,NULL);
}
for(i=0;i<n2;i++)
{
pthread_join(writerthreads[i],NULL);
pthread_join(readerthreads[i],NULL);
}
}

OUTPUT
1 reader is inside
Writer is trying to enter
2 reader is inside
2 Reader is leaving
2 reader is inside
Writer is trying to enter
2 Reader is leaving
1 Reader is leaving

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy