SlideShare a Scribd company logo
The following code is an implementation of the producer consumer problem using a software
locking mechanism. Your tasks here require you to debug the code with the intent of achieving
the following tasks:
Task 1: Identifying the critical section
Task 2: Identify the software locks and replace them with a simplified mutex lock and unlock.
HINT: The code provided relies heavily on the in and out pointers of the buffer. You should
make the code run on a single count variable.
#include
#include
#include
#include
#define MAXSIZE 100
#define ITERATIONS 1000
int buffer[MAXSIZE]; // buffer
int nextp, nextc; // temporary storage
int count=0;
void printfunction(void * ptr)
{
int count = *(int *) ptr;
if (count==0)
{
printf("All items produced are consumed by the consumer  ");
}
else
{
for (int i=0; i<=count; i=i+1)
{
printf("%d, t",buffer[i]);
}
printf(" ");
}
}
void *producer(void *ptr)
{
int item, flag=0;
int in = *(int *) ptr;
do
{
item = (rand()%7)%10;
flag=flag+1;
nextp=item;
buffer[in]=nextp;
in=((in+1)%MAXSIZE);
while(count <= MAXSIZE)
{
count=count+1;
printf("Count = %d - incremented at producer ", count);
}
} while (flag<=ITERATIONS);
pthread_exit(NULL);
}
void *consumer(void *ptr)
{
int item, flag=ITERATIONS;
int out = *(int *) ptr;
do
{
while (count >0)
{
nextc = buffer[out];
out=(out+1)%MAXSIZE;
printf("tCount = %d - decremented at consumer ", count, flag);
count = count-1;
flag=flag-1;
}
if (count <= 0)
{
printf("consumer made to wait...faster than producer. ");
}
}while (flag>=0);
pthread_exit(NULL);
}
int main(void)
{
int in=0, out=0; //pointers
pthread_t pro, con;
// Spawn threads
pthread_create(&pro, NULL, producer, &count);
pthread_create(&con, NULL, consumer, &count);
if (rc1)
{
printf("ERROR; return code from pthread_create() is %d ", rc1);
exit(-1);
}
if (rc2)
{
printf("ERROR; return code from pthread_create() is %d ", rc2);
exit(-1);
}
// Wait for the threads to finish
// Otherwise main might run to the end
// and kill the entire process when it exits.
pthread_join(pro, NULL);
pthread_join(con, NULL);
printfunction(&count);
}
Solution
#include
#include
#include
#include
#define MAXSIZE 100
#define ITERATIONS 1000
int buffer[MAXSIZE]; // buffer
int nextp, nextc; // temporary storage
int count=0;
void printfunction(void * ptr)
{
int count = *(int *) ptr;
if (count==0)
{
printf("All items produced are consumed by the consumer  ");
}
else
{
for (int i=0; i<=count; i=i+1)
{
printf("%d, t",buffer[i]);
}
printf(" ");
}
}
void *producer(void *ptr)
{
int item, flag=0;
int in = *(int *) ptr;
do
{
item = (rand()%7)%10;
flag=flag+1;
nextp=item;
buffer[in]=nextp;
in=((in+1)%MAXSIZE);
while(count <= MAXSIZE)
{
count=count+1;
printf("Count = %d - incremented at producer ", count);
}
} while (flag<=ITERATIONS);
pthread_exit(NULL);
}
void *consumer(void *ptr)
{
int item, flag=ITERATIONS;
int out = *(int *) ptr;
do
{
while (count >0)
{
nextc = buffer[out];
out=(out+1)%MAXSIZE;
printf("tCount = %d - decremented at consumer ", count, flag);
count = count-1;
flag=flag-1;
}
if (count <= 0)
{
printf("consumer made to wait...faster than producer. ");
}
}while (flag>=0);
pthread_exit(NULL);
}
int main(void)
{
int in=0, out=0; //pointers
pthread_t pro, con;
// Spawn threads
pthread_create(&pro, NULL, producer, &count);
pthread_create(&con, NULL, consumer, &count);
if (rc1)
{
printf("ERROR; return code from pthread_create() is %d ", rc1);
exit(-1);
}
if (rc2)
{
printf("ERROR; return code from pthread_create() is %d ", rc2);
exit(-1);
}
// Wait for the threads to finish
// Otherwise main might run to the end
// and kill the entire process when it exits.
pthread_join(pro, NULL);
pthread_join(con, NULL);
printfunction(&count);
}

More Related Content

PDF
Modify this code to use multiple threads with the same data1.Modif.pdf
DOCX
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
PPTX
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PPT
Microkernel Development
PPTX
Static analysis of C++ source code
PPTX
Static analysis of C++ source code
PDF
The ProblemUsing C programming language write a program that simul.pdf
Modify this code to use multiple threads with the same data1.Modif.pdf
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
Microkernel Development
Static analysis of C++ source code
Static analysis of C++ source code
The ProblemUsing C programming language write a program that simul.pdf

Similar to The following code is an implementation of the producer consumer pro.pdf (20)

KEY
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
PDF
CUDA lab's slides of "parallel programming" course
PDF
망고100 보드로 놀아보자 15
PPT
Lập trình C
PDF
Please help with the below 3 questions, the python script is at the.pdf
PDF
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
PDF
operating system ubuntu,linux,MacProgram will work only if you g.pdf
PDF
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
PDF
Toonz code leaves much to be desired
DOCX
2.1 ### uVision Project, (C) Keil Software .docx
PDF
Functions
PPTX
Linux kernel debugging
DOCX
Dam31303 dti2143 lab sheet 7
PDF
rrxv6 Build a Riscv xv6 Kernel in Rust.pdf
PDF
40d5984d819aaa72e55aa10376b73bde_MIT6_087IAP10_lec12.pdf
PDF
Safe Clearing of Private Data
PPT
Linux_C_LabBasics.ppt
PDF
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
PPTX
PDF
Checking the Open-Source Multi Theft Auto Game
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
CUDA lab's slides of "parallel programming" course
망고100 보드로 놀아보자 15
Lập trình C
Please help with the below 3 questions, the python script is at the.pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdf
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
Toonz code leaves much to be desired
2.1 ### uVision Project, (C) Keil Software .docx
Functions
Linux kernel debugging
Dam31303 dti2143 lab sheet 7
rrxv6 Build a Riscv xv6 Kernel in Rust.pdf
40d5984d819aaa72e55aa10376b73bde_MIT6_087IAP10_lec12.pdf
Safe Clearing of Private Data
Linux_C_LabBasics.ppt
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Checking the Open-Source Multi Theft Auto Game
Ad

More from marketing413921 (20)

