C程序在OpenMp中使用信号量实现读写器

时间:2015-04-07 14:21:52

标签: c multithreading openmp

我试图用读者优先级实现以下读写器问题,所以首先,所有读者线程都应该执行剩余的写入程序线程。

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

    int var=10;
    int ReadCount=0;

    sem_t Sem;

    void main()
    {
        sem_init(&Sem, 0, 1);
        int ThreadId = 0;
        int NReader, NWriter;
        int i,j;

        printf("\nEnter number of readers: ");
        scanf("%d",&NReader);
        printf("\nEnter number of writers: ");
        scanf("%d",&NWriter);

        #pragma omp parallel num_threads( (NReader+NWriter) )   shared(ThreadId)       /*specifies threadId variable is shared 
                             among all the threads*/                                                                                                 
        {
            printf("\n in parallel construct");



            #pragma omp for nowait
            for(i=0 ; i<NReader ; i++)
            {
                printf("\nReader started %d",i);
                //sleep(5);

                #pragma omp critical
                {           
                    ReadCount++;
                    if(ReadCount==1)
                        sem_wait(&Sem);
                }

                ThreadId = omp_get_thread_num();
                printf("\n\nReader %d with thread id %d is reading shared variable %d ",i,ThreadId,var);    

                #pragma omp critical
                {           
                    ReadCount--;
                    if(ReadCount==0)
                        sem_post(&Sem);
                }
            //  sleep(5);       
            }



            #pragma omp for nowait
            for(j=0 ; j<NWriter ; j++)
            {
                printf("\nWriter started %d",j);


                sem_wait(&Sem);
                sleep(1);

                var=var+2;

                ThreadId = omp_get_thread_num();

                printf("\nWriter %d with ThreadId %d has updated the shared variable to %d ",j,ThreadId,var);

                sem_post(&Sem);


            }


        }
        //end of parallel construct



    }

但是在输出中总是有一些编写器线程在它们之间执行。我不知道为什么会出现这种情况?请有人建议我解决它。

OUTPUT:
                                                                           [eshwar@localhost ~]$ gcc -fopenmp readwrit.c
[eshwar@localhost ~]$ ./a.out

Enter number of readers: 3

Enter number of writers: 2

 in parallel construct
Reader started 0

Reader 0 with thread id 0 is reading shared variable 10 
Writer started 0
 in parallel construct
 in parallel construct
 in parallel construct
Reader started 2
 in parallel construct
Reader started 1
Writer 0 with ThreadId 0 has updated the shared variable to 12 

Reader 2 with thread id 2 is reading shared variable 12 

Reader 1 with thread id 1 is reading shared variable 12 
Writer started 1
Writer 1 with ThreadId 1 has updated the shared variable to 14 [eshwar@localhost ~]$ 

1 个答案:

答案 0 :(得分:-1)

我有一个解决您问题的代码

#include<stdio.h>
#include <time.h>
#include <unistd.h>
#include <omp.h>

int main()
{
 int i=0,NumberofReaderThread=0,NumberofWriterThread;



omp_lock_t writelock;
omp_init_lock(&writelock);

int readCount=0;

 printf("\nEnter number of Readers thread(MAX 10)");
 scanf("%d",&NumberofReaderThread); 
 printf("\nEnter number of Writers thread(MAX 10)");
 scanf("%d",&NumberofWriterThread); 

int tid=0;
#pragma omp parallel
#pragma omp for

 for(i=0;i<NumberofReaderThread;i++)
 {
   // time_t rawtime;
  //struct tm * timeinfo;

//  time ( &rawtime );
  //timeinfo = localtime ( &rawtime );
  //printf ( "Current local time and date: %s", asctime (timeinfo) );
    //sleep(2); 


    printf("\nReader %d is trying to enter into the Database for reading the data",i);


    omp_set_lock(&writelock);
    readCount++;
    if(readCount==1)
    {

      printf("\nReader %d is reading the database",i); 
    }

    omp_unset_lock(&writelock);
    readCount--;
    if(readCount==0)
    {
      printf("\nReader %d is leaving the database",i);  
    }
 }

#pragma omp parallel shared(tid)// Specifies that one or more variables should be shared among all threads.
#pragma omp for nowait     //If there are multiple independent loops within a parallel region
 for(i=0;i<NumberofWriterThread;i++)
 { 


    printf("\nWriter %d is trying to enter into database for modifying the data",i);

    omp_set_lock(&writelock);

    printf("\nWriter %d is writting into the database",i); 
    printf("\nWriter %d is leaving the database",i); 

    omp_unset_lock(&writelock);
 }

  omp_destroy_lock(&writelock); 
 return 0;
}

但这是使用锁机制完成的。您也可以找到信号量的类似步骤。