Created
March 19, 2015 16:32
-
-
Save rajabishek/6209a575f00b122fe490 to your computer and use it in GitHub Desktop.
Semaphore - Reader Writer Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<pthread.h> | |
#include<semaphore.h> | |
sem_t mutex,writeblock; | |
int data = 0,rcount = 0; | |
void *reader(void *arg) | |
{ | |
int f; | |
f = ((int)arg); | |
sem_wait(&mutex); | |
rcount = rcount + 1; | |
if(rcount==1) | |
sem_wait(&writeblock); | |
sem_post(&mutex); | |
printf("Data read by the reader%d is %d\n",f,data); | |
sleep(1); | |
sem_wait(&mutex); | |
rcount = rcount - 1; | |
if(rcount==0) | |
sem_post(&writeblock); | |
sem_post(&mutex); | |
} | |
void *writer(void *arg) | |
{ | |
int f; | |
f = ((int) arg); | |
sem_wait(&writeblock); | |
data++; | |
printf("Data writen by the writer%d is %d\n",f,data); | |
sleep(1); | |
sem_post(&writeblock); | |
} | |
int main() | |
{ | |
int i,b; | |
pthread_t rtid[5],wtid[5]; | |
sem_init(&mutex,0,1); | |
sem_init(&writeblock,0,1); | |
for(i=0;i<=2;i++) | |
{ | |
pthread_create(&wtid[i],NULL,writer,(void *)i); | |
pthread_create(&rtid[i],NULL,reader,(void *)i); | |
} | |
for(i=0;i<=2;i++) | |
{ | |
pthread_join(wtid[i],NULL); | |
pthread_join(rtid[i],NULL); | |
} | |
return 0; | |
} |
mjay4
commented
Sep 27, 2019
via email
It is first writers priority. Once the writer has written something then
only any reader can read.
…On Fri, 27 Sep 2019, 1:49 pm Ferogle, ***@***.***> wrote:
Is it reader's priority or writer's priority??
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/6209a575f00b122fe490?email_source=notifications&email_token=AK3DGUP7G52K2IEMZKCLJETQLW6Y3A5CNFSM4HK6C4BKYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAFZPPM#gistcomment-3038966>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AK3DGUOXBDQNQ4ZHXU2MSM3QLW6Y3ANCNFSM4HK6C4BA>
.
Can you please explain with the lines of code in the writer function how it is the writers priority?
Can you please explain with the lines of code in the writer function how it is the writers priority?
sem_wait(&mutex); rcount = rcount + 1; if(rcount==1) sem_wait(&writeblock);
Inside reader you can see this piece of code, observe carefully:
rcount is 0 initially,
after statement :rcount = rcount+1;
value of rcount is 1,
If rcount is 1, It is calling sem_wait(&writeblock), this will make sure that the read block is on hold for long enough time.
Since now read block is on hold, the write block will be executed.
I hope I have answered your question.
Thank You!
I thought that would force write to wait. Like I thought it would force
write to not be capable of executing if there were new writers after and
that it would need to wait for the reader to finish.
…On Fri, Apr 3, 2020 at 7:59 AM Mritunjay Choubey ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Can you please explain with the lines of code in the writer function how
it is the writers priority?
sem_wait(&mutex); rcount = rcount + 1; if(rcount==1) sem_wait(&writeblock);
Inside reader you can see this piece of code, observe carefully:
rcount is 0 initially,
after statement : rcount = rcount+1;
value of rcount is 1,
If rcount is 1, It is calling sem_wait(&writeblock), this will make sure
that the read block is on hold for long enough time.
Since now read block is on hold, the write block will be executed.
I hope I have answered your question.
Thank You!
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/6209a575f00b122fe490#gistcomment-3239055>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ALC3CNFFK4YB265V7SOHXLDRKXFQZANCNFSM4HK6C4BA>
.
How can I solve this problem using shared_mutex?
I figured it out. Thank you.
…On Fri, Apr 10, 2020 at 10:38 AM jprustom ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
How can I solve this problem using shared_mutex?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/6209a575f00b122fe490#gistcomment-3248877>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ALC3CNFTZSE2JAJ6655OKQ3RL4VOZANCNFSM4HK6C4BA>
.
it is binary semaphore
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment