Answered step by step
Verified Expert Solution
Question
1 Approved Answer
c++ reader and writer problem fixing codes need to use switch statement but not using mutex 5 processes are characterized by 3 readers and 2
#include
#include
# include
#include
using namespace std;
int counter, r1counter=0, r2counter=0, r3counter=0, wrt1counter=0, wrt2counter=0;// shared
void p(int s)
{
while (s<=0 )
;
s--;
}
void v(int s)
{
s++;
}
switch (r1counter)
{
case 0:
p(s);//check the door, if locked do nothing and come back
// entering the critical section and lock the door
cout<<"I am the reader."<
r1counter++;
break;
}
}
switch (r2counter)
{
case 0:
p(s);//check the door, if locked do nothing and come back
// entering the critical section and lock the door
cout<<"I am the reader."<
r2counter++;
break;
}
}
switch (r3counter)
{
case 0:
p(s);//check the door, if locked do nothing and come back
// entering the critical section and lock the door
cout<<"I am the reader."<
r3counter++;
break;
}
}
switch (wrt1counter)
{
case 0:
p(s);
// here the writer is checking if anyone is in the critical section
// if not it enters the critical section and says..
cout<<"I am writing in the CS, there is no one else here "<
wrt1counter++;
break;
}
}
switch (wrt2counter)
{
case 0:
p(s);
// here the writer is checking if anyone is in the critical section
// if not it enters the critical section and says..
cout<<"I am writing in the CS, there is no one else here "<
wrt2counter++;
break;
}
}
int main() {
for (int i = 0; i<500;i++){
int coin = rand()%5;
switch (coin)
{
case 0: r1();
break;
case 1: r2();
break;
case 2: r3();
break;
case 3: wrt1();
break;
case 4: wrt2();
break;
}
}
}
system ("pause");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started