我正在尝试以读取器优先级实现以下读取器-写入器问题,因此首先,所有读取器线程都应该执行,然后执行剩余的写入器线程。
#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;
}
但是这是使用锁机制来完成的。您也可以找到针对信号量的类似步骤。
关于OpenMp中使用信号量实现读写器的C程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29493901/