Thread: reader-writer problem with accesing multiple readers

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    23

    reader-writer problem with accesing multiple readers

    Hey,

    I have a few problems I can't solve in implementing WRITER-READER problem in UNIX.
    First one is that I have no idea, how can I modify the code to work like threads are always calling to enter reading room. For example, when a writer is in the reading room, readers are waiting to access the reading room. When writer is escaping the reading room and readers are entering the reading room, he still is waiting for his chance.
    The second one is that I have no idea how to modify the code to allow a few readers to enter the reading room. In my code only one thread can be in the same time in the reading room.
    The third one is, how to recognize if writer or reader is starving? Which one is starving in my code?

    Here is the code:

    Code:
        #include <stdio.h>
        #include <pthread.h>
        #include <semaphore.h>
        
        #define READERS 15
        #define WRITERS 10
        
        int bufferw = 0, bufferr = 0, counterw = WRITERS, counterr = READERS;
        int i;
        
        pthread_mutex_t mwrite, mread;
        pthread_cond_t condw, condr;
        pthread_t r[READERS], w[WRITERS];
        
        void *writer(void *ptr) {
            
            pthread_mutex_lock(&mwrite);
            
            {
              counterr = READERS;
              counterw = WRITERS;
              ++bufferw;    
        
             for(i=0; i<READERS; i++) while(bufferr > 0) pthread_cond_wait(&condw, &r[i]);
              
              printf("WRITER ENTERING!");
              
              pthread_mutex_unlock(&mwrite);
            
                bufferw--;
            }
            
            pthread_cond_signal(&condr);
            
            pthread_exit(0);
        }
        
        void *reader(void *ptr) {
            
            counterr = READERS;
            counterw = WRITERS;
        
            {
              ++bufferr;
        
              for(i=0; i<WRITERS; i++) while(bufferw == 1) pthread_cond_wait(&condr, &w[i]);
        
              printf("READER ENTERING!");
            
                bufferr = 0;
            }
            
            pthread_cond_signal(&condw);
            pthread_exit(0);
        }
        
        
        int main(int argc, char* argv[]) {
            
            pthread_mutex_init(&mwrite, 0);
            pthread_mutex_init(&mread, 0);
            
            pthread_cond_init(&condw, 0);
            pthread_cond_init(&condr, 0);
        
            for(i=0; i<WRITERS; i++) pthread_create(&w[i], NULL, writer, NULL);
            for(i=0; i<READERS; i++) pthread_create(&r[i], NULL, reader, NULL);
        
            for(i=0; i<WRITERS; i++) pthread_join(w[i], NULL);
            for(i=0; i<READERS; i++) pthread_join(r[i], NULL);
        
            pthread_cond_destroy(&condw);
            pthread_cond_destroy(&condr);
            pthread_mutex_destroy(&mwrite);
            pthread_mutex_destroy(&mread);
        
         return 0;
        }
    Thanks in advance for your help!

    EDIT:// How can I avoid race in this case?
    Last edited by fkmk; 01-09-2015 at 01:23 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Look up semaphores. You have the header file #include'd, but are not using them.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    23
    grumpy, thanks! I will try to implement them.
    Could you please take a look at this code, why it does not work properly?
    #include <stdio.h> #include <pthread.h> #include <semaphore.h> #define READ - Pastebin.com

    And here is what shows:
    View image: Screenshot 10 01 2015 00 04 37

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reader-writer implementation sync problem
    By Joanna91 in forum C Programming
    Replies: 9
    Last Post: 12-31-2011, 02:32 PM
  2. Reader & Writer problem
    By pantherman34 in forum C Programming
    Replies: 6
    Last Post: 05-04-2010, 07:28 PM
  3. reader/writer problem using semaphores
    By jalex39 in forum C Programming
    Replies: 3
    Last Post: 03-06-2009, 09:54 PM
  4. Replies: 5
    Last Post: 01-10-2009, 02:36 PM
  5. Implementing shared/exclusive locking (readers/writer) lock
    By ruj.sabya in forum Linux Programming
    Replies: 0
    Last Post: 05-08-2008, 12:06 AM