Due date: June 27,2017 How to submit? :Submit the hardcopy of your source code a
ID: 3850586 • Letter: D
Question
Due date: June 27,2017 How to submit? :Submit the hardcopy of your source code and sample result 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. 7 processes are characterized by 4 readers and 3 writers. Up to three 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 implementany 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 real buffer. (Optiona) 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 function to choose process to execute. The 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 turm.) You can implement this using switch statement in Cor CH. 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 ofprogram counter ofeach process to resume at the right place once it will be chosen to run by keeping global counter variable per subproiect 1. You should implement binary and counting semaphores as studied in the class for MacBook AirExplanation / Answer
// if you like the answer please do thumbs up
#include<iostream>
#include<pthread.h>
#include<semaphore.h>
#include <string.h>
using namespace std;
sem_t readAccess;
sem_t writeAccess;
int readCount=0;
char buffer[50];
void * Reader(void *arg)
{
sem_wait(&readAccess);
readCount++;
if(readCount==1)
sem_wait(&writeAccess);
sem_post(&readAccess);
cout<<endl;
cout<<"Inside reader Buffer value is "<<buffer<<endl;
sem_wait(&readAccess);
readCount--;
if(readCount==0)
sem_post(&writeAccess);
sem_post(&readAccess);
}
void * Writer(void *arg)
{
int writerNo=*(int*)arg;
sem_wait(&writeAccess);
cout<<endl;
cout<<"writer number"<<writerNo<<endl;
cout<<"Before Inside writer Buffer value is "<<buffer<<endl;
strcpy(buffer,"test data written to buffer");
cout<<"After Inside writer Buffer value is "<<buffer<<endl;
sem_post(&writeAccess);
}
int main()
{
pthread_t readers[4];
pthread_t writers[3];
strcpy(buffer,"Not used so far");
sem_init(&readAccess,0,1);
sem_init(&writeAccess,0,1);
for(int i=0; i<4;i++)
{
pthread_create(&readers[i],NULL,Reader,(void *)&i);
}
for(int i=0; i<3;i++)
{
pthread_create(&writers[i],NULL,Writer,(void*)&i);
}
for(int i=0; i<4;i++)
{
pthread_join(readers[i],0);
}
for(int i=0; i<3;i++)
{
pthread_join(writers[i],0);
}
sem_destroy (&readAccess);
sem_destroy (&writeAccess);
return 0;
}
output:
cout<<endl;
cout<<"Inside reader Buffer value is "<<buffer<<endl;
Inside reader Buffer value is Not used so far
Inside reader Buffer value is Not used so far
Inside reader Buffer value is Not used so far
Inside reader Buffer value is Not used so far
writer number1
Before Inside writer Buffer value is Not used so far
After Inside writer Buffer value is test data written to buffer
writer number2
Before Inside writer Buffer value is test data written to buffer
After Inside writer Buffer value is test data written to buffer
writer number3
Before Inside writer Buffer value is test data written to buffer
After Inside writer Buffer value is test data written to buffer
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.