Answered step by step
Verified Expert Solution
Link Copied!

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

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 writers. Up to two reader processes can be inside their critical section without any writer process. For writer process to go into its critical section, it should check whether there is any reader or writer process is in the critical section. Critical section in this problem is reading shared data buffer for reader and updating shared data buffer for writer processes. It is up to you to implement any shared data for readers and writers but you have to specify clearly following things in your sample output. • When reader or writer enters its critical section, it has to report whether there are any reader(s) or writer(s) other than itself. • You may print out the data you read or write when you implement real buffer. (Optional) • You have to print out “Panic Messages” when the rules behind this semi critical section problem are not observed. In your main program, you run the random number generator function to choose process to execute. The chosen process starts (resumes) execution and after one instruction, it will be returned. (You should force each process run exactly one instruction then returns and waiting for its turn.) You can implement this using switch statement in C or C++. Do not use any multi-threading nor mutex feature from programming language. Each process is one big switch statement and will be returned after each instruction. You need to keep track of program counter of each process to resume at the right place once it will be chosen to run by keeping global counter variable per process. You should implement binary and counting semaphores as studied in the class for this project.

#include
#include
#include
# include
#include
using namespace std;

int s=0;
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++;
}

int r1(){
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."< v(s);// leave the critical section and unlock the door
r1counter++;
break;
}
}

int r2(){
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."< v(s);// leave the critical section and unlock the door
r2counter++;
break;
}
}

int r3(){
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."< v(s);// leave the critical section and unlock the door
r3counter++;
break;
}
}

int wrt1(){
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 "< v(s);
wrt1counter++;
break;
}

}

int wrt2(){
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 "< v(s);
wrt2counter++;
break;
}

}


int main() {

cout<<"Reader Writer problem"< while (true){
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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Computer Performance Engineering 10th European Workshop Epew 2013 Venice Italy September 17 2013 Proceedings

Authors: Maria Simonetta Balsamo ,William Knottenbelt ,Andrea Marin

2013 Edition

3642407242, 978-3642407246

More Books

Students also viewed these Programming questions

Question

Why is the study of lubrication regimes important?

Answered: 1 week ago