簡體   English   中英

C程序在OpenMp中使用信號量實現閱讀器編寫器

[英]C program to implement reader writer using semaphore in 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 ~]$ 

我有一個可以解決您問題的代碼

#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;
}

但這是使用鎖定機制完成的。 您也可以找到類似的信號量步驟。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM