0

I want to add a contention condition in this code in order to understand the nature of threads. But i am not able to get it through this code.

        #include<stdio.h>
        #include<pthread.h>
        #include<semaphore.h>
        #include<omp.h>

        sem_t rsem;
        sem_t wsem;
        int readCount=0;
        int writeCount=0;
        void *Reader(void *arg);
        void *Writer(void *arg);
        int main()
        {
            int i=0,NumberofReaderThread=0,NumberofWriterThread=0,TID, nthreads;
            sem_init(&rsem,0,1);
            sem_init(&wsem,0,1);
            pthread_t Readers_thr[100],Writer_thr[100];
            printf("\nEnter number of Readers thread(MAX 10) : ");
            scanf("%d",&NumberofReaderThread);
            printf("\nEnter number of Writers thread(MAX 10) : ");
            scanf("%d",&NumberofWriterThread);
            for(i=0;i<NumberofReaderThread;i++)
            {
                pthread_create(&Readers_thr[i],NULL,Reader,(void *)i);
            }
            for(i=0;i<NumberofWriterThread;i++)
            {
                pthread_create(&Writer_thr[i],NULL,Writer,(void *)i);
            }
            for(i=0;i<NumberofWriterThread;i++)
            {
                pthread_join(Writer_thr[i],NULL);
            }
            for(i=0;i<NumberofReaderThread;i++)
            {
                pthread_join(Readers_thr[i],NULL);
            }

            i=0;
            pthread_create(&Readers_thr[i],NULL,Reader,(void *)i);
            pthread_create(&Writer_thr[i],NULL,Writer,(void *)i);
            i=1;
            pthread_create(&Readers_thr[i],NULL,Reader,(void *)i);
            pthread_create(&Writer_thr[i],NULL,Writer,(void *)i);
            i=0;
            pthread_join(Writer_thr[i],NULL);
            pthread_join(Readers_thr[i],NULL);
            i=1;
            pthread_join(Writer_thr[i],NULL);
            pthread_join(Readers_thr[i],NULL);  

            sem_destroy(&rsem);
            sem_destroy(&wsem);
            return 0;
        }

        void * Writer(void *arg)
        {
            int TID, nthreads,temp;
            #pragma omp parallel default(none), private(TID), shared(nthreads,wsem,rsem,temp,arg,writeCount)
            {
                //sleep(3);
                TID = omp_get_thread_num();
                if (TID == 0)
                {
                    nthreads = omp_get_num_threads();
                    printf("\n Number of threads = (%d) \n",nthreads);
                }
                temp=(int)arg;
                printf("\n Thread %d :: Writer %d is trying to enter into database ",TID,temp);


                sem_wait(&wsem);
                writeCount++;
                if(writeCount==1) sem_wait(&rsem);
                printf("\n Thread %d :: Writer %d is writing into the database ",TID,temp);
                    //sleep(5);
                printf("\n Thread %d :: Writer %d is leaving the database ",TID,temp);
                sem_post(&wsem);
                sem_post(&rsem);
            }
        }

        void *Reader(void *arg)
        {
            int TID, nthreads,temp;
            #pragma omp parallel default(none), private(TID),shared(nthreads,wsem,rsem,readCount,temp,arg)
            {
                //sleep(3);
                TID = omp_get_thread_num();
                if (TID == 0)
                {
                    nthreads = omp_get_num_threads();
                    printf("\n Number of threads = (%d) \n",nthreads);
                }
                temp=(int)arg;
                printf("\n Thread %d :: Reader %d is trying to enter into the Database ",TID,temp);
                sem_wait(&rsem);
                readCount++;
                if(readCount==1)
                {
                    sem_wait(&wsem);
                    //sleep(5);
                    printf("\n Thread %d :: Reader %d is reading the database ",TID,temp);
                }
                sem_post(&rsem);
                printf("\n Thread %d :: Reader %d is reading the database ",TID,temp);

                sem_wait(&rsem);
                readCount--;
                printf("\nThread %d :: Reader %d is leaving the database ",TID,temp);

                if(readCount==0)
                {
                    sem_post(&wsem);
                }
                sem_post(&rsem);

            }
        }

Enter number of Readers thread(MAX 10) : 1

Enter number of Writers thread(MAX 10) : 1

Thread 3 :: Reader 0 is trying to enter into the Database Thread 3 :: Reader 0 is reading the database Thread 2 :: Reader 0 is trying to enter into the Database Thread 3 :: Writer 0 is trying to enter into database Thread 1 :: Writer 0 is trying to enter into database Thread 1 :: Reader 0 is trying to enter into the Database Number of threads = (4)

Thread 0 :: Writer 0 is trying to enter into database Thread 2 :: Writer 0 is trying to enter into database Number of threads = (4)

Thread 0 :: Reader 0 is trying to enter into the Database Thread 2 :: Reader 0 is reading the database Thread 2 :: Reader 0 is reading the database Thread 2 :: Reader 0 is leaving the database Thread 1 :: Reader 0 is reading the database Thread 1 :: Reader 0 is leaving the database Thread 0 :: Reader 0 is reading the database Thread 0 :: Reader 0 is reading the database Thread 0 :: Reader 0 is leaving the database Thread 1 :: Writer 0 is writting into the database Thread 1 :: Writer 0 is leaving the database Thread 0 :: Writer 0 is writting into the database
Thread 0 :: Writer 0 is leaving the database Thread 2 :: Writer 0 is writting into the database Thread 2 :: Writer 0 is leaving the database Thread 3 :: Writer 0 is writting into the database
Thread 3 :: Writer 0 is leaving the database Thread 3 :: Writer 0 is trying to enter into database Thread 3 :: Writer 0 is writting into the database Thread 3 :: Writer 0 is leaving the database
Thread 1 :: Writer 0 is trying to enter into database Thread 1 :: Writer 0 is writting into the database Thread 1 :: Writer 0 is leaving the database Thread 2 :: Writer 0 is trying to enter into database Thread 2 :: Writer 0 is writting into the database
Thread 2 :: Writer 0 is leaving the database Number of threads = (4)

Thread 0 :: Writer 0 is trying to enter into database Thread 0 :: Writer 0 is writting into the database Thread 0 :: Writer 0 is leaving the database Thread 1 :: Reader 0 is trying to enter into the Database Thread 1 :: Reader 0 is reading the database Thread 3 :: Reader 1 is trying to enter into the Database Thread 2 :: Reader 0 is trying to enter into the Database Thread 2 :: Reader 0 is reading the database Thread 2 :: Reader 0 is leaving the database Thread 1 :: Reader 0 is reading the database Thread 1 :: Reader 0 is leaving the database Number of threads = (4)

Thread 0 :: Reader 1 is trying to enter into the Database Thread 0 :: Reader 1 is reading the database Thread 0 :: Reader 1 is leaving the database Number of threads = (4)

Thread 0 :: Writer 1 is trying to enter into database Thread 3 :: Reader 1 is reading the database Thread 3 :: Reader 1 is leaving the database Thread 1 :: Writer 1 is trying to enter into database Thread 3 :: Reader 0 is trying to enter into the Database Thread 2 :: Reader 1 is trying to enter into the Database Thread 2 :: Reader 1 is reading the database Thread 2 :: Reader 1 is leaving the database Thread 1 :: Reader 1 is trying to enter into the Database Thread 1 :: Reader 1 is reading the database Thread 1 :: Reader 1 is leaving the database Thread 2 :: Writer 1 is trying to enter into database Thread 3 :: Writer 1 is trying to enter into database Number of threads = (4)

Thread 0 :: Reader 0 is trying to enter into the Database Thread 0 :: Reader 0 is reading the database Thread 0 :: Reader 0 is leaving the database Thread 0 :: Writer 1 is writting into the database Thread 0 :: Writer 1 is leaving the database Thread 1 :: Writer 1 is writting into the database Thread 1 :: Writer 1 is leaving the database Thread 3 :: Reader 0 is reading the database Thread 3 :: Reader 0 is reading the database Thread 3 :: Reader 0 is leaving the database Thread 2 :: Writer 1 is writting into the database Thread 2 :: Writer 1 is leaving the database Thread 3 :: Writer 1 is writting into the database Thread 3 :: Writer 1 is leaving the database

Learner
  • 512
  • 2
  • 7
  • 23
  • What do you mean with *How to achieve contention condition in reader writer program*? Are you trying to create a deadlock? – NathanOliver Apr 15 '16 at 14:42
  • Yes I am. I am trying to create a condition where Two threads read the database simultaneously, which lead to a sort of a deadlock condition due to resource allocation occurring at the concurrent instance of time. – Learner Apr 15 '16 at 14:45

0 Answers0