// Implement Reader-Writer problem using OPENMP (file name rw.c)
#include<stdio.h>
#include<omp.h>
main()
{
int semaphore=0;
#pragma omp parallel num_threads(2) // two thread are created;
{
int i;
int tid=omp_get_thread_num();
do
{
printf("\n thread %d\n%d.read\n%d.write\n5.exit",tid,tid*2,tid*2+1);
//0: read at thread0 ;
//1: write at thread 0;
// 2: read at thread 1;
//3: write at thread 1;
//5: exit thread .;
scanf("%d",&i);
switch(i)
{
case 0: if(semaphore==1)//thread 1 is writing;
{
while(semaphore==1)//wait untill semaphore becomes 0;
{
printf("\n shared data is in use");
sleep(1);
}
}
printf("\n reading in thread 0");
sleep(5);
printf("\n reading done in thread 0");
break;
case 1: if(semaphore==1)// thread 1 is writing
{
while(semaphore==1)// wait until smaphore become 0
sleep(1);
}
semaphore=1;
printf("\n writing in thread 0");
sleep(5);
printf("\n writing done in thread 0");
semaphore=0;
break;
case 2: if(semaphore==1)//thread 0 is writing
{
while(semaphore==1)//wait untill semaphore becomes 0
{
printf("\n shared data is in use");
sleep(1);
}
}
printf("\n reading in thread 1");
sleep(5);
printf("reading done in thread 1");
break;
case 3: if(semaphore==1)//thread 0 is writing
{
while(semaphore==1)//wait untill semaphore becomes 0
{
printf("\n shared data is in use");
sleep(1);
}
}
semaphore=1;
printf("\n writing in thread 1");
sleep(5);
printf("writing done in thread 1");
semaphore=0;
break;
}
}
while(i<5);
}
}
-------------------------------------------------O/P--------------------------------------------------------------
test@test-ThinkCentre-M72e:~$ gcc rw.c -fopenmp
test@test-ThinkCentre-M72e:~$ ./a.out
thread 1
2.read
3.write
5.exit
thread 0
0.read
1.write
2
5.exit
reading in thread 1
reading done in thread 1
thread 1
2.read
3.write
3
5.exit
writing in thread 1
writing done in thread 1
thread 0
0.read
1.write
1
5.exit
writing in thread 0
writing done in thread 0
thread 1
2.read
3.write
5
5.exit
#include<stdio.h>
#include<omp.h>
main()
{
int semaphore=0;
#pragma omp parallel num_threads(2) // two thread are created;
{
int i;
int tid=omp_get_thread_num();
do
{
printf("\n thread %d\n%d.read\n%d.write\n5.exit",tid,tid*2,tid*2+1);
//0: read at thread0 ;
//1: write at thread 0;
// 2: read at thread 1;
//3: write at thread 1;
//5: exit thread .;
scanf("%d",&i);
switch(i)
{
case 0: if(semaphore==1)//thread 1 is writing;
{
while(semaphore==1)//wait untill semaphore becomes 0;
{
printf("\n shared data is in use");
sleep(1);
}
}
printf("\n reading in thread 0");
sleep(5);
printf("\n reading done in thread 0");
break;
case 1: if(semaphore==1)// thread 1 is writing
{
while(semaphore==1)// wait until smaphore become 0
sleep(1);
}
semaphore=1;
printf("\n writing in thread 0");
sleep(5);
printf("\n writing done in thread 0");
semaphore=0;
break;
case 2: if(semaphore==1)//thread 0 is writing
{
while(semaphore==1)//wait untill semaphore becomes 0
{
printf("\n shared data is in use");
sleep(1);
}
}
printf("\n reading in thread 1");
sleep(5);
printf("reading done in thread 1");
break;
case 3: if(semaphore==1)//thread 0 is writing
{
while(semaphore==1)//wait untill semaphore becomes 0
{
printf("\n shared data is in use");
sleep(1);
}
}
semaphore=1;
printf("\n writing in thread 1");
sleep(5);
printf("writing done in thread 1");
semaphore=0;
break;
}
}
while(i<5);
}
}
-------------------------------------------------O/P--------------------------------------------------------------
test@test-ThinkCentre-M72e:~$ gcc rw.c -fopenmp
test@test-ThinkCentre-M72e:~$ ./a.out
thread 1
2.read
3.write
5.exit
thread 0
0.read
1.write
2
5.exit
reading in thread 1
reading done in thread 1
thread 1
2.read
3.write
3
5.exit
writing in thread 1
writing done in thread 1
thread 0
0.read
1.write
1
5.exit
writing in thread 0
writing done in thread 0
thread 1
2.read
3.write
5
5.exit
No comments:
Post a Comment