Thread: Reader & Writer problem

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    58

    Reader & Writer problem

    Code:
    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <semaphore.h>
    
    #define NWRITER 2		/* total number of producers - 1 */
    #define NREADER 5		/* total number of consumers - 1 */
    #define NORW 4		/* number of items writen/read */
    
    typedef struct {
        sem_t mutex;    	  /* enforce mutual exclusion to shared data */
        sem_t db;
    } rwbuff_t;
    
    /* instance shared of sbuf_t */
    rwbuff_t shared;
    
    /* number of processes reading or wanting to */
    int rc = 0;
    /* number of processes writing or wanting to */
    int wc = 0;
    
    /* function prototypes */
    void *Writer(void *arg);
    void *Read(void *arg);
    
    /* increments within the mutex */
    int twrite, tread;
    
    int main()
    {
       /* writer/read threads */
        pthread_t tWrite, tRead;
    
        /* creates semaphores */
        sem_init(&shared.mutex, 0, 1);
        sem_init(&shared.db, 0, 1);
        
        /* loop control */
        int index;
         
        /* creates write thread */
        for (index = 1; index < NWRITER; index++)
        {
           /* Create 1 write */
           pthread_create(&tWrite, NULL, Writer, (void*)index);
        }
    
        /* creates 4 read threads */
        for (index = 1; index < NREADER; index++)
        {
           pthread_create(&tRead, NULL, Read, (void*)index);
        }
    
        /* temp variables to compute the consumes and produces per hour */
        int writePH, readPH, ratio;
    
        /* keeps track of seconds */
        int seconds = 1;
    
        while(1)
        {
           sleep(1);
    
           readPH = ((tread * 3600) / seconds);
           printf("\nOperations after %d seconds: \n\n", seconds);
           printf("Number of read operations completed per hour : %d\n", readPH);
           
          
           writePH = ((twrite * 3600) / seconds);  
           ratio = writePH / readPH;
           printf("Number of write operations completed per hour: %d\n", writePH);
           printf("Ratio of reads per hour over writes per hour: %d\n\n", ratio);
           seconds++;
        }
        /* destroy semaphore */
        sem_destroy(&shared.mutex); 
        sem_destroy(&shared.db);
        pthread_exit(NULL);
    }
    
    void *Read(void *arg)
    {
       int index, i;
    
       index = (int)arg;
        
        printf("Read thread - %d - in line to read...\n\n", index);
    
        for (i = 0; i < NORW; i++) 
        {
    
            sem_wait(&shared.mutex);
            rc++;
            if (rc == 1)           
            {
               sem_wait(&shared.db);
               sem_post(&shared.mutex);
               printf("Reader %d in criticial section\n",i);
              // read_data_base();
               sem_wait(&shared.mutex);
               rc--;
            }
            if(rc == 0)
            {
                  sem_post(&shared.db);
                  sem_post(&shared.mutex);
                  printf("Reader %d in non-critical section\n", i);
            }
            sleep(1);
        }
    }
    
    void *Writer(void *arg)
    {
        int data, index, i;
    
        index = (int)arg;
    
        printf("Write thread %d - in line to write...\n\n", index);
    
        for (i = 0; i < NORW; i++) {
    
    
            printf("Writer tries to enter the critical section \n");
            sem_wait(&shared.db);
          //  write_data_base();
            printf("Writer is in critical region\n");
            sem_post(&shared.db);
            printf("Writer is in non-critical region\n");
        }
        sleep(1);
    }
    Last edited by pantherman34; 05-04-2010 at 05:40 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It runs, but something is definitely amiss!
    What does that even mean? I know there are a couple of people here that love to run random code just because people like you don't give them any idea as to what's really going on, but I'm not one of them.

    So, what exactly is the problem with your program and the way it runs that is 'amiss'?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    58
    i updated the code above. for one, the writer is supposed to write something. its in red. im not sure what to do there. and the reader is supposed to read, and im not sure what to do there either. it too is in red.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I know this is going to sound obvious, but you could actually try reading or writing something. I assume you know where you're supposed to be reading from/writing to?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    58
    here is up'd code. i am getting the equal amount of reads as to writes....since i have 4 read threads and only 1 write thread....shouldnt i be getting 4x more reads than writes?!

    Code:
    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <semaphore.h>
    
    #define NWRITER 2		/* total number of producers - 1 */
    #define NREADER 5		/* total number of consumers - 1 */
    #define NORW 4		/* number of items writen/read */
    
    typedef struct {
        sem_t mutex;    	  /* enforce mutual exclusion to shared data */
        sem_t db;
    } rwbuff_t;
    
    /* instance shared of sbuf_t */
    rwbuff_t shared;
    
    /* number of processes reading or wanting to */
    int rc = 0;
    
    
    /* function prototypes */
    void *Writer(void *arg);
    void *Read(void *arg);
    
    /* increments within the mutex */
    int countW, countR;
    
    int write_request = 0;
    
    int main()
    {
       /* writer/read threads */
        pthread_t tWrite, tRead;
    
        /* creates semaphores */
        sem_init(&shared.mutex, 0, 1);
        sem_init(&shared.db, 0, 1);
        
        /* loop control */
        int index;
         
        /* creates write thread */
        for (index = 1; index < NWRITER; index++)
        {
           /* Create 1 write */
           pthread_create(&tWrite, NULL, Writer, (void*)index);
        }
    
        /* creates 4 read threads */
        for (index = 1; index < NREADER; index++)
        {
           pthread_create(&tRead, NULL, Read, (void*)index);
        }
    
        /* temp variables to compute the consumes and produces per hour */
        int writePH, readPH, ratio;
    
        /* keeps track of seconds */
        int seconds = 1;
    
        while(1)
        {
           sleep(1);
    
           readPH = ((countR * 3600) / seconds);
           printf("\nOperations after %d seconds: \n\n", seconds);
           printf("Number of read operations completed per hour : %d\n", readPH);
           if (countW == 0)
           {
              printf("Number of write operations completed per hour: %d\n", countW);
              printf("The ratio cannot be calculated until a write operations "
                          "has taken place\n");
           }
           else
           {
               writePH = ((countW * 3600) / seconds);
               ratio = writePH / readPH;
               printf("Number of write operations completed per hour: %d\n", writePH);
               printf("Ratio of reads per hour over writes per hour: %d\n\n", ratio);
    
           }  seconds++;
        }
        /* destroy semaphore */
        sem_destroy(&shared.mutex); 
        sem_destroy(&shared.db);
        pthread_exit(NULL);
    }
    
    void *Read(void *arg)
    {
       int index, i;
    
       index = (int)arg;
        
        printf("Read thread %d - in line to read...\n\n", index);
        
        while (1)
        {
    
            sem_wait(&shared.mutex);
            rc++;
            if (rc == 1)
            {
               sem_wait(&shared.db);
               sem_post(&shared.mutex);
               printf("Reader %d in criticial section\n",index); 
               countR++;
               sleep(1);
              // read_data_base();
               sem_wait(&shared.mutex);
               rc--;
            }
            if(rc == 0)
            {
                  sem_post(&shared.db);
                  sem_post(&shared.mutex);
                  printf("Reader %d in non-critical section\n", index);
                  // use_data_read();
            }
    
        }
    }
    
    void *Writer(void *arg)
    {
        int data, index, i;
    
        index = (int)arg;
    
        printf("Write thread %d - in line to write...\n\n", index);
    
        while (1)
        {
    
           //think_up_data();
            printf("Writer tries to enter the critical section \n");
            sem_wait(&shared.db);
    
          //  write_data_base();
            printf("Writer is in critical region\n");
            countW++;
            sleep(1);
            sem_post(&shared.db);
            printf("Writer is in non-critical region\n");
        }
        sleep(1);
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by pantherman34 View Post
    here is up'd code. i am getting the equal amount of reads as to writes....since i have 4 read threads and only 1 write thread....shouldnt i be getting 4x more reads than writes?!
    Where would they be getting the extra data to read from? Your consumers shouldn't be consuming more than your producer is producing, right?

    EDIT: Although, you're not actually reading or writing (still) are you. Just for fun, I ran the code for 50 seconds and did not get a single read event....
    Last edited by tabstop; 05-04-2010 at 07:12 PM.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    58
    you have a point. it should probably be 1:1. The code works fine for me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Adobe Reader 6 activation problem
    By geek@02 in forum Tech Board
    Replies: 2
    Last Post: 10-13-2006, 11:25 AM
  5. Problem with opening file and pathname
    By stevespai in forum C# Programming
    Replies: 3
    Last Post: 07-13-2006, 08:03 AM