Readers Writers Problem - Semaphore - Systems Lab - C Program


//Readers Writers Problem -OS Lab

Program:
#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);
}

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);
  }
}



21 comments:

  1. thnx :) u just helped me out wid my assignment...cheerio :)

    ReplyDelete
  2. Thanks :D ,you just helped me with my lab work.

    ReplyDelete
  3. you are a great man...

    ReplyDelete
  4. thank you so much :) :)

    ReplyDelete
  5. cc filename.c -lpthread -lrt
    you can excute by above format :)

    ReplyDelete
  6. gcc filename.c -o output -pthread && clear && ./output

    ReplyDelete
  7. can u tell me which priority is it follow??
    either reader or writer or both

    ReplyDelete
  8. give output for the program

    ReplyDelete
  9. How to design and implement an Automatic Car Manufacturing System (ACMS) software that is used in automobile assembly plant in Linux platform. The functioning of ACMS is as follows. Different tasks such as assembling of chassis, body, paint, interior assembly, etc. are handled by different units of the system. Each unit is responsible to complete its operation based on the required variants within specified time limit.

    ReplyDelete
  10. If I'm not wrong you have given priority to the reader here?
    If possible could anyone explain the code to me.

    ReplyDelete
  11. This is quite educational arrange. It has famous breeding about what I rarity to vouch.
    Colossal proverb. This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved.
    This arrange is synchronous s informative impolite festivity to pity. I appreciated what you ok extremely here.


    Selenium interview questions and answers
    Selenium Online training
    Selenium training in Pune
    selenium training in USA
    selenium training in chennai

    ReplyDelete
  12. Welcome, your blog is great. I have read many of your articles. It's very interesting and meaningful, it helps me have more knowledge about life. picbear

    ReplyDelete
  13. output is assign but soo many mistake r there plss checkk...

    ReplyDelete
  14. Thanks for offering us such a useful details. Keep up the great work. You can also check out this awarded Vitamin supplement store for nutritional supplements from leading brands. Nutritional Supplement Store

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...