PDF
Complete the implementation of the Weighted Graph that we began in t.pdf
PDF
Can someone help me please I need help writing this code using Java.pdf
PDF
C++ in XcodePass the values by reference to complete the programmi.pdf
PDF
Assume an infix expression can only contain five operators, + .pdf
PDF
appropriate nursing interventionsmanagement -COPD -Crackles, wheezi.pdf
PDF
Arrange the following event in chronological order. The Roman Republi.pdf
PDF
Why do we need internal control in an organization What is its purp.pdf
PDF
Why Diamond and semiconductor material like Silicon are used in elect.pdf
PDF
Which of the following was NOT a goal of Titchener’s psychologyA..pdf
PDF
You discover a new kind of fungus with large, square-shaped spores. .pdf
PDF
Write 1-2 pages of the definitions of the followings (Use APA forma.pdf
PDF
Which cell type does not move materials Which of the following cell .pdf
PDF
What is the practical relevanceirrelevance of different digital med.pdf
PDF
using MATLABplease write down the code to compute these functions.pdf
PDF
What are the main duties of the Canadian Engineering Accreditation B.pdf
PDF
A supererogatory action is one in which a person must engage. T or F.pdf
PDF
The purpose of specialized lacunae are to provide structure to th.pdf
PDF
Prerequisites — Classes or Knowledge Required for this Course.pdf
PDF
Please dont answer if you cannot complete all the requirements. Th.pdf
PDF
Part 1 Select a controversial current topic and describe the litera.pdf
Complete the implementation of the Weighted Graph that we began in t.pdf
Can someone help me please I need help writing this code using Java.pdf
C++ in XcodePass the values by reference to complete the programmi.pdf
Assume an infix expression can only contain five operators, + .pdf
appropriate nursing interventionsmanagement -COPD -Crackles, wheezi.pdf
Arrange the following event in chronological order. The Roman Republi.pdf
Why do we need internal control in an organization What is its purp.pdf
Why Diamond and semiconductor material like Silicon are used in elect.pdf
Which of the following was NOT a goal of Titchener’s psychologyA..pdf
You discover a new kind of fungus with large, square-shaped spores. .pdf
Write 1-2 pages of the definitions of the followings (Use APA forma.pdf
Which cell type does not move materials Which of the following cell .pdf
What is the practical relevanceirrelevance of different digital med.pdf
using MATLABplease write down the code to compute these functions.pdf
What are the main duties of the Canadian Engineering Accreditation B.pdf
A supererogatory action is one in which a person must engage. T or F.pdf
The purpose of specialized lacunae are to provide structure to th.pdf
Prerequisites — Classes or Knowledge Required for this Course.pdf
Please dont answer if you cannot complete all the requirements. Th.pdf
Part 1 Select a controversial current topic and describe the litera.pdf
Ad

Recently uploaded (20)

PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
master seminar digital applications in india
PPTX
Lesson notes of climatology university.
PPTX
Presentation on HIE in infants and its manifestations
PDF
RMMM.pdf make it easy to upload and study
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Classroom Observation Tools for Teachers
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Computing-Curriculum for Schools in Ghana
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
master seminar digital applications in india
Lesson notes of climatology university.
Presentation on HIE in infants and its manifestations
RMMM.pdf make it easy to upload and study
2.FourierTransform-ShortQuestionswithAnswers.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Anesthesia in Laparoscopic Surgery in India
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Classroom Observation Tools for Teachers
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Chinmaya Tiranga quiz Grand Finale.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pharma ospi slides which help in ospi learning
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Computing-Curriculum for Schools in Ghana

The following code is an implementation of the producer consumer pro.pdf

  • 1. The following code is an implementation of the producer consumer problem using a software locking mechanism. Your tasks here require you to debug the code with the intent of achieving the following tasks: Task 1: Identifying the critical section Task 2: Identify the software locks and replace them with a simplified mutex lock and unlock. HINT: The code provided relies heavily on the in and out pointers of the buffer. You should make the code run on a single count variable. #include #include #include #include #define MAXSIZE 100 #define ITERATIONS 1000 int buffer[MAXSIZE]; // buffer int nextp, nextc; // temporary storage int count=0; void printfunction(void * ptr) { int count = *(int *) ptr; if (count==0) { printf("All items produced are consumed by the consumer "); } else { for (int i=0; i<=count; i=i+1) { printf("%d, t",buffer[i]); } printf(" "); } } void *producer(void *ptr) { int item, flag=0;
  • 2. int in = *(int *) ptr; do { item = (rand()%7)%10; flag=flag+1; nextp=item; buffer[in]=nextp; in=((in+1)%MAXSIZE); while(count <= MAXSIZE) { count=count+1; printf("Count = %d - incremented at producer ", count); } } while (flag<=ITERATIONS); pthread_exit(NULL); } void *consumer(void *ptr) { int item, flag=ITERATIONS; int out = *(int *) ptr; do { while (count >0) { nextc = buffer[out]; out=(out+1)%MAXSIZE; printf("tCount = %d - decremented at consumer ", count, flag); count = count-1; flag=flag-1; } if (count <= 0) { printf("consumer made to wait...faster than producer. "); } }while (flag>=0); pthread_exit(NULL);
  • 3. } int main(void) { int in=0, out=0; //pointers pthread_t pro, con; // Spawn threads pthread_create(&pro, NULL, producer, &count); pthread_create(&con, NULL, consumer, &count); if (rc1) { printf("ERROR; return code from pthread_create() is %d ", rc1); exit(-1); } if (rc2) { printf("ERROR; return code from pthread_create() is %d ", rc2); exit(-1); } // Wait for the threads to finish // Otherwise main might run to the end // and kill the entire process when it exits. pthread_join(pro, NULL); pthread_join(con, NULL); printfunction(&count); } Solution #include #include #include #include #define MAXSIZE 100 #define ITERATIONS 1000 int buffer[MAXSIZE]; // buffer int nextp, nextc; // temporary storage
  • 4. int count=0; void printfunction(void * ptr) { int count = *(int *) ptr; if (count==0) { printf("All items produced are consumed by the consumer "); } else { for (int i=0; i<=count; i=i+1) { printf("%d, t",buffer[i]); } printf(" "); } } void *producer(void *ptr) { int item, flag=0; int in = *(int *) ptr; do { item = (rand()%7)%10; flag=flag+1; nextp=item; buffer[in]=nextp; in=((in+1)%MAXSIZE); while(count <= MAXSIZE) { count=count+1; printf("Count = %d - incremented at producer ", count); } } while (flag<=ITERATIONS); pthread_exit(NULL); }
  • 5. void *consumer(void *ptr) { int item, flag=ITERATIONS; int out = *(int *) ptr; do { while (count >0) { nextc = buffer[out]; out=(out+1)%MAXSIZE; printf("tCount = %d - decremented at consumer ", count, flag); count = count-1; flag=flag-1; } if (count <= 0) { printf("consumer made to wait...faster than producer. "); } }while (flag>=0); pthread_exit(NULL); } int main(void) { int in=0, out=0; //pointers pthread_t pro, con; // Spawn threads pthread_create(&pro, NULL, producer, &count); pthread_create(&con, NULL, consumer, &count); if (rc1) { printf("ERROR; return code from pthread_create() is %d ", rc1); exit(-1); } if (rc2) { printf("ERROR; return code from pthread_create() is %d ", rc2);
  • 6. exit(-1); } // Wait for the threads to finish // Otherwise main might run to the end // and kill the entire process when it exits. pthread_join(pro, NULL); pthread_join(con, NULL); printfunction(&count); }