Help in Pthread and Mutex program

This is a discussion on Help in Pthread and Mutex program within the C Programming forums, part of the General Programming Boards category; hello. I'm trying to solve a problem in C, with an efficient system of locks(mutex). Is a simulation of writer/reader ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    Help in Pthread and Mutex program

    hello.

    I'm trying to solve a problem in C, with an efficient system of locks(mutex). Is a simulation of writer/reader program.

    The program have three restrictions:
    - there are multiple readers who can simultaneously read what the writers have written;
    - simultaneous writes by different writers are not allowed;
    - simultaneous writing and readind is not allowed;

    This is a example what the program should print:

    W0: Start
    W0: End
    W1: Start
    W1: End
    R0: Start
    R1: Start
    R0: End
    R2: Start
    R1: End
    R2: End
    W2: Start
    W2: End

    I've walked around with it and can't figured out how to solve the third restriction.

    I have some clues:
    You will need at least two locks. You should count the number of threads that are simultaneously reading and allow only the first thread of the batch to lock out the writers and the last one to finish the read to unlock them.
    I already do this code, but is not working in the correct manner:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <pthread.h> 
    
    #define N 3
    
    /*Declaration*/
    void *writer(int n);
    void *reader(int n);
    void clean();
    
    /*Init*/
    
    pthread_mutex_t mutex,mutex1;
    
    int main(int argc, char **argv)
    {
            int i;
            pthread_t thrR[N];
            pthread_t thrW[N];
            
            pthread_mutex_init(&mutex,NULL);
            pthread_mutex_init(&mutex1,NULL);
    
            
            for(i=0;i<N;i++){
                    
                    pthread_create(&thrW[i],NULL,writer, (void *)i);
                    pthread_create(&thrR[i],NULL,reader, (void *)i);
            }
            
            for(i=0;i<N;++i){
                    pthread_join(thrW[i], NULL);
                    pthread_join(thrR[i], NULL);
            }
            
            /*Clean Resources*/
            clean();
            
            return 0;
    }
    
    void *writer(int n){
            int i;
                    
            for(i=0;i<5;++i){
                    
                    pthread_mutex_lock(&mutex);
    
                    printf("W%d:S \n",n);
                    sleep(2*n);
                    printf("W%d:E \n",n);
                    
                    pthread_mutex_unlock(&mutex);
    
            }
    
            return NULL;
    }
    
    void *reader(int n){
            int i=0;
            
            if (n==0) pthread_mutex_unlock(&mutex);
            
            while(i<5){
                            
                            printf("R%d:S \n",n);
                            pthread_mutex_lock(&mutex1);
                            
                            sleep(n);
                            
                            pthread_mutex_unlock(&mutex1);
                            printf("R%d:E \n",n);
                            i++;
            }                        
                                    
            if (n==N) pthread_mutex_lock(&mutex);
    
            return NULL;
    }
    
    void clean()
    {
            pthread_mutex_destroy(&mutex);
            pthread_mutex_destroy(&mutex1);
    }

    I hope someone can help me.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    Nobody can help? Even if it's some tips on mutex.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex, Cond and Thread questions (pthread, linux)
    By thebearot in forum C Programming
    Replies: 14
    Last Post: 04-23-2010, 12:10 PM
  2. mutex necessity
    By mynickmynick in forum C Programming
    Replies: 9
    Last Post: 07-14-2008, 09:23 AM
  3. mutex and padding ?!
    By mynickmynick in forum C Programming
    Replies: 1
    Last Post: 06-30-2008, 11:17 AM
  4. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 12:43 AM
  5. pthread mutex problemos
    By cnchybrid in forum C Programming
    Replies: 0
    Last Post: 04-05-2007, 11:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21