• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

Readers Writers Problem using Semaphores

April 3, 2011 by ProjectsGeek 2 Comments

Readers Writers Problem using Semaphores

 

Reader Priority problem

 

Readers Writers Problem using SemaphoresReaders Writers problem in operating system for solving synchronization problem can be solved. This program is for unix machine as their only you can run this code. This program demonstrate reader priority code for reader writer problem.
Problem statement for implement Readers Writers problem using semaphores with reader  priority using C language. Use mutex and semaphores to implement above problem in c language environment.

In the reader priority case, the reader will always be given priority to other processes. Writer has to wait every time if their is any read request to be completed. So by this simulation of readers writers problem we can see how actually this problem can be tackled in real time. 

In the context of concurrent programming, the “reader priority case” refers to a scenario where readers are given precedence over writers when accessing a shared resource, such as a database or a file. This means that if there are both read and write requests pending, the system will prioritize servicing the read requests first before attending to any write requests.

In this scenario, whenever a writer wants to access the shared resource, it has to wait if there are any ongoing read operations. This ensures that readers are not kept waiting indefinitely and that their access to the resource is not unnecessarily delayed by write operations.

Simulating the readers-writers problem allows us to observe how different strategies for managing access to shared resources perform in real-world scenarios. By implementing and analyzing various algorithms and techniques for handling concurrent read and write operations, we can gain insights into the trade-offs involved and determine the most efficient and fair approach for different applications.

In essence, through this simulation, we can understand and evaluate the mechanisms for managing reader and writer access to shared resources in a real-time environment, helping us design more robust and efficient concurrent systems.

Readers Writers Problem Code

 

 #include   
 #include   
 #include   
 void * reader(void *) ;  
 void *writer (void *) ;  
 sem_t wsem,mutex ;  
 int readcount=0 ;  
 main()  
 {  
  int a=1,b=1;  
     system("clear");  
  sem_init(&wsem,0,1) ;  
  sem_init(&mutex,0,1) ;  
  pthread_t r,w,r1,w1 ;  
  pthread_create(&r,NULL,reader,(void *)a);  
     a++;  
  pthread_create(&w1,NULL,writer,(void *)b);  
     b++;  
  pthread_create(&r1,NULL,reader,(void *)a);  
  pthread_create(&w,NULL,writer,(void *)b);  
  pthread_join(r,NULL);  
  pthread_join(w1,NULL);  
  pthread_join(r1,NULL);  
  pthread_join(w,NULL) ;  
  printf("main terminated\n");  
 }  
 void * reader(void * arg)  
 {  
  int c=(int)arg ;  
  printf("\nreader %d is created",c);  
     sleep(1);  
  sem_wait(&mutex) ;  
     readcount++;  
     if(readcount==1)  
         sem_wait(&wsem) ;  
  sem_post(&mutex) ;  
 /*Critcal Section */  
  printf("\n\nreader %d is reading\n ",c);  
  sleep(1) ;  
  printf("\nreader%d finished reading\n",c);  
 /* critical section completd */  
  sem_wait(&mutex) ;  
     readcount-- ;  
     if(readcount==0)  
         sem_post(&wsem) ;  
  sem_post(&mutex) ;  
 }  
 void * writer(void * arg)  
 {  
  int c=(int)arg ;  
  printf("\nwriter %d is created",c);  
     sleep(1);  
  sem_wait(&wsem) ;  
  printf("\nwriter %d is writing\n",c) ;  
  sleep(1);  
  printf("\nwriter%d finished writing\n",c);  
  sem_post(&wsem) ;  
 }

 Download Readers writers problem using semaphore

Other Projects to Try:

  1. Readers-writers problem using Semaphore
  2. Queue Operations using C Language
  3. Matrix Operations in C Language
  4. Breadth First and Depth First Search C Language
  5. To Perform File Handling in Java

Filed Under: Operating System Tagged With: OS Problems

Reader Interactions

Comments

  1. rohith says

    November 28, 2015 at 4:56 pm

    readwrite2.c:16: warning: cast to pointer from integer of different size
    readwrite2.c:18: warning: cast to pointer from integer of different size
    readwrite2.c:20: warning: cast to pointer from integer of different size
    readwrite2.c:21: warning: cast to pointer from integer of different size
    readwrite2.c: In function ‘reader’:
    readwrite2.c:30: warning: cast from pointer to integer of different size
    readwrite2.c: In function ‘writer’:
    readwrite2.c:51: warning: cast from pointer to integer of different size
    ?? how to avoid this error?

    Reply
    • ProjectsGeek says

      January 2, 2016 at 12:02 pm

      Try to include stdint.h or inttypes.h in your code and then check.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy