开发者

Reader writers issue: getting different output every time

开发者 https://www.devze.com 2023-03-03 22:42 出处:网络
I have written simple solution for Reader-Writer\'s problem using semaphores in C. But I am getting different output after every successful run. What is the exact reason behind this? Here\'s the code:

I have written simple solution for Reader-Writer's problem using semaphores in C. But I am getting different output after every successful run. What is the exact reason behind this? Here's the code:

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

sem_t x,wsem;
int rc;

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

int svar=0;

int main()
{
    pthread_t w[2],r[3];
    sem_init(&x,0,1);
    sem_init(&wsem,0,1);

    rc=0;

    pthread_create(&w[0],NULL,writer,(void *)0);
    pthread_create(&r[0],NULL,reader,(void *)0);
    pthread_create(&w[1],NULL,writer,(void *)1);
    pthread_create(&r[1],NULL,reader,(void *)1);
    pthread_create(&r[2],NULL,reader,(void *)2);

    pthread_join(r[0],NULL);
    pthread_join(w[0],NULL);
    pthread_join(w[1],NULL);
    pthread_join(r[1],NULL);
    pthread_join(r[2],NULL);

    sem_destroy(&x);
    sem_dest开发者_开发问答roy(&wsem);

    return 0;
}

void *reader(void *arg)
{

        printf("\nReader is executing......");
        sem_wait(&x);
        rc++;
        if (rc == 1)
        sem_wait(&wsem);
        sem_post(&x);
        printf("Reader-%d : value of shared variable : %d\n", (int)arg,svar);
        sem_wait(&x);
        rc--;
        if (rc==0)
            sem_post(&wsem);
        sem_post(&x);

}

void *writer(void *arg)
{

        printf("\nWriter is executing......");
        sem_wait(&wsem);
        svar=svar+5;
        printf("Writer-%d : value of shared variable : %d\n",(int)arg,svar);
        sem_post(&wsem);

}

Output1 : 
Writer is executing......Writer-0 : value of shared variable : 5

Reader is executing......Reader-0 : value of shared variable : 5

Reader is executing......Reader-1 : value of shared variable : 5

Writer is executing......Writer-1 : value of shared variable : 10

Reader is executing......Reader-2 : value of shared variable : 10

Output2: 

Writer is executing......Writer-0 : value of shared variable : 5

Writer is executing......Writer-1 : value of shared variable : 10

Reader is executing......Reader-1 : value of shared variable : 10

Reader is executing......Reader-0 : value of shared variable : 10

Reader is executing......Reader-2 : value of shared variable : 10 

Output3:

Writer is executing......Writer-0 : value of shared variable : 5

Writer is executing......Writer-1 : value of shared variable : 10

Reader is executing......Reader-1 : value of shared variable : 10

Reader is executing......Reader-0 : value of shared variable : 10

Reader is executing......Reader-2 : value of shared variable : 10


Thread execution is non-deterministic. Just because you've launched the threads in a specific order does not mean they'll execute in that order. In this case the way you've structured your semaphores and condition variables, means that once the first writer has written, then either one or more writers can write or one or more readers can read, in no particular order.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号