**WRITE CODE IN C++** In this lab you will simulate one of the classical synchro
ID: 3605409 • Letter: #
Question
**WRITE CODE IN C++**
In this lab you will simulate one of the classical synchronization problems in order to see how the (semi) critical section problem could be implemented using binary and counting semaphores.
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++. 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.
Subproject 1: You should implement binary and counting semaphores as studied in the class for this project.
Subproject 2: You should implement test and set operation as studied in the class for this project.
Explanation / Answer
//Include libraries
#include<iostream>
#include<pthread.h>
#include<semaphore.h>
#include <string.h>
//Use std namespace
using namespace std;
//Define semaphore
sem_t lRaccss;
//Define semaphore
sem_t lWaccss;
//Declare variable
int lRcnt=0;
//Declare array
char lBffr[50];
//Define a method for reader
void * Reader(void *arg)
{
//Call method
sem_wait(&lRaccss);
//Increment
lRcnt++;
//If count is 1
if(lRcnt==1)
//Call method
sem_wait(&lWaccss);
//Call method
sem_post(&lRaccss);
//Display message
cout<<"Inside reader Buffer value is "<<lBffr<<endl;
//End of line
cout<<endl;
//Call method
sem_wait(&lRaccss);
//Decrement value
lRcnt--;
//If count is 0
if(lRcnt==0)
//Call method
sem_post(&lWaccss);
//Call method
sem_post(&lRaccss);
}
//Define a method for writer
void * Writer(void *arg)
{
//Declare variable
int lWNum=*(int*)arg;
//Call method
sem_wait(&lWaccss);
//End of line
cout<<endl;
//Display message
cout<<"writer number"<<lWNum<<endl;
//Display message
cout<<"Before Inside writer Buffer value is "<<lBffr<<endl;
//Display message
strcpy(lBffr,"test data written to Buffer");
//Display message
cout<<"After Inside writer Buffer value is "<<lBffr<<endl;
//Call method
sem_post(&lWaccss);
}
//Define main method
int main()
{
//Generate random number
int x = rand()%2;
//Declare array
pthread_t lReaders[4];
//Declare array
pthread_t lWrtrs[3];
//Display message
strcpy(lBffr,"Not used so far");
//Call method
sem_init(&lRaccss,0,1);
//Call method
sem_init(&lWaccss,0,1);
//Loop until length
for(int li=0; li<4;li++)
{
//Call method
pthread_create(&lReaders[li],NULL,Reader,(void *)&li);
}
//Loop until length
for(int li=0; li<3;li++)
{
//Call method
pthread_create(&lWrtrs[li],NULL,Writer,(void*)&li);
}
//Define switch case
switch(x)
{
//Define case 0
case 0:
//Loop until length
for(int li=0; li<4;li++)
{
//Call method
pthread_join(lReaders[li],0);
}
//Break
break;
//Define case 1
case 1:
//Loop until length
for(int li=0; li<3;li++)
{
//Call method
pthread_join(lWrtrs[li],0);
}
//Break
break;
//Default
default:
//Break
break;
}
//Call method
sem_destroy (&lRaccss);
//Call method
sem_destroy (&lWaccss);
//Return 0
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.