I need help getting this code to run. It is a concurrency simulator in JBACI. But I get a deadlock issue. It is a code in JBACI C---. Thank you. semaphore full, empty, mutex; const int SIZE = 8; int arr[SIZE]; const int MAX = 15; int producerNum = 0; int consumer = 0; int buffer[SIZE]; void theProducer(){ int i; for(i=0;i
I need help getting this code to run. It is a concurrency simulator in JBACI. But I get a deadlock issue. It is a code in JBACI C---. Thank you.
semaphore full, empty, mutex;
const int SIZE = 8;
int arr[SIZE];
const int MAX = 15;
int producerNum = 0;
int consumer = 0;
int buffer[SIZE];
void theProducer(){
int i;
for(i=0;i<MAX;i++){
wait(mutex);
producerNum++;
buffer[producerNum-1]=i+1;
cout << "Item " << buffer[producerNum-1] << " is produced" << endl;
if(producerNum == SIZE){
wait(empty);
producerNum = 0;{
signal(full);
signal(mutex);
}
}
}
}
void theConsumer(){
int i;
for(i=0; i < MAX; i++){
wait(mutex);
consumer++;
cout << "Item " <<buffer[consumer-1] << " is consumed" << endl;
buffer[consumer-1] = 50;
if(consumer == SIZE){
wait(full);
consumer = 0;{
signal(empty);
signal(mutex);
}
}
}
}
void myStatement(){
}
void main(){
myStatement();
initialsem(full, SIZE);
initialsem(mutex, 1);
initialsem(empty, 0);
cobegin{
theProducer();
theConsumer();
}
}
Step by step
Solved in 2 steps