Complete the following code. The goal is to implement the producer-consumer problem. You are expected to extend the provided C code to synchronize the thread operations consumer() and producer() such that an underflow and overflow of the queue is prevented. You are not allowed to change the code for implementing the queue operations, that is the code between lines 25 and 126 as shown in the screenshot. You must complete the missing parts as shown in the screenshot as well as complete the missing codes of producer and consumer. #include #include #include #include #include #include #include #include #define MAX_LENGTH_CAP 100 #define INIT -127 #define UNDERFLOW (0x80 + 0x02) #define OVERFLOW 0x80 + 0x01 #define BADPTR (0x80 + 0x03) #define CONSUMER_TERMINATION_PROBABILITY 40 #define PRODUCER_TERMINATION_PROBABILITY 30 // ============= LOCKED ============ //sem_t ACCESS; // use a mutex instead of a binary semaphore pthread_mutex_t ACCESS = PTHREAD_MUTEX_INITIALIZER; sem_t *FULLSPOTS; sem_t *EMPTYSPOTS; struct timespec remaining; struct timespec requested_time; struct Queue{ unsignedint head; unsignedint tail; unsignedint length; int8_t* data; }; void enqueue(struct Queue *q, char x) { if(q==NULL) { printf("\nequeue()>> bad queue pointer %x\n", BADPTR); return; } if ( q->head == (q->tail + 1) % q->length){ printf("\nenqueue()>> Attempt to overflow the queue at %p was prevented.\n", q); } else{ // Why is the following commented code wrong (hint: checking for the underflow condition): // wrong: q->tail = (q->tail+1) % q->length; q->data[q->tail] = x; q->data[q->tail-1] = x; // because arrays start at index 0 but our queue starts at index 1 we should write q->data[q->tail-1] = x; // now we must increment the tail if(q->tail == q->length){ q->tail = 1; } else{ q->tail = q->tail + 1; // this line must happen before enqueue is interruption } } } char dequeue(struct Queue *q) { char val; if(q==NULL) { printf("\ndequeue()>> bad queue pointer %x\n", BADPTR); return (char) BADPTR; } if(q->head == q->tail){ printf("\ndequeue()>> Attempt to underflow the queue at %p was prevented.\n", q); return (char) UNDERFLOW; } else{ if(!q->data) // as a rule, we always check pointers before we access them { printf("dequeue()>> bad data pointer %x\n", BADPTR); return (char) BADPTR; } val = q->data[q->head-1]; // because arrays start at index 0 but our queue starts at index 1 we should write q->data[q->head-1]; q->data[q->head-1] = INIT; if (q->head == q->length){ q->head = 1; } else{ q->head = q->head + 1; } } return (char) val; } void build(struct Queue **q, unsigned int length) { printf("\nbuild()>> Building a queue of length %d", length); if( (length == 0) || (length > MAX_LENGTH_CAP) ) { printf("\nbuild()>> Invalid length."); return; } struct Queue *ptr=NULL; if ( ! (ptr = (struct Queue*) malloc(sizeof(struct Queue))) ) { printf("\nbuild()>> Failed to allocate memory for the structure."); return; } printf("\nbuild()>> Allocated memory for the structure at the address %p", ptr); if ((ptr->data = (int8_t*) malloc(length*sizeof(int8_t)) ) == NULL ) { printf("\nbuild()>> Failed to allocate memory of size %lu for queue's data", length * sizeof(char)); } ptr->length = length; ptr->head = 1; ptr->tail = 1; printf("\nbuild()>> Initialize all elements of data to %d\n", (int8_t) INIT); for (int i=0; idata[i] = (int8_t) INIT; *q = ptr; } // ================= UNLOCKED ==================== void *producer( void *qptr ) { } void *consumer( void *qptr) { pthread_exit(0); } void closeup(void); int main(int argc, const char * argv[]) { return0; } void closeup(void)
Complete the following code. The goal is to implement the producer-consumer problem. You are expected to extend the provided C code to synchronize the thread operations consumer() and producer() such that an underflow and overflow of the queue is prevented. You are not allowed to change the code for implementing the queue operations, that is the code between lines 25 and 126 as shown in the screenshot. You must complete the missing parts as shown in the screenshot as well as complete the missing codes of producer and consumer. #include #include #include #include #include #include #include #include #define MAX_LENGTH_CAP 100 #define INIT -127 #define UNDERFLOW (0x80 + 0x02) #define OVERFLOW 0x80 + 0x01 #define BADPTR (0x80 + 0x03) #define CONSUMER_TERMINATION_PROBABILITY 40 #define PRODUCER_TERMINATION_PROBABILITY 30 // ============= LOCKED ============ //sem_t ACCESS; // use a mutex instead of a binary semaphore pthread_mutex_t ACCESS = PTHREAD_MUTEX_INITIALIZER; sem_t *FULLSPOTS; sem_t *EMPTYSPOTS; struct timespec remaining; struct timespec requested_time; struct Queue{ unsignedint head; unsignedint tail; unsignedint length; int8_t* data; }; void enqueue(struct Queue *q, char x) { if(q==NULL) { printf("\nequeue()>> bad queue pointer %x\n", BADPTR); return; } if ( q->head == (q->tail + 1) % q->length){ printf("\nenqueue()>> Attempt to overflow the queue at %p was prevented.\n", q); } else{ // Why is the following commented code wrong (hint: checking for the underflow condition): // wrong: q->tail = (q->tail+1) % q->length; q->data[q->tail] = x; q->data[q->tail-1] = x; // because arrays start at index 0 but our queue starts at index 1 we should write q->data[q->tail-1] = x; // now we must increment the tail if(q->tail == q->length){ q->tail = 1; } else{ q->tail = q->tail + 1; // this line must happen before enqueue is interruption } } } char dequeue(struct Queue *q) { char val; if(q==NULL) { printf("\ndequeue()>> bad queue pointer %x\n", BADPTR); return (char) BADPTR; } if(q->head == q->tail){ printf("\ndequeue()>> Attempt to underflow the queue at %p was prevented.\n", q); return (char) UNDERFLOW; } else{ if(!q->data) // as a rule, we always check pointers before we access them { printf("dequeue()>> bad data pointer %x\n", BADPTR); return (char) BADPTR; } val = q->data[q->head-1]; // because arrays start at index 0 but our queue starts at index 1 we should write q->data[q->head-1]; q->data[q->head-1] = INIT; if (q->head == q->length){ q->head = 1; } else{ q->head = q->head + 1; } } return (char) val; } void build(struct Queue **q, unsigned int length) { printf("\nbuild()>> Building a queue of length %d", length); if( (length == 0) || (length > MAX_LENGTH_CAP) ) { printf("\nbuild()>> Invalid length."); return; } struct Queue *ptr=NULL; if ( ! (ptr = (struct Queue*) malloc(sizeof(struct Queue))) ) { printf("\nbuild()>> Failed to allocate memory for the structure."); return; } printf("\nbuild()>> Allocated memory for the structure at the address %p", ptr); if ((ptr->data = (int8_t*) malloc(length*sizeof(int8_t)) ) == NULL ) { printf("\nbuild()>> Failed to allocate memory of size %lu for queue's data", length * sizeof(char)); } ptr->length = length; ptr->head = 1; ptr->tail = 1; printf("\nbuild()>> Initialize all elements of data to %d\n", (int8_t) INIT); for (int i=0; idata[i] = (int8_t) INIT; *q = ptr; } // ================= UNLOCKED ==================== void *producer( void *qptr ) { } void *consumer( void *qptr) { pthread_exit(0); } void closeup(void); int main(int argc, const char * argv[]) { return0; } void closeup(void)
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
100%
Complete the following code. The goal is to implement the producer-consumer problem. You are expected to extend the provided C code to synchronize the thread operations consumer() and producer() such that an underflow and overflow of the queue is prevented. You are not allowed to change the code for implementing the queue operations, that is the code between lines 25 and 126 as shown in the screenshot. You must complete the missing parts as shown in the screenshot as well as complete the missing codes of producer and consumer.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
#include <fcntl.h>
#define MAX_LENGTH_CAP 100
#define INIT -127
#define UNDERFLOW (0x80 + 0x02)
#define OVERFLOW 0x80 + 0x01
#define BADPTR (0x80 + 0x03)
#define CONSUMER_TERMINATION_PROBABILITY 40
#define PRODUCER_TERMINATION_PROBABILITY 30
// ============= LOCKED ============
//sem_t ACCESS; // use a mutex instead of a binary semaphore
pthread_mutex_t ACCESS = PTHREAD_MUTEX_INITIALIZER;
sem_t *FULLSPOTS;
sem_t *EMPTYSPOTS;
struct timespec remaining;
struct timespec requested_time;
struct Queue{
unsignedint head;
unsignedint tail;
unsignedint length;
int8_t* data;
};
void enqueue(struct Queue *q, char x)
{
if(q==NULL)
{
printf("\nequeue()>> bad queue pointer %x\n", BADPTR);
return;
}
if ( q->head == (q->tail + 1) % q->length){
printf("\nenqueue()>> Attempt to overflow the queue at %p was prevented.\n", q);
}
else{
// Why is the following commented code wrong (hint: checking for the underflow condition):
// wrong: q->tail = (q->tail+1) % q->length; q->data[q->tail] = x;
q->data[q->tail-1] = x; // because arrays start at index 0 but our queue starts at index 1 we should write q->data[q->tail-1] = x;
// now we must increment the tail
if(q->tail == q->length){
q->tail = 1;
}
else{
q->tail = q->tail + 1; // this line must happen before enqueue is interruption
}
}
}
char dequeue(struct Queue *q)
{
char val;
if(q==NULL)
{
printf("\ndequeue()>> bad queue pointer %x\n", BADPTR);
return (char) BADPTR;
}
if(q->head == q->tail){
printf("\ndequeue()>> Attempt to underflow the queue at %p was prevented.\n", q);
return (char) UNDERFLOW;
}
else{
if(!q->data) // as a rule, we always check pointers before we access them
{
printf("dequeue()>> bad data pointer %x\n", BADPTR);
return (char) BADPTR;
}
val = q->data[q->head-1]; // because arrays start at index 0 but our queue starts at index 1 we should write q->data[q->head-1];
q->data[q->head-1] = INIT;
if (q->head == q->length){
q->head = 1;
}
else{
q->head = q->head + 1;
}
}
return (char) val;
}
void build(struct Queue **q, unsigned int length)
{
printf("\nbuild()>> Building a queue of length %d", length);
if( (length == 0) || (length > MAX_LENGTH_CAP) )
{
printf("\nbuild()>> Invalid length.");
return;
}
struct Queue *ptr=NULL;
if ( ! (ptr = (struct Queue*) malloc(sizeof(struct Queue))) )
{
printf("\nbuild()>> Failed to allocate memory for the structure.");
return;
}
printf("\nbuild()>> Allocated memory for the structure at the address %p", ptr);
if ((ptr->data = (int8_t*) malloc(length*sizeof(int8_t)) ) == NULL )
{
printf("\nbuild()>> Failed to allocate memory of size %lu for queue's data", length * sizeof(char));
}
ptr->length = length;
ptr->head = 1;
ptr->tail = 1;
printf("\nbuild()>> Initialize all elements of data to %d\n", (int8_t) INIT);
for (int i=0; i<length; i++)
ptr->data[i] = (int8_t) INIT;
*q = ptr;
}
// ================= UNLOCKED ====================
void *producer( void *qptr )
{
}
void *consumer( void *qptr)
{
pthread_exit(0);
}
void closeup(void);
int main(int argc, const char * argv[])
{
return0;
}
void closeup(void)
{
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 7 steps with 5 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